1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
|
# angular-cache
[![bower version](https://img.shields.io/bower/v/angular-cache.svg?style=flat)](https://www.npmjs.org/package/angular-cache)
[![npm version](https://img.shields.io/npm/v/angular-cache.svg?style=flat)](https://www.npmjs.org/package/angular-cache)
[![Circle CI](https://img.shields.io/circleci/project/jmdobry/angular-cache/master.svg?style=flat)](https://circleci.com/gh/jmdobry/angular-cache/tree/master)
[![npm downloads](https://img.shields.io/npm/dm/angular-cache.svg?style=flat)](https://www.npmjs.org/package/angular-cache)
[![codecov](https://img.shields.io/codecov/c/github/jmdobry/angular-cache.svg)](https://codecov.io/gh/jmdobry/angular-cache)
A very useful replacement for Angular's $cacheFactory.
__versions of angular-cache below 4.0.0 have been deprecated, see the [breaking changes](https://github.com/jmdobry/angular-cache/blob/master/CHANGELOG.md) in 4.0.0__
### Table of Contents
- [Quick Start](#quick-start)
- [The Basics](#the-basics)
- [Working with a cache](#working-with-a-cache)
- [Configuration Options](#configuration-options)
- [Using angular-cache with $http](#using-angular-cache-with-http)
- [Using angular-cache with localStorage](#using-angular-cache-with-localstorage)
- [API Reference](#api-reference)
### Quick Start
`bower install --save angular-cache` or `npm install --save angular-cache`.
```js
angular.module('myApp', ['angular-cache'])
.config(function (CacheFactoryProvider) {
angular.extend(CacheFactoryProvider.defaults, { maxAge: 15 * 60 * 1000 });
})
.service('BookService', function (CacheFactory, $http) {
if (!CacheFactory.get('bookCache')) {
// or CacheFactory('bookCache', { ... });
CacheFactory.createCache('bookCache', {
deleteOnExpire: 'aggressive',
recycleFreq: 60000
});
}
var bookCache = CacheFactory.get('bookCache');
return {
findBookById: function (id) {
return $http.get('/api/books/' + id, { cache: bookCache });
}
};
});
```
#### Why would you want to replace $cacheFactory?
| Feature | $cacheFactory | angular-cache |
| ----------- | ------------------- | --------------- |
| core implementation usable outside of Angular | no | [__yes__](https://github.com/jmdobry/CacheFactory) |
| key/value store | __yes__ | __yes__ |
| in-memory support | __yes__ | __yes__ |
| localStorage support | no | __yes__ |
| sessionStorage support | no | __yes__ |
| custom storage support | no | __yes__ |
| can set maximum capacity | __yes__ | __yes__ |
| LRU support | __yes__ | __yes__ |
| support for time-based expiration of items | no | __yes__ |
| remove all expired items | n/a | __yes__ |
| "touch" an item | n/a | __yes__ |
| execute callback when an item expires | n/a | __yes__ |
| get info about cache | __yes__ | __yes__ |
| get info about single item | no | __yes__ |
| get item by key | __yes__ | __yes__ |
| remove item by key | __yes__ | __yes__ |
| remove all items | __yes__ | __yes__ |
| get keys of all caches | no | __yes__ |
| clear all caches | no | __yes__ |
| disable/enable a cache | no | __yes__ |
| "touch" all items in all caches | n/a | __yes__ |
| remove expired items from all caches | n/a | __yes__ |
| get all keys in a cache | no | __yes__ |
| clear a cache on a given interval | no | __yes__ |
### The Basics
First, inject `CacheFactory` then create a cache. Let's go:
```js
app.service('myService', function (CacheFactory) {
var profileCache;
// Check to make sure the cache doesn't already exist
if (!CacheFactory.get('profileCache')) {
profileCache = CacheFactory('profileCache');
}
});
```
Let's add some items to the cache:
```js
profileCache.put('/profiles/34', {
name: 'John',
skills: ['programming', 'piano']
});
profileCache.put('/profiles/22', {
name: 'Sally',
skills: ['marketing', 'climbing', 'painting']
});
```
Right now, these items will stay in the cache until a page refresh.
Let's have items which are added to `profileCache` expire after an hour:
```js
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour
});
```
Perfect. Say we also want the items removed from the cache when they expire:
```js
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour,
deleteOnExpire: 'aggressive'
});
```
Let's say that when the items do expire, we want to refresh them with new values:
```js
profileCache = CacheFactory('profileCache', {
maxAge: 60 * 60 * 1000 // 1 hour,
deleteOnExpire: 'aggressive',
onExpire: function (key, value) {
$http.get(key).success(function (data) {
profileCache.put(key, data);
});
}
});
```
Or say we want all of our caches to use that configuration as their default:
```js
angular.module('app', ['angular-cache']).config(function (CacheFactoryProvider) {
angular.extend(CacheFactoryProvider.defaults, {
maxAge: 3600000,
deleteOnExpire: 'aggressive',
onExpire: function (key, value) {
var _this = this; // "this" is the cache in which the item expired
angular.injector(['ng']).get('$http').get(key).success(function (data) {
_this.put(key, data);
});
}
});
});
```
### Working with a cache
We can retrieve items from a cache like so:
```js
var profile = profileCache.get('/profiles/34');
profile.name; // 'John'
```
And get information about items in the cache:
```js
var info = profileCache.info('/profiles/34');
info.isExpired; // false
// etc.
```
and information about the cache itself:
```
var info = profileCache.info();
info.size; // 2
info.maxAge; // 3600000
info.deleteOnExpire; // 'aggressive'
// etc.
```
Items are easily removed, and we can destroy our cache when we're done with it:
```js
profileCache.remove('/profiles/34');
profileCache.get('/profiles/34'); // undefined
profileCache.destroy();
CacheFactory.get('profileCache'); // undefined
```
### Configuration Options
These options apply to:
- `CacheFactory(cacheId[, options)`
- `CacheFactory.createCache(cacheId[, options])`
- `Cache#setOptions(options[, strict])`
- `Cache#setMaxAge(maxAge)`, `Cache#setOnExpire(onExpire)`, etc.
##### `cacheFlushInterval`
If set, remove all items from a cache on an interval after the given number of milliseconds. Default: `null`.
##### `capacity`
Maximum number of items a cache can hold. Adding more items than the capacity will cause the cache to operate like an LRU cache, removing the least recently used items to stay under capacity. Default: `Number.MAX_VALUE`.
##### `deleteOnExpire`
Determines the behavior of a cache when an item expires. Default: `none`.
Possible values:
- `none` - Cache will do nothing when an item expires.
- `passive` - Cache will do nothing when an item expires. Expired items will remain in the cache until requested, at which point they are removed, and `undefined` is returned.
- `aggressive` - Cache will remove expired items as soon as they are discovered.
##### `disabled`
Determines whether a cache is disabled. Default: `false`.
##### `onExpire`
A callback function to be executed whenever an expired item is removed from a cache when the cache is in `passive` or `aggressive` mode. Will be passed the `key` and `value` of the expired item.
Will be passed a third `done` argument if the cache is in `passive` mode. This allows you to synchronously access the `key` and `value` of the expired item when you make the `Cache#get(key[, options])` call that is the reason the expired item is being removed in the first place. Default: `null`.
##### `maxAge`
The number of milliseconds until a newly inserted item expires. Default: `Number.MAX_VALUE`.
##### `recycleFreq`
Determines how often a cache will scan for expired items when in `aggressive` mode. Default: `1000` (milliseconds).
##### `storageImpl`
Provide a custom storage medium, e.g. a polyfill for `localStorage`. Default: `null`.
Must implement:
- `setItem` - Same API as `localStorage.setItem(key, value)`
- `getItem` - Same API as `localStorage.getItem(key)`
- `removeItem` - Same API as `localStorage.removeItem(key)`
##### `storageMode`
Determines the storage medium used by a cache. Default: `memory`.
Possible values:
- `memory` - Cache will hold data in memory. Data is cleared when the page is refreshed.
- `localStorage` - Cache will hold data in `localStorage` if available. Data is _not_ cleared when the page is refreshed.
- `sessionStorage` - Cache will hold data in `sessionStorage` if available. Data is _not_ cleared when the page is refreshed.
##### `storagePrefix`
Determines the namespace of a cache when `storageMode` is set to `localStorage` or `sessionStorage`. Make it a shorter string to save space. Default: `angular-cache.caches.`.
##### `storeOnReject`
If inserting a promise into a cache, also insert the rejection value if the promise rejects. Default: `false`.
##### storeOnResolve
If inserting a promise into a cache, also insert the resolved value if the promise resolves. Default: `false`.
### Using angular-cache with $http
__Note:__ The downside of letting `$http` handle caching for you is that it caches the responses (in string form) to your requests–not the JavaScript Object parsed from the response body. This means you can't interact with the data in the cache used by `$http`. See below for how to handle the caching yourself, which gives you more control and the ability to interact with the cache (use it as a data store).
Configure `$http` to use a cache created by `CacheFactory` by default:
```js
app.run(function ($http, CacheFactory) {
$http.defaults.cache = CacheFactory('defaultCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
});
```
```js
app.service('MyService', function ($http, $q) {
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
$http.get('api/data/' + id, {
cache: true
}).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
deferred.resolve(data);
});
return deferred.promise;
}
};
});
```
```js
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
```
Tell $http to use a cache created by CacheFactory for a specific request:
```js
app.service('MyService', function ($q, $http, CacheFactory) {
CacheFactory('dataCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
$http.get('api/data/' + id, {
cache: CacheFactory.get('dataCache')
}).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
deferred.resolve(data);
});
return deferred.promise;
}
};
});
```
```js
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
```
Do your own caching while using the $http service:
```js
app.service('MyService', function ($q, $http, CacheFactory) {
CacheFactory('dataCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour
deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
});
return {
getDataById: function (id) {
var deferred = $q.defer();
var start = new Date().getTime();
var dataCache = CacheFactory.get('dataCache');
// Now that control of inserting/removing from the cache is in our hands,
// we can interact with the data in "dataCache" outside of this context,
// e.g. Modify the data after it has been returned from the server and
// save those modifications to the cache.
if (dataCache.get(id)) {
deferred.resolve(dataCache.get(id));
} else {
$http.get('api/data/' + id).success(function (data) {
console.log('time taken for request: ' + (new Date().getTime() - start) + 'ms');
dataCache.put(id, data);
deferred.resolve(data);
});
}
return deferred.promise;
}
};
});
```
```js
app.controller('myCtrl', function (MyService) {
MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 2375ms"
// Data returned by this next call is already cached.
return MyService.getDataById(1).then(function (data) {
// e.g. "time taken for request: 1ms"
});
});
});
```
### Using angular-cache with localStorage
```js
app.service('myService', function (CacheFactory) {
// This cache will sync itself with localStorage if it exists, otherwise it won't. Every time the
// browser loads this app, this cache will attempt to initialize itself with any data it had
// already saved to localStorage (or sessionStorage if you used that).
var myAwesomeCache = CacheFactory('myAwesomeCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will use `localStorage`.
});
});
```
Using angular-cache in browsers that DON'T support localStorage:
Option 1 - Do nothing (the cache will just store data in memory)
Option 2 - Create/use a polyfill that provides the global `localStorage` and `sessionStorage` objects. angular-cache will attempt to use these if it finds them.
Option 3 - Tell angular-cache exactly which polyfill to use (also useful if you just want to use your own implementation/wrapper for localStorage):
```js
app.service('myService', function (CacheFactory) {
var localStoragePolyfill = {
getItem: function (key) { ... },
setItem: function (key, value) { ... },
removeItem: function (key) { ... }
};
// Always use the polyfill
var myAwesomeCache = CacheFactory('myAwesomeCache', {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage', // This cache will use `localStorage`.
storageImpl: localStoragePolyfill // angular-cache will use this polyfill instead of looking for localStorage
});
// Conditionally use the polyfill
var options = {
maxAge: 15 * 60 * 1000, // Items added to this cache expire after 15 minutes.
cacheFlushInterval: 60 * 60 * 1000, // This cache will clear itself every hour.
deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire.
storageMode: 'localStorage' // This cache will use `localStorage`.
};
if (!window.localStorage) {
options.storageImpl = localStoragePolyfill;
}
var myAwesomeCache = CacheFactory('myAwesomeCache', options);
});
```
Documentation on the interface that must be implemented by any storageImpl polyfill used by angular-cache can be found on the W3C Recommendation page for webstorage. The interface itself looks like:
```
interface Storage {
readonly attribute unsigned long length;
DOMString? key(unsigned long index);
getter DOMString getItem(DOMString key);
setter creator void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};
```
angular-cache cares only about these three methods:
- `setItem`
- `getItem`
- `removeItem`
One developer suggested using store.js–a wrapper and polyfill for localStorage. However, store.js has its own API that doesn't match that of the webstorage spec, so if you want to use store.js or any other 3rd-party polyfill then you'll need to create a wrapper for it if it doesn't have the same API as localStorage . For example:
```js
var storeJsToStandard {
getItem: store.get,
setItem: store.set,
removeItem: store.remove
};
CacheFactory('myNewCache', {
storageMode: 'localStorage',
storageImpl: storeJsToStandard
});
```
### API Reference
##### `CacheFactory(cacheId[, options])` & `CacheFactory.createCache(cacheId[, options])`
Create a cache. Cache must not already exist. `cacheId` must be a string. `options` is an optional argument and must be an object. Any options you pass here will override any default options.
```js
var cache = CacheFactory('cache');
var cache2 = CacheFactory.createCache('cache2');
var cache3 = CacheFactory('cache', { maxAge: 900000 });
var cache4 = CacheFactory('cache'); // Error "cache already exists!"
```
##### `CacheFactory.get(cacheId)`
Return the cache with the given `cacheId`.
##### `CacheFactory.info()`
Return an object of key-value pairs, the keys being cache ids and the values being the result of `.info()` being called on each cache.
##### `CacheFactory.keySet()`
Return the ids of all registered caches as an object.
##### `CacheFactory.keys()`
Return the ids of all registered caches as an array.
##### `CacheFactory.destroy(cacheId)`
Destroy the cache with the given `cacheId`.
##### `CacheFactory.destroyAll()`
Destroy all registered caches.
##### `CacheFactory.clearAll()`
Remove all data from all registered caches.
##### `CacheFactory.enableAll()`
Enable all registered caches.
##### `CacheFactory.disableAll()`
Disable all registered caches.
##### `CacheFactory.touchAll()`
Call `.touch()` on all registered caches.
##### `CacheFactory.removeExpiredFromAll()`
Call `.removeExpired()` on all registered caches. Returns a hash of any expired items, keyed by cache id.
##### `Cache#get(key[, options])`
Return the item with the given `key`. `options`, if provided, must be an object.
If the cache is in `passive` mode, then `options.onExpire` can be a function that will be called with the `key` and `value` of the requested item if the requested item is expired, with the `get` call itself returning undefined.
##### `Cache#put(key, value[, options])`
Insert the item with the given `key` and `value` into the cache. `options`, if provided, must be an object.
If inserting a promise, `options.storeOnReject` determines whether to insert the rejection value if the promise rejects (overriding the default `storeOnReject` setting for the cache).
If inserting a promise, `options.storeOnResolve` determines whether to insert the resolved value if the promise resolves (overriding the default `storeOnResolve` setting for the cache).
##### `Cache.remove(key)`
Remove and return the item with the given `key`, if it is in the cache.
##### `Cache.removeAll()`
Remove all items in the cache.
##### `Cache.removeExpired()`
Remove and return all expired items in the cache.
##### `Cache.destroy()`
Completely destroy this cache and its data.
##### `Cache#info([key])`
`Cache#info()` returns an object containing information about the cache.
`Cache#info(key)` returns an object containing information about the item with the given `key`, if the item is in the cache.
##### `Cache#keySet()`
Return the keys of all items in the cache as an object.
##### `Cache#keys()`
Return the keys of all items in the cache as an array.
##### `Cache#enable()`
Enable the cache.
##### `Cache#disable()`
Disable the cache.
##### `Cache#values()`
Return all values in the cache.
##### `Cache#touch([key])`
`Cache#touch()` will "touch" all items in the cache.
`Cache#touch(key)` will "touch" the item with the given `key`.
##### `Cache#setCacheFlushInterval(cacheFlushInterval)`
Set the `cacheFlushInterval` for the cache.
##### `Cache#setCapacity(capacity)`
Set the `capacity` for the cache. Setting this lower than the current item count will result in those items being removed.
##### `Cache#setDeleteOnExpire(deleteOnExpire)`
Set the `deleteOnExpire` for the cache.
##### `Cache#setMaxAge(maxAge)`
Set the `maxAge` for the cache.
##### `Cache#setOnExpire(onExpire)`
Set the `onExpire` for the cache.
##### `Cache#setRecycleFreq(recycleFreq)`
Set the `recycleFreq` for the cache.
##### `Cache#setStorageMode(storageMode)`
Set the `storageMode` for the cache. This will move data from the current storage medium to the new one.
##### `Cache#setOptions(options[, strict])`
Set multiple options for the cache at a time. Setting `strict` to `true` will reset options for the cache that are not specifically set in the `options` hash to `CacheFactoryProvider.defaults`.
### License
[MIT License](https://github.com/jmdobry/angular-cache/blob/master/LICENSE)
Copyright (C) 2013-2016 angular-cache project authors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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.
|