angular-local-storage icon indicating copy to clipboard operation
angular-local-storage copied to clipboard

[REQUEST] in memoryCache

Open gimox opened this issue 10 years ago • 1 comments
trafficstars

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.

gimox avatar Apr 02 '15 07:04 gimox

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;
  });
});

xlc avatar Apr 07 '15 00:04 xlc