|
| 1 | +@doc overview |
| 2 | +@id basics |
| 3 | +@name Basics |
| 4 | +@description |
| 5 | + |
| 6 | +## Create a cache |
| 7 | +First, inject `$angularCacheFactory` then create as many caches as you so desire. Let's go: |
| 8 | + |
| 9 | +```javascript |
| 10 | +app.service('myService', function ($angularCacheFactory) { |
| 11 | + |
| 12 | + // Create a new cache called "profileCache" |
| 13 | + var profileCache = $angularCacheFactory('profileCache'); |
| 14 | +}); |
| 15 | +``` |
| 16 | + |
| 17 | +Let's add some items to the cache: |
| 18 | + |
| 19 | +```javascript |
| 20 | +profileCache.put('/profiles/34', { |
| 21 | + name: 'John', |
| 22 | + skills: ['programming', 'piano'] |
| 23 | +}); |
| 24 | + |
| 25 | +profileCache.put('/profiles/22', { |
| 26 | + name: 'Sally', |
| 27 | + skills: ['marketing', 'climbing', 'painting'] |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +Right now, these items will stay in the cache indefinitely. What if, for the sake of the example, I know that users change their skills in their profile once a week. The profile information is subject to change, but we don't want to make tons of requests for profiles that probably haven't changed. |
| 32 | + |
| 33 | +Let's have items which are added to `profileCache` expire after an hour: |
| 34 | + |
| 35 | +```javascript |
| 36 | +profileCache.setOptions({ |
| 37 | + maxAge: 3600000 |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +Perfect. Say we also want the items removed from the cache when they expire: |
| 42 | + |
| 43 | +```javascript |
| 44 | +profileCache.setOptions({ |
| 45 | + deleteOnExpire: 'aggressive' |
| 46 | +}); |
| 47 | +``` |
| 48 | + |
| 49 | +Let's say that when the items do expire, we want to refresh them with new values: |
| 50 | + |
| 51 | +```javascript |
| 52 | +profileCache.setOptions({ |
| 53 | + onExpire: function (key, value) { |
| 54 | + $http.get(key).success(function (data) { |
| 55 | + profileCache.put(key, data); |
| 56 | + }); |
| 57 | + } |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +Sweet! Now we'd probably have configured our cache correctly when we created it: |
| 62 | + |
| 63 | +```javascript |
| 64 | +var profileCache = $angularCacheFactory('profileCache', { |
| 65 | + maxAge: 3600000, |
| 66 | + deleteOnExpire: 'aggressive', |
| 67 | + onExpire: function (key, value) { |
| 68 | + $http.get(key).success(function (data) { |
| 69 | + profileCache.put(key, data); |
| 70 | + }); |
| 71 | + } |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +Or say we want all of our caches to use that configuration as their default: |
| 76 | + |
| 77 | +```javascript |
| 78 | +angular.module('app', ['jmdobry.angular-cache']) |
| 79 | + .config(function ($angularCacheFactoryProvider) { |
| 80 | + |
| 81 | + $angularCacheFactoryProvider.setCacheDefaults({ |
| 82 | + maxAge: 3600000, |
| 83 | + deleteOnExpire: 'aggressive', |
| 84 | + onExpire: function (key, value) { |
| 85 | + $http.get(key).success(function (data) { |
| 86 | + profileCache.put(key, data); |
| 87 | + }); |
| 88 | + } |
| 89 | + }); |
| 90 | + }); |
| 91 | +``` |
| 92 | + |
| 93 | +## Working with a cache |
| 94 | +We can retrieve items from a cache like so: |
| 95 | + |
| 96 | +```javascript |
| 97 | +var profile = profileCache.get('/profiles/34'); |
| 98 | + |
| 99 | +profile.name; // 'John' |
| 100 | +``` |
| 101 | + |
| 102 | +And get information about items in the cache: |
| 103 | + |
| 104 | +```javascript |
| 105 | +var info = profileCache.info('/profiles/34'); |
| 106 | + |
| 107 | +info.isExpired; // false |
| 108 | +// etc. |
| 109 | +``` |
| 110 | + |
| 111 | +and information about the cache itself: |
| 112 | + |
| 113 | +```javascript |
| 114 | +var info = profileCache.info(); |
| 115 | + |
| 116 | +info.size; // 2 |
| 117 | +info.maxAge; // 3600000 |
| 118 | +info.deleteOnExpire; // 'aggressive' |
| 119 | +// etc. |
| 120 | +``` |
| 121 | + |
| 122 | +Items are easily removed, and we can destroy out cache when we're done with it: |
| 123 | + |
| 124 | +```javascript |
| 125 | +profileCache.remove('/profiles/34'); |
| 126 | + |
| 127 | +profileCache.get('/profiles/34'); // undefined |
| 128 | + |
| 129 | +profileCache.destroy(); |
| 130 | + |
| 131 | +$angularCacheFactory.get('profileCache'); // undefined |
| 132 | +``` |
| 133 | + |
| 134 | +See the [API Documentation](api.html) for more information on the available methods. |
0 commit comments