grevory / angular-local-storage

An AngularJS module that gives you access to the browsers local storage with cookie fallback
Other
2.83k stars 585 forks source link

[REQUEST] in memoryCache #208

Open gimox opened 9 years ago

gimox commented 9 years ago

request for memory cache.

if key is present in memory, serve it from, memory else call storage (cookie/db). when set a key save to memory and local storage.

this is the only think that this class need.

xlc commented 9 years ago

I use this script to patch it with $provide.decorator

app.config(function($provide) {
  $provide.decorator('localStorageService', function($delegate) {
    var original;
    original = {
      remove: $delegate.remove,
      set: $delegate.set,
      get: $delegate.get
    };
    $delegate.my_cache = {};
    $delegate.remove = function(key) {
      delete this.my_cache[key];
      return original.remove.call(this, key);
    };
    $delegate.set = function(key, val) {
      this.my_cache[key] = val;
      return original.set.call(this, key, val);
    };
    $delegate.get = function(key) {
      var val;
      if (this.my_cache.hasOwnProperty(key)) {
        return this.my_cache[key];
      }
      val = original.get.call(this, key);
      this.my_cache[key] = val;
      return val;
    };
    return $delegate;
  });
});