diff --git a/CHANGELOG.md b/CHANGELOG.md index aaaa428..3a3886e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +##### 1.2.0 - 20 September 2013 + +###### Backwards compatible API changes +- Added AngularCache#info(key) #43 + +###### Backwards compatible bug fixes +- Fixed #39, #44, #49, #50 + ##### 1.1.0 - 03 September 2013 ###### Backwards compatible API changes diff --git a/Gruntfile.js b/Gruntfile.js index d95b071..368f18d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -13,7 +13,7 @@ module.exports = function (grunt) { pkg: grunt.file.readJSON('package.json'), clean: ['dist/', 'docs/'], jshint: { - all: ['Gruntfile.js', 'src/**/*.js', 'test/angular-cacheSpec.js'], + all: ['Gruntfile.js', 'src/**/*.js', 'test/*.js'], jshintrc: '.jshintrc' }, copy: { @@ -37,7 +37,7 @@ module.exports = function (grunt) { }, karma: { options: { - configFile: 'test/karma.conf.js', + configFile: './karma.conf.js', singleRun: true, autoWatch: false }, diff --git a/README.md b/README.md index 9a0c9ba..34eee96 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -angular-cache (1.1.0) -===================== -angular-cache is a caching system that improves upon the capabilities of the $cacheFactory provided by AngularJS. Check out the [demo](http://jmdobry.github.io/angular-cache/demo/) for a quick introduction, or continue on down for more detailed information. +### angular-cache (1.2.0) is a very useful replacement for Angular's $cacheFactory. + +Check out the [demo](http://jmdobry.github.io/angular-cache/demo/) for a quick introduction, or continue on down for more detailed information. The goal of the project is to solve a general problem, not satisfy a specific scenario. @@ -8,6 +8,8 @@ The goal of the project is to solve a general problem, not satisfy a specific sc #### [View the Demo](http://jmdobry.github.io/angular-cache/demo/) +#### [Mailing List](https://groups.google.com/forum/#!forum/angular-cache) + ##### $cacheFactory ```javascript // Angular's provided $cacheFactory @@ -233,9 +235,9 @@ $angularCacheFactory.get('someCache').setOptions({ capacity: 4500 }); ## Status | Version | Branch | Build status | Test Coverage | | ------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | -| 1.1.0 | [master](https://github.com/jmdobry/angular-cache) | [![Build Status](https://travis-ci.org/jmdobry/angular-cache.png?branch=master)](https://travis-ci.org/jmdobry/angular-cache) | [Test Coverage](http://jmdobry.github.io/angular-cache/coverage/) | -| 1.1.0 | [develop](https://github.com/jmdobry/angular-cache/tree/develop) | [![Build Status](https://travis-ci.org/jmdobry/angular-cache.png?branch=develop)](https://travis-ci.org/jmdobry/angular-cache) | | -| 1.1.0 | [all](https://drone.io/github.com/jmdobry/angular-cache) | [![Build Status](https://drone.io/github.com/jmdobry/angular-cache/status.png)](https://drone.io/github.com/jmdobry/angular-cache/latest) +| 1.2.0 | [master](https://github.com/jmdobry/angular-cache) | [![Build Status](https://travis-ci.org/jmdobry/angular-cache.png?branch=master)](https://travis-ci.org/jmdobry/angular-cache) | [Test Coverage](http://jmdobry.github.io/angular-cache/coverage/) | +| 1.2.0 | [develop](https://github.com/jmdobry/angular-cache/tree/develop) | [![Build Status](https://travis-ci.org/jmdobry/angular-cache.png?branch=develop)](https://travis-ci.org/jmdobry/angular-cache) | | +| 1.2.0 | [all](https://drone.io/github.com/jmdobry/angular-cache) | [![Build Status](https://drone.io/github.com/jmdobry/angular-cache/status.png)](https://drone.io/github.com/jmdobry/angular-cache/latest) ## Download @@ -243,8 +245,8 @@ $angularCacheFactory.get('someCache').setOptions({ capacity: 4500 }); #### Latest Stable Version | Type | File | Size | | ------------- | ----------------- | ------------------- | ---- | -| Production | [angular-cache-1.1.0.min.js](https://raw.github.com/jmdobry/angular-cache/master/dist/angular-cache-1.1.0.min.js) | 6 KB | -| Development | [angular-cache-1.1.0.js](https://raw.github.com/jmdobry/angular-cache/master/dist/angular-cache-1.1.0.js) | 34 KB | +| Production | [angular-cache-1.2.0.min.js](https://raw.github.com/jmdobry/angular-cache/master/dist/angular-cache-1.2.0.min.js) | 6 KB | +| Development | [angular-cache-1.2.0.js](https://raw.github.com/jmdobry/angular-cache/master/dist/angular-cache-1.2.0.js) | 34 KB | ## Installation @@ -595,6 +597,9 @@ app.service('myService', function ($angularCacheFactory) { cache.info(); // { ..., size: 30, cacheFlushInterval: 5500, // capacity: 1.7976931348623157e+308, maxAge: null, ... } + + cache.put('someItem', 'someValue', { maxAge: 12000, aggressiveDelete: true }); + cache.info('someItem'); // { timestamp: 12345678978, maxAge: 12000, aggressiveDelete: true, isExpired: false } }); ``` See [AngularCache#setOptions](http://jmdobry.github.io/angular-cache/docs/Cache.html#setOptions) @@ -696,6 +701,14 @@ See [AngularCache#info](http://jmdobry.github.io/angular-cache/docs/Cache.html#i ## Changelog +##### 1.2.0 - 20 September 2013 + +###### Backwards compatible API changes +- Added AngularCache#info(key) #43 + +###### Backwards compatible bug fixes +- Fixed #39, #44, #49, #50 + ##### 1.1.0 - 03 September 2013 ###### Backwards compatible API changes @@ -809,4 +822,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/bower.json b/bower.json index ca8c520..f52f3b6 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "author": "Jason Dobry", "name": "angular-cache", "description": "angular-cache is a very useful replacement for Angular's $cacheFactory.", - "version": "1.1.0", + "version": "1.2.0", "homepage": "http://jmdobry.github.io/angular-cache/", "repository": { "type": "git", diff --git a/coverage/index.html b/coverage/index.html index 0250863..540f294 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -183,16 +183,16 @@

Code coverage report for All files

- Statements: 96.05% (316 / 329)      + Statements: 96.44% (325 / 337)      - Branches: 91.09% (225 / 247)      + Branches: 91.95% (240 / 261)      Functions: 100% (44 / 44)      - Lines: 96.05% (316 / 329)      + Lines: 96.44% (325 / 337)     

@@ -215,16 +215,16 @@

- src/ - - 96.05% - (316 / 329) - 91.09% - (225 / 247) + ./src\ + + 96.44% + (325 / 337) + 91.95% + (240 / 261) 100% (44 / 44) - 96.05% - (316 / 329) + 96.44% + (325 / 337) @@ -232,7 +232,7 @@

diff --git a/coverage/src/angular-cache.js.html b/coverage/src/angular-cache.js.html index 0b12dde..346baa6 100644 --- a/coverage/src/angular-cache.js.html +++ b/coverage/src/angular-cache.js.html @@ -1,7 +1,7 @@ - Code coverage report for src/angular-cache.js + Code coverage report for ./src/angular-cache.js @@ -180,22 +180,22 @@
-

Code coverage report for src/angular-cache.js

+

Code coverage report for ./src/angular-cache.js

- Statements: 96.05% (316 / 329)      + Statements: 96.44% (325 / 337)      - Branches: 91.09% (225 / 247)      + Branches: 91.95% (240 / 261)      Functions: 100% (44 / 44)      - Lines: 96.05% (316 / 329)      + Lines: 96.44% (325 / 337)     

-
All files » src/ » angular-cache.js
+
All files » ./src\ » angular-cache.js

@@ -1030,7 +1030,27 @@ 

829 830 831 -832

- - - - - + + + + + - - + + @@ -232,7 +232,7 @@

