httpu.caches

Factory, api compatible with $cacheFactory, that creates LRU caches with support for:

The returned cache is again API compatible with angular $cacheFactory.Cache

Characteristics:

Installation

Get it from bower or directly download it.

bower install --save angular-hu-caches

Add the dependencies to the HTML

<script type="text/javascript" src="bower_components/serialized-lru-cache/lib/lru-cache.js"></script>
<script type="text/javascript" src="bower_components/angular-hu-cacherp/caches.js"></script>

Add the httpu.caches dependency to your App Module

angular.module('MyApp', ['httpu.caches']);

The huCacheSerializableFactory dependency is now available

Usage

//Direct replacement of $cacheFactory, in localStorage
//Creates a cache of 50 entries saved to localStorage
var cache = huCacheSerializableFactory('myCache', {
  capacity: 50
});

//Cache whose 50 entries expire after 2 minutes, in sessionStorage
var cache = huCacheSerializableFactory('myCache', {
  capacity: 50,
  maxAge: 2 * 60 * 1000, //2 minutes in milliseconds
  storage: $window.sessionStorage
});

//2 MB cache whose entries expire after 10 minutes, in localStorage
var cache = huCacheSerializableFactory('myCache', {
  maxLength: 2 * 1024 * 1024, // 2MB of chars
  maxAge: 10 * 60 * 1000 //10 minutes in milliseconds
});

//1MB cache with compression in local storage
//You must create a dependency with the folling API
angular.module('myApp').factory('myCompressSerializer', function($window) {
  //add lz-string module to your included scripts
  //https://github.com/pieroxy/lz-string
  return {
     stringify: function(obj) {
       return $window.LZString.compress(JSON.stringify(obj));
     },
     parse: function(str) {
       return JSON.parse($window.LZString.decompress(str));
     }
  };
});

var cache = huCacheSerializableFactory('myCache', {
  maxLength: 1 * 1024 * 1024, // 1MB of compressed chars
  serializer: 'myCompressSerializer'
});

Live Example

Use cases

LICENSE

The MIT License (MIT)

Copyright (c) 2015 Telefónica I+D - http://www.tid.es