5
5
* @name $cacheFactory
6
6
*
7
7
* @description
8
- * Factory that constructs cache objects and gives access to them.
8
+ * Factory that constructs {@link $cacheFactory.Cache Cache} objects and gives access to
9
+ * them.
9
10
*
10
11
* ```js
11
12
*
@@ -96,8 +97,65 @@ function $CacheFactoryProvider() {
96
97
freshEnd = null ,
97
98
staleEnd = null ;
98
99
100
+ /**
101
+ * @ngdoc type
102
+ * @name $cacheFactory.Cache
103
+ *
104
+ * @description
105
+ * A cache object used to store and retrieve data, primarily used by
106
+ * {@link $http $http} and the {@link ng.directive:script script} directive to cache
107
+ * templates and other data.
108
+ *
109
+ * ```js
110
+ * angular.module('superCache')
111
+ * .factory('superCache', ['$cacheFactory', function($cacheFactory) {
112
+ * return $cacheFactory('super-cache');
113
+ * }]);
114
+ * ```
115
+ *
116
+ * Example test:
117
+ *
118
+ * ```js
119
+ * it('should behave like a cache', inject(function(superCache) {
120
+ * superCache.put('key', 'value');
121
+ * superCache.put('another key', 'another value');
122
+ *
123
+ * expect(superCache.info()).toEqual({
124
+ * id: 'super-cache',
125
+ * size: 2
126
+ * });
127
+ *
128
+ * superCache.remove('another key');
129
+ * expect(superCache.get('another key')).toBeUndefined();
130
+ *
131
+ * superCache.removeAll();
132
+ * expect(superCache.info()).toEqual({
133
+ * id: 'super-cache',
134
+ * size: 0
135
+ * });
136
+ * }));
137
+ * ```
138
+ */
99
139
return caches [ cacheId ] = {
100
140
141
+ /**
142
+ * @ngdoc method
143
+ * @name $cacheFactory.Cache#put
144
+ * @function
145
+ *
146
+ * @description
147
+ * Inserts a named entry into the {@link $cacheFactory.Cache Cache} object to be
148
+ * retrieved later, and incrementing the size of the cache if the key was not already
149
+ * present in the cache. If behaving like an LRU cache, it will also remove stale
150
+ * entries from the set.
151
+ *
152
+ * It will not insert undefined values into the cache.
153
+ *
154
+ * @param {string } key the key under which the cached data is stored.
155
+ * @param {* } value the value to store alongside the key. If it is undefined, the key
156
+ * will not be stored.
157
+ * @returns {* } the value stored.
158
+ */
101
159
put : function ( key , value ) {
102
160
if ( capacity < Number . MAX_VALUE ) {
103
161
var lruEntry = lruHash [ key ] || ( lruHash [ key ] = { key : key } ) ;
@@ -116,7 +174,17 @@ function $CacheFactoryProvider() {
116
174
return value ;
117
175
} ,
118
176
119
-
177
+ /**
178
+ * @ngdoc method
179
+ * @name $cacheFactory.Cache#get
180
+ * @function
181
+ *
182
+ * @description
183
+ * Retrieves named data stored in the {@link $cacheFactory.Cache Cache} object.
184
+ *
185
+ * @param {string } key the key of the data to be retrieved
186
+ * @returns {* } the value stored.
187
+ */
120
188
get : function ( key ) {
121
189
if ( capacity < Number . MAX_VALUE ) {
122
190
var lruEntry = lruHash [ key ] ;
@@ -130,6 +198,16 @@ function $CacheFactoryProvider() {
130
198
} ,
131
199
132
200
201
+ /**
202
+ * @ngdoc method
203
+ * @name $cacheFactory.Cache#remove
204
+ * @function
205
+ *
206
+ * @description
207
+ * Removes an entry from the {@link $cacheFactory.Cache Cache} object.
208
+ *
209
+ * @param {string } key the key of the entry to be removed
210
+ */
133
211
remove : function ( key ) {
134
212
if ( capacity < Number . MAX_VALUE ) {
135
213
var lruEntry = lruHash [ key ] ;
@@ -148,6 +226,14 @@ function $CacheFactoryProvider() {
148
226
} ,
149
227
150
228
229
+ /**
230
+ * @ngdoc method
231
+ * @name $cacheFactory.Cache#removeAll
232
+ * @function
233
+ *
234
+ * @description
235
+ * Clears the cache object of any entries.
236
+ */
151
237
removeAll : function ( ) {
152
238
data = { } ;
153
239
size = 0 ;
@@ -156,6 +242,15 @@ function $CacheFactoryProvider() {
156
242
} ,
157
243
158
244
245
+ /**
246
+ * @ngdoc method
247
+ * @name $cacheFactory.Cache#destroy
248
+ * @function
249
+ *
250
+ * @description
251
+ * Destroys the {@link $cacheFactory.Cache Cache} object entirely,
252
+ * removing it from the {@link $cacheFactory $cacheFactory} set.
253
+ */
159
254
destroy : function ( ) {
160
255
data = null ;
161
256
stats = null ;
@@ -164,6 +259,22 @@ function $CacheFactoryProvider() {
164
259
} ,
165
260
166
261
262
+ /**
263
+ * @ngdoc method
264
+ * @name $cacheFactory.Cache#info
265
+ * @function
266
+ *
267
+ * @description
268
+ * Retrieve information regarding a particular {@link $cacheFactory.Cache Cache}.
269
+ *
270
+ * @returns {object } an object with the following properties:
271
+ * <ul>
272
+ * <li>**id**: the id of the cache instance</li>
273
+ * <li>**size**: the number of entries kept in the cache instance</li>
274
+ * <li>**...**: any additional properties from the options object when creating the
275
+ * cache.</li>
276
+ * </ul>
277
+ */
167
278
info : function ( ) {
168
279
return extend ( { } , stats , { size : size } ) ;
169
280
}
0 commit comments