diff --git a/dist/angular-cache-1.1.0.min.js b/dist/angular-cache-1.1.0.min.js deleted file mode 100644 index b3191bf..0000000 --- a/dist/angular-cache-1.1.0.min.js +++ /dev/null @@ -1 +0,0 @@ -!function(a,b){"use strict";function c(){this.$get=["$timeout","$window",function(a,c){function d(a){var b,c={};for(b in a)a.hasOwnProperty(b)&&(c[b]=b);return c}function e(a){var b,c=[];for(b in a)a.hasOwnProperty(b)&&c.push(b);return c}function f(f,g){function i(b,c){w[b].timeoutId=a(function(){var a;w[b]&&(a=w[b].value),C.remove(b),v.onExpire&&v.onExpire(b,a)},c)}function j(a,c){b.isNumber(a)?0>a?c("must be greater than zero!",a):c(null,a):c("must be a number!",a)}function k(a,b){a===Number.MAX_VALUE?(v.capacity=a,b(null,v.capacity)):j(a,function(a,c){if(a)b(a,c);else{for(v.capacity=c;u>v.capacity;)C.remove(z.key);b(null,v.capacity)}})}function l(a,b){null===a?(v.aggressiveDelete=!1,b(null,v.aggressiveDelete)):a===!0||a===!1?(v.aggressiveDelete=a,b(null,v.aggressiveDelete)):b("must be a boolean!",a)}function m(b,c){var d=e(w);if(null===b){v.maxAge=b;for(var f=0;f0&&v.aggressiveDelete?i(g,v.maxAge):C.remove(g)}}c(null,v.maxAge)}})}function n(b,c){v.cacheFlushIntervalId&&(clearInterval(v.cacheFlushIntervalId),delete v.cacheFlushIntervalId),null===b?(v.cacheFlushInterval=b,c(null,v.cacheFlushInterval)):j(b,function(b,d){b?c(b,d):(v.cacheFlushInterval=d,v.cacheFlushIntervalId=setInterval(function(){for(var b=e(w),c=0;ce)D.removeItem(A+".data."+a[c]);else{var f={timestamp:d.timestamp};d.maxAge&&(f.maxAge=d.maxAge),d.hasOwnProperty("aggressiveDelete")&&(f.aggressiveDelete=d.aggressiveDelete),C.put(a[c],d.value)}}t()}}function t(){v.storageMode&&D&&D.setItem(A+".keys",b.toJson(e(w)))}var u=0,v=b.extend({},{id:f}),w={},x={},y=null,z=null,A="angular-cache.caches."+f,B=!1,C=this,D=null;g=g||{},g.hasOwnProperty("aggressiveDelete")||(g.aggressiveDelete=!1),this.put=function(c,d,e){if(!b.isString(c))throw new Error("The key must be a string!");if(e&&e.maxAge&&j(e.maxAge,function(a){if(a)throw new Error("AngularCache.put(): maxAge: "+a)}),e&&e.hasOwnProperty("aggressiveDelete")&&e.aggressiveDelete!==!0&&e.aggressiveDelete!==!1)throw new Error("AngularCache.put(): aggressiveDelete must be a boolean!");if(!b.isUndefined(d)){var f=x[c]||(x[c]={key:c});return q(f),c in w||u++,w[c]={value:d},e&&e.hasOwnProperty("aggressiveDelete")&&(w[c].aggressiveDelete=e.aggressiveDelete),w[c].timestamp=e&&e.timestamp||(new Date).getTime(),(e&&e.maxAge||v.maxAge)&&(w[c].timeoutId&&a.cancel(w[c].timeoutId),(w[c].aggressiveDelete||!w[c].hasOwnProperty("aggressiveDelete")&&v.aggressiveDelete)&&i(c,e&&e.maxAge||v.maxAge)),t(),v.storageMode&&D.setItem(A+".data."+c,b.toJson(w[c])),u>v.capacity&&this.remove(z.key),d}},this.get=function(a,b){var c,d,e=x[a],f=w[a];if(e&&f)return c=f.maxAge||v.maxAge,d=f.hasOwnProperty("aggressiveDelete")?f.aggressiveDelete:v.aggressiveDelete,!d&&c&&(new Date).getTime()-f.timestamp>c?(this.remove(a),e=null,v.onExpire?(v.onExpire(a,f.value,b),void 0):b&&"function"==typeof b?(b(a,f.value),void 0):void 0):(q(e),f.value)},this.remove=function(a){var b=x[a];b&&(b===y&&(y=b.p),b===z&&(z=b.n),r(b.n,b.p),delete x[a],delete w[a],t(),v.storageMode&&D.removeItem(A+".data."+a),u--)},this.removeAll=function(){if(v.storageMode)for(var a=e(w),b=0;b - * @file angular-cache-1.1.0.js - * @version 1.1.0 - [Homepage]{@link http://jmdobry.github.io/angular-cache/} + * @file angular-cache-1.2.0.js + * @version 1.2.0 - [Homepage]{@link http://jmdobry.github.io/angular-cache/} * @copyright (c) 2013 Jason Dobry * @license MIT * @@ -298,7 +298,7 @@ if (!cacheDirty) { _loadCacheConfig(); } else { - _saveCacheConfig(); + _syncToStorage(null); keys = _keys(data); for (i = 0; i < keys.length; i++) { storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]])); @@ -313,7 +313,7 @@ if (!cacheDirty) { _loadCacheConfig(); } else { - _saveCacheConfig(); + _syncToStorage(null); keys = _keys(data); for (i = 0; i < keys.length; i++) { storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]])); @@ -470,22 +470,26 @@ if (data.hasOwnProperty('aggressiveDelete')) { options.aggressiveDelete = data.aggressiveDelete; } - self.put(keys[i], data.value); + self.put(keys[i], data.value, options); } } - _saveCacheConfig(); + _syncToStorage(null, null); } } /** - * @method _saveCacheConfig - * @desc If storageMode is set, save current keys of cache to localStorage. + * @method _syncToStorage + * @desc If storageMode is set, sync to localStorage. + * @param {String} key The identifier of the item to sync. * @private * @ignore */ - function _saveCacheConfig() { + function _syncToStorage(key) { if (config.storageMode && storage) { storage.setItem(prefix + '.keys', angular.toJson(_keys(data))); + if (key) { + storage.setItem(prefix + '.data.' + key, angular.toJson(data[key])); + } } } @@ -494,7 +498,7 @@ * @desc Add a key-value pair with timestamp to the cache. * @param {String} key The identifier for the item to add to the cache. * @param {*} value The value of the item to add to the cache. - * @param {Object} [options] { maxAge: {Number} } + * @param {Object} [options] {{ maxAge: {Number}, aggressiveDelete: {Boolean}, timestamp: {Number} }} * @returns {*} value The value of the item added to the cache. * @privileged */ @@ -525,12 +529,20 @@ if (!(key in data)) { size++; + } else { + if (data[key].timeoutId) { + $timeout.cancel(data[key].timeoutId); + } } data[key] = { value: value }; + if (options && options.maxAge) { + data[key].maxAge = options.maxAge; + } + if (options && options.hasOwnProperty('aggressiveDelete')) { data[key].aggressiveDelete = options.aggressiveDelete; } @@ -538,19 +550,12 @@ data[key].timestamp = (options && options.timestamp) || new Date().getTime(); if ((options && options.maxAge) || config.maxAge) { - if (data[key].timeoutId) { - $timeout.cancel(data[key].timeoutId); - } if (data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete)) { _setTimeoutToRemove(key, ((options && options.maxAge) || config.maxAge)); } } - _saveCacheConfig(); - - if (config.storageMode) { - storage.setItem(prefix + '.data.' + key, angular.toJson(data[key])); - } + _syncToStorage(key); if (size > config.capacity) { this.remove(staleEnd.key); @@ -603,6 +608,8 @@ _refresh(lruEntry); + _syncToStorage(key); + return item.value; }; @@ -630,7 +637,7 @@ delete lruHash[key]; delete data[key]; - _saveCacheConfig(); + _syncToStorage(null); if (config.storageMode) { storage.removeItem(prefix + '.data.' + key); @@ -659,7 +666,7 @@ staleEnd = null; if (config.storageMode) { - _saveCacheConfig(); + _syncToStorage(); } }; @@ -692,8 +699,21 @@ * @returns {Object} stats Object containing information about this cache. * @privileged */ - this.info = function () { - return angular.extend({}, config, { size: size }); + this.info = function (key) { + if (key in data) { + var info = { + timestamp: data[key].timestamp, + maxAge: data[key].maxAge || config.maxAge, + aggressiveDelete: data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete) || false, + isExpired: false + }; + if (info.maxAge) { + info.isExpired = (new Date().getTime() - info.timestamp) > info.maxAge; + } + return info; + } else { + return angular.extend({}, config, { size: size }); + } }; /** diff --git a/dist/angular-cache-1.2.0.min.js b/dist/angular-cache-1.2.0.min.js new file mode 100644 index 0000000..9b76f02 --- /dev/null +++ b/dist/angular-cache-1.2.0.min.js @@ -0,0 +1 @@ +!function(a,b){"use strict";function c(){this.$get=["$timeout","$window",function(a,c){function d(a){var b,c={};for(b in a)a.hasOwnProperty(b)&&(c[b]=b);return c}function e(a){var b,c=[];for(b in a)a.hasOwnProperty(b)&&c.push(b);return c}function f(f,g){function i(b,c){w[b].timeoutId=a(function(){var a;w[b]&&(a=w[b].value),C.remove(b),v.onExpire&&v.onExpire(b,a)},c)}function j(a,c){b.isNumber(a)?0>a?c("must be greater than zero!",a):c(null,a):c("must be a number!",a)}function k(a,b){a===Number.MAX_VALUE?(v.capacity=a,b(null,v.capacity)):j(a,function(a,c){if(a)b(a,c);else{for(v.capacity=c;u>v.capacity;)C.remove(z.key);b(null,v.capacity)}})}function l(a,b){null===a?(v.aggressiveDelete=!1,b(null,v.aggressiveDelete)):a===!0||a===!1?(v.aggressiveDelete=a,b(null,v.aggressiveDelete)):b("must be a boolean!",a)}function m(b,c){var d=e(w);if(null===b){v.maxAge=b;for(var f=0;f0&&v.aggressiveDelete?i(g,v.maxAge):C.remove(g)}}c(null,v.maxAge)}})}function n(b,c){v.cacheFlushIntervalId&&(clearInterval(v.cacheFlushIntervalId),delete v.cacheFlushIntervalId),null===b?(v.cacheFlushInterval=b,c(null,v.cacheFlushInterval)):j(b,function(b,d){b?c(b,d):(v.cacheFlushInterval=d,v.cacheFlushIntervalId=setInterval(function(){for(var b=e(w),c=0;ce)D.removeItem(A+".data."+a[c]);else{var f={timestamp:d.timestamp};d.maxAge&&(f.maxAge=d.maxAge),d.hasOwnProperty("aggressiveDelete")&&(f.aggressiveDelete=d.aggressiveDelete),C.put(a[c],d.value,f)}}t(null,null)}}function t(a){v.storageMode&&D&&(D.setItem(A+".keys",b.toJson(e(w))),a&&D.setItem(A+".data."+a,b.toJson(w[a])))}var u=0,v=b.extend({},{id:f}),w={},x={},y=null,z=null,A="angular-cache.caches."+f,B=!1,C=this,D=null;g=g||{},g.hasOwnProperty("aggressiveDelete")||(g.aggressiveDelete=!1),this.put=function(c,d,e){if(!b.isString(c))throw new Error("The key must be a string!");if(e&&e.maxAge&&j(e.maxAge,function(a){if(a)throw new Error("AngularCache.put(): maxAge: "+a)}),e&&e.hasOwnProperty("aggressiveDelete")&&e.aggressiveDelete!==!0&&e.aggressiveDelete!==!1)throw new Error("AngularCache.put(): aggressiveDelete must be a boolean!");if(!b.isUndefined(d)){var f=x[c]||(x[c]={key:c});return q(f),c in w?w[c].timeoutId&&a.cancel(w[c].timeoutId):u++,w[c]={value:d},e&&e.maxAge&&(w[c].maxAge=e.maxAge),e&&e.hasOwnProperty("aggressiveDelete")&&(w[c].aggressiveDelete=e.aggressiveDelete),w[c].timestamp=e&&e.timestamp||(new Date).getTime(),(e&&e.maxAge||v.maxAge)&&(w[c].aggressiveDelete||!w[c].hasOwnProperty("aggressiveDelete")&&v.aggressiveDelete)&&i(c,e&&e.maxAge||v.maxAge),t(c),u>v.capacity&&this.remove(z.key),d}},this.get=function(a,b){var c,d,e=x[a],f=w[a];if(e&&f)return c=f.maxAge||v.maxAge,d=f.hasOwnProperty("aggressiveDelete")?f.aggressiveDelete:v.aggressiveDelete,!d&&c&&(new Date).getTime()-f.timestamp>c?(this.remove(a),e=null,v.onExpire?(v.onExpire(a,f.value,b),void 0):b&&"function"==typeof b?(b(a,f.value),void 0):void 0):(q(e),t(a),f.value)},this.remove=function(a){var b=x[a];b&&(b===y&&(y=b.p),b===z&&(z=b.n),r(b.n,b.p),delete x[a],delete w[a],t(null),v.storageMode&&D.removeItem(A+".data."+a),u--)},this.removeAll=function(){if(v.storageMode)for(var a=e(w),b=0;bc.maxAge),c}return b.extend({},v,{size:u})},this.keySet=function(){return d(w)},this.keys=function(){return e(w)},this.setOptions=p,p(g,!0)}function g(a,c){if(a in h)throw new Error("cacheId "+a+" taken!");if(!b.isString(a))throw new Error("cacheId must be a string!");return h[a]=new f(a,c),h[a]}var h={};return g.info=function(){for(var a={},b=e(h),c=0;c
Source:
@@ -144,7 +144,7 @@

Index

Modules

  • diff --git a/docs/AngularCache.html b/docs/AngularCache.html index ec65271..029a7b0 100644 --- a/docs/AngularCache.html +++ b/docs/AngularCache.html @@ -1,1269 +1,1267 @@ - - - - - JSDoc: Class: AngularCache - - - - - - - - - - -
    - -

    Class: AngularCache

    - - - - - -
    - -
    -

    - AngularCache -

    - -
    - -
    -
    - - - - -
    -

    new AngularCache(cacheId, options)

    - - -
    -
    - - -
    - Instantiated via $angularCacheFactory(cacheId[, options]) -
    - - - - - - - -
    Parameters:
    - - -

  +832 +833 +834 +835 +836 +837 +838 +839 +840 +841 +842 +843 +844 +845 +846 +847 +848 +849 +850 +851 +852        @@ -1090,13 +1110,13 @@

    1 -178 -178 -83 -83 +211 +211 +101 +101     -178 +211       @@ -1133,7 +1153,7 @@

    1 -20 +21 20 20 20 @@ -1154,12 +1174,12 @@

    1 -51 +53 4 -47 +49 4   -43 +45       @@ -1281,18 +1301,18 @@

  8 8 -3 -3 +27 +27 6 6       -3 -3 -3 -3 -3 +27 +27 +27 +27 +27   8   @@ -1444,17 +1464,17 @@

    1 -109 -97 +111 +98 47 -50 +51 15     -97 -97 -97 -97 +98 +98 +98 +98       @@ -1467,12 +1487,12 @@

    1 -238 -132 -132 +240 +133 +133   -132 -50 +133 +51       @@ -1497,7 +1517,7 @@

    8 -  +1   8   @@ -1515,9 +1535,13 @@

      +  1 -152 -59 +180 +68 +68 +31 +        @@ -1532,63 +1556,64 @@

  77   -89 +90 3   -86 -6 -6 +87 +8 +8 2       -84 -2 +85 +3       -84 +85 1     -83 +84   -83 +84   +84 83 -82   +1 +    -83     +84   -83 -2     -83 +84 +6   -83 -25 -    -25 -18 +84 +3     +84   -83 +84 +26 +19   -83 -22     -83 +84 +  +84 3     -83 +84       @@ -1601,20 +1626,20 @@

    77 -72 +73         -72 +73 40     -32 -32 +33 +33     -32 +33 8   6 @@ -1633,9 +1658,11 @@

      -26 +27   -26 +27 +  +27       @@ -1725,6 +1752,18 @@

    77 +70 +7 +  +  +  +  +  +7 +4 +  +7 +  63     @@ -1734,6 +1773,7 @@

      +  77 2   @@ -2161,7 +2201,7 @@

if (!cacheDirty) { _loadCacheConfig(); } else { - _saveCacheConfig(); + _syncToStorage(null); keys = _keys(data); for (i = 0; i < keys.length; i++) { storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]])); @@ -2176,7 +2216,7 @@

if (!cacheDirty) { _loadCacheConfig(); } else { - _saveCacheConfig(); + _syncToStorage(null); keys = _keys(data); for (i = 0; i < keys.length; i++) { storage.setItem(prefix + '.data.' + keys[i], angular.toJson(data[keys[i]])); @@ -2327,28 +2367,32 @@

var options = { timestamp: data.timestamp }; - Iif (data.maxAge) { - options.maxAge = data.maxAge; + if (data.maxAge) { + options.maxAge = data.maxAge; } Iif (data.hasOwnProperty('aggressiveDelete')) { options.aggressiveDelete = data.aggressiveDelete; } - self.put(keys[i], data.value); + self.put(keys[i], data.value, options); } } - _saveCacheConfig(); + _syncToStorage(null, null); } }   /** - * @method _saveCacheConfig - * @desc If storageMode is set, save current keys of cache to localStorage. + * @method _syncToStorage + * @desc If storageMode is set, sync to localStorage. + * @param {String} key The identifier of the item to sync. * @private * @ignore */ - function _saveCacheConfig() { + function _syncToStorage(key) { if (config.storageMode && storage) { storage.setItem(prefix + '.keys', angular.toJson(_keys(data))); + if (key) { + storage.setItem(prefix + '.data.' + key, angular.toJson(data[key])); + } } }   @@ -2357,7 +2401,7 @@

* @desc Add a key-value pair with timestamp to the cache. * @param {String} key The identifier for the item to add to the cache. * @param {*} value The value of the item to add to the cache. - * @param {Object} [options] { maxAge: {Number} } + * @param {Object} [options] {{ maxAge: {Number}, aggressiveDelete: {Boolean}, timestamp: {Number} }} * @returns {*} value The value of the item added to the cache. * @privileged */ @@ -2388,11 +2432,19 @@

  if (!(key in data)) { size++; + } else { + Iif (data[key].timeoutId) { + $timeout.cancel(data[key].timeoutId); + } }   data[key] = { value: value }; +  + if (options && options.maxAge) { + data[key].maxAge = options.maxAge; + }   if (options && options.hasOwnProperty('aggressiveDelete')) { data[key].aggressiveDelete = options.aggressiveDelete; @@ -2401,19 +2453,12 @@

data[key].timestamp = (options && options.timestamp) || new Date().getTime();   if ((options && options.maxAge) || config.maxAge) { - Iif (data[key].timeoutId) { - $timeout.cancel(data[key].timeoutId); - } if (data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete)) { _setTimeoutToRemove(key, ((options && options.maxAge) || config.maxAge)); } }   - _saveCacheConfig(); -  - if (config.storageMode) { - storage.setItem(prefix + '.data.' + key, angular.toJson(data[key])); - } + _syncToStorage(key);   if (size > config.capacity) { this.remove(staleEnd.key); @@ -2465,6 +2510,8 @@

}   _refresh(lruEntry); +  + _syncToStorage(key);   return item.value; }; @@ -2493,7 +2540,7 @@

delete lruHash[key]; delete data[key];   - _saveCacheConfig(); + _syncToStorage(null);   if (config.storageMode) { storage.removeItem(prefix + '.data.' + key); @@ -2522,7 +2569,7 @@

staleEnd = null;   if (config.storageMode) { - _saveCacheConfig(); + _syncToStorage(); } };   @@ -2555,8 +2602,21 @@

* @returns {Object} stats Object containing information about this cache. * @privileged */ - this.info = function () { - return angular.extend({}, config, { size: size }); + this.info = function (key) { + if (key in data) { + var info = { + timestamp: data[key].timestamp, + maxAge: data[key].maxAge || config.maxAge, + aggressiveDelete: data[key].aggressiveDelete || (!data[key].hasOwnProperty('aggressiveDelete') && config.aggressiveDelete) || false, + isExpired: false + }; + if (info.maxAge) { + info.isExpired = (new Date().getTime() - info.timestamp) > info.maxAge; + } + return info; + } else { + return angular.extend({}, config, { size: size }); + } };   /** @@ -2697,7 +2757,7 @@

diff --git a/coverage/src/index.html b/coverage/src/index.html index 422bdc0..b7cedd9 100644 --- a/coverage/src/index.html +++ b/coverage/src/index.html @@ -1,7 +1,7 @@ - Code coverage report for src/ + Code coverage report for ./src\ @@ -180,22 +180,22 @@
-

Code coverage report for src/

+

Code coverage report for ./src\

- Statements: 96.05% (316 / 329)      + Statements: 96.44% (325 / 337)      - Branches: 91.09% (225 / 247)      + Branches: 91.95% (240 / 261)      Functions: 100% (44 / 44)      - Lines: 96.05% (316 / 329)      + Lines: 96.44% (325 / 337)     

-
All files » src/
+
All files » ./src\
@@ -216,15 +216,15 @@

angular-cache.js96.05%(316 / 329)91.09%(225 / 247)96.44%(325 / 337)91.95%(240 / 261) 100% (44 / 44)96.05%(316 / 329)96.44%(325 / 337)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
cacheId - - -String - - - - - - - - - - The id of the new cache.
options - - -Object - - - - - - <optional>
- - - - - -
{{[capacity]: Number, [maxAge]: Number, [cacheFlushInterval]: Number, [aggressiveDelete]: Boolean, [onExpire]: Function, [storageMode]: String, [localStorageImpl]: Object}}
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - -
- - - - - - - - - - - - - - -

Methods

- -
- -
-

<static> destroy()

- - -
-
- - -
- Completely destroy this cache. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - -
- - - -
-

<static> get(key, onExpire) → {*}

- - -
-
- - -
- Retrieve the item from the cache with the specified key. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
key - - -String - - - - - - - - - - The key of the item to retrieve.
onExpire - - -Function - - - - - - <optional>
- - - - - -
Callback to be executed if it is discovered the -requested item has expired.
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -
Returns:
- - -
- The value of the item in the cache with the specified key. -
- - - -
-
- Type -
-
- -* - - -
-
- - - - -
- - - -
-

<static> info() → {Object}

- - -
-
- - -
- Return an object containing information about this cache. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -
Returns:
- - -
- stats Object containing information about this cache. -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - - -
-

<static> keys() → {Array}

- - -
-
- - -
- Return an array of the keys of all items currently in this cache.. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -
Returns:
- - -
- An array of the keys of all items currently in this cache.. -
- - - -
-
- Type -
-
- -Array - - -
-
- - - - -
- - - -
-

<static> keySet() → {Object}

- - -
-
- - -
- Return the set of the keys of all items currently in this cache. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -
Returns:
- - -
- The set of the keys of all items currently in this cache. -
- - - -
-
- Type -
-
- -Object - - -
-
- - - - -
- - - -
-

<static> put(key, value, options) → {*}

- - -
-
- - -
- Add a key-value pair with timestamp to the cache. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeArgumentDescription
key - - -String - - - - - - - - - - The identifier for the item to add to the cache.
value - - -* - - - - - - - - - - The value of the item to add to the cache.
options - - -Object - - - - - - <optional>
- - - - - -
{ maxAge: {Number} }
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - -
Returns:
- - -
- value The value of the item added to the cache. -
- - - -
-
- Type -
-
- -* - - -
-
- - - - -
- - - -
-

<static> remove(key)

- - -
-
- - -
- Remove the specified key-value pair from this cache. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
key - - -String - - - - The key of the key-value pair to remove.
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - -
- - - -
-

<static> removeAll()

- - -
-
- - -
- Clear this cache. -
- - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - -
- - - -
-

<static> setOptions(options, strict)

- - -
-
- - -
- Configure this cache with the given options. -
- - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
options - - -Object - - - -
strict - - -Boolean - - - - If true then any existing configuration will be reset to defaults before -applying the new options, otherwise only the options specified in the hash will be altered.
- - - -
- - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - -
- -
- - - - - - - - - - - - - - - - -
- - - - - - + + + + + JSDoc: Class: AngularCache + + + + + + + + + + +
+ +

Class: AngularCache

+ + + + + +
+ +
+

+ AngularCache +

+ +
+ +
+
+ + + + +
+

new AngularCache(cacheId, options)

+ + +
+
+ + +
+ Instantiated via $angularCacheFactory(cacheId[, options]) +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
cacheId + + +String + + + + + + + + + + The id of the new cache.
options + + +Object + + + + + + <optional>
+ + + + + +
{{[capacity]: Number, [maxAge]: Number, [cacheFlushInterval]: Number, [aggressiveDelete]: Boolean, [onExpire]: Function, [storageMode]: String, [localStorageImpl]: Object}}
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
+ + +
+ + + + + + + + + + + + + + +

Methods

+ +
+ +
+

<static> destroy()

+ + +
+
+ + +
+ Completely destroy this cache. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
+ + + +
+

<static> get(key, onExpire) → {*}

+ + +
+
+ + +
+ Retrieve the item from the cache with the specified key. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
key + + +String + + + + + + + + + + The key of the item to retrieve.
onExpire + + +Function + + + + + + <optional>
+ + + + + +
Callback to be executed if it is discovered the requested item has expired.
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +
Returns:
+ + +
+ The value of the item in the cache with the specified key. +
+ + + +
+
+ Type +
+
+ +* + + +
+
+ + + + +
+ + + +
+

<static> info() → {Object}

+ + +
+
+ + +
+ Return an object containing information about this cache. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +
Returns:
+ + +
+ stats Object containing information about this cache. +
+ + + +
+
+ Type +
+
+ +Object + + +
+
+ + + + +
+ + + +
+

<static> keys() → {Array}

+ + +
+
+ + +
+ Return an array of the keys of all items currently in this cache.. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +
Returns:
+ + +
+ An array of the keys of all items currently in this cache.. +
+ + + +
+
+ Type +
+
+ +Array + + +
+
+ + + + +
+ + + +
+

<static> keySet() → {Object}

+ + +
+
+ + +
+ Return the set of the keys of all items currently in this cache. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +
Returns:
+ + +
+ The set of the keys of all items currently in this cache. +
+ + + +
+
+ Type +
+
+ +Object + + +
+
+ + + + +
+ + + +
+

<static> put(key, value, options) → {*}

+ + +
+
+ + +
+ Add a key-value pair with timestamp to the cache. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeArgumentDescription
key + + +String + + + + + + + + + + The identifier for the item to add to the cache.
value + + +* + + + + + + + + + + The value of the item to add to the cache.
options + + +Object + + + + + + <optional>
+ + + + + +
{{ maxAge: {Number}, aggressiveDelete: {Boolean}, timestamp: {Number} }}
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +
Returns:
+ + +
+ value The value of the item added to the cache. +
+ + + +
+
+ Type +
+
+ +* + + +
+
+ + + + +
+ + + +
+

<static> remove(key)

+ + +
+
+ + +
+ Remove the specified key-value pair from this cache. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
key + + +String + + + + The key of the key-value pair to remove.
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
+ + + +
+

<static> removeAll()

+ + +
+
+ + +
+ Clear this cache. +
+ + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
+ + + +
+

<static> setOptions(options, strict)

+ + +
+
+ + +
+ Configure this cache with the given options. +
+ + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
options + + +Object + + + +
strict + + +Boolean + + + + If true then any existing configuration will be reset to defaults before applying the new options, otherwise only the options specified in the hash will be altered.
+ + + +
+ + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + +
+ +
+ + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + diff --git a/docs/AngularCacheFactory.html b/docs/AngularCacheFactory.html index bab96ac..875f931 100644 --- a/docs/AngularCacheFactory.html +++ b/docs/AngularCacheFactory.html @@ -167,7 +167,7 @@
Parameters:
Source:
@@ -242,7 +242,7 @@

Index

Modules