Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active September 27, 2018 15:47
Show Gist options
  • Save nathansmith/9793648 to your computer and use it in GitHub Desktop.
Save nathansmith/9793648 to your computer and use it in GitHub Desktop.
Example of self-clearing cache using window.localStorage
// Set up "namespace."
var APP = APP || {};
// `Shared settings.
//----------------------------------------------------------------------------------------------------
APP.config = APP.config || {};
// How long to keep cache, in minutes.
APP.config.cache_duration = APP.config.cache_duration || 15;
// `Local storage cache.
//----------------------------------------------------------------------------------------------------
APP.cache = APP.cache || (function($, window, document, undefined) {
// Strict mode.
'use strict';
// Internal reference.
var cache = window.localStorage || {};
var can_clear = typeof cache.clear === 'function';
// What time is it?
var now = new Date().getTime();
// When was it last updated?
var m = 60000;
var t = RWD.config.cache_duration * m;
var timestamp = cache.timestamp;
// Is the timestamp valid?
var invalid = !timestamp || now - timestamp > t;
if (invalid) {
// Flush the cache.
can_clear && cache.clear();
// New timestamp.
cache.timestamp = now;
}
// Expose as "APP.cache"
return cache;
// Parameters: jQuery, window, document.
})(jQuery, this, this.document);
@nsrau
Copy link

nsrau commented Sep 27, 2018

export class LocalstorageService {

  private _localstorage: Storage = window.localStorage;
  private _cache_duration = 15;

  constructor() {
    if (!this.supportsLocalStorage()) {
      console.error('LocalstorageService: No HTML5 localStorage Support');
    } else {
      this.AutoClear();
    }
  }

  supportsLocalStorage(): boolean {
    return typeof(this._localstorage) !== 'undefined';
  }

  Set(key: string, val): void {
    this._localstorage.setItem(key, val);
  }

  Get(key: string): any {
    return this._localstorage.getItem(key);
  }

  Remove(key): void {
    this._localstorage.removeItem(key);
  }

  SetJSON(key: string, val: Object): void {
    try {
      this.Set(key, JSON.stringify(val));
    } catch (e) {
      console.error(e);
    }
  }

  GetJSON(key: string): any {
    try {
      return JSON.parse(this.Get(key));
    } catch (e) {
      console.error(e);
      return null;
    }
  }

  ClearAll(): void {
    this._localstorage.clear();
  }

  private AutoClear(): void {
    // When was it last updated?
    const m = 60000;
    const t = this._cache_duration * m;
    const timestamp = this.Get('check_duration');
    const now = new Date().getTime();
    // Is the timestamp valid?
    const invalid = !timestamp || now - timestamp > t;

    if (invalid) {
      // Flush the cache.
      this.ClearAll();

      // New timestamp.
      this.Set('check_duration', now);
    }
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment