Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ae342b5

Browse files
caitpbtford
authored andcommitted
docs($cacheFactory): document cache instance methods
These were apparently entirely undocumented. I'm not sure if they're intended to be private, but in case they're not, I've written some initial docs for them
1 parent 562334f commit ae342b5

File tree

1 file changed

+113
-2
lines changed

1 file changed

+113
-2
lines changed

src/ng/cacheFactory.js

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* @name $cacheFactory
66
*
77
* @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.
910
*
1011
* ```js
1112
*
@@ -96,8 +97,65 @@ function $CacheFactoryProvider() {
9697
freshEnd = null,
9798
staleEnd = null;
9899

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+
*/
99139
return caches[cacheId] = {
100140

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+
*/
101159
put: function(key, value) {
102160
if (capacity < Number.MAX_VALUE) {
103161
var lruEntry = lruHash[key] || (lruHash[key] = {key: key});
@@ -116,7 +174,17 @@ function $CacheFactoryProvider() {
116174
return value;
117175
},
118176

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+
*/
120188
get: function(key) {
121189
if (capacity < Number.MAX_VALUE) {
122190
var lruEntry = lruHash[key];
@@ -130,6 +198,16 @@ function $CacheFactoryProvider() {
130198
},
131199

132200

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+
*/
133211
remove: function(key) {
134212
if (capacity < Number.MAX_VALUE) {
135213
var lruEntry = lruHash[key];
@@ -148,6 +226,14 @@ function $CacheFactoryProvider() {
148226
},
149227

150228

229+
/**
230+
* @ngdoc method
231+
* @name $cacheFactory.Cache#removeAll
232+
* @function
233+
*
234+
* @description
235+
* Clears the cache object of any entries.
236+
*/
151237
removeAll: function() {
152238
data = {};
153239
size = 0;
@@ -156,6 +242,15 @@ function $CacheFactoryProvider() {
156242
},
157243

158244

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+
*/
159254
destroy: function() {
160255
data = null;
161256
stats = null;
@@ -164,6 +259,22 @@ function $CacheFactoryProvider() {
164259
},
165260

166261

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+
*/
167278
info: function() {
168279
return extend({}, stats, {size: size});
169280
}

0 commit comments

Comments
 (0)