©
This document uses PHP Chinese website manual Release
Factory that constructs Cache objects and gives access to them.
var cache = $cacheFactory('cacheId');
expect($cacheFactory.get('cacheId')).toBe(cache);
expect($cacheFactory.get('noSuchCacheId')).not.toBeDefined();
cache.put("key", "value");
cache.put("another key", "another value");
// We've specified no options on creation
expect(cache.info()).toEqual({id: 'cacheId', size: 2});
$cacheFactory(cacheId, [options]);
参数 | 类型 | 详述 |
---|---|---|
cacheId | string | Name or id of the newly created cache. |
options
(可选)
|
Object |
Options object that specifies the cache behavior. Properties:
|
Object |
Newly created cache object with the following set of methods:
|
info();
Get information about all the caches that have been created
Object |
|
get(cacheId);
Get access to a cache object by the cacheId
used when it was created.
参数 | 类型 | 详述 |
---|---|---|
cacheId | string | Name or id of a cache to access. |
Object | Cache object identified by the cacheId or undefined if no such cache. |
<div ng-controller="CacheController">
<input ng-model="newCacheKey" placeholder="Key">
<input ng-model="newCacheValue" placeholder="Value">
<button ng-click="put(newCacheKey, newCacheValue)">Cache</button>
<p ng-if="keys.length">Cached Values</p>
<div ng-repeat="key in keys">
<span ng-bind="key"></span>
<span>: </span>
<b ng-bind="cache.get(key)"></b>
</div>
<p>Cache Info</p>
<div ng-repeat="(key, value) in cache.info()">
<span ng-bind="key"></span>
<span>: </span>
<b ng-bind="value"></b>
</div></div>
angular.module('cacheExampleApp', []).
controller('CacheController', ['$scope', '$cacheFactory', Function($scope, $cacheFactory) {
$scope.keys = [];
$scope.cache = $cacheFactory('cacheId');
$scope.put = Function(key, value) {
$scope.cache.put(key, value);
$scope.keys.push(key);
};
}]);
p {
margin: 10px 0 3px;}