An implementation of cache at Angular2 (4.0.0-rc.5 compatible).
Description
The cache service supports the following types of caching:
ZONE based on NgZone (the analogue of Java ThreadLocal) and the MemoryGlobalCache.
The NgZoneGlobalCache service and @ZoneCached decorator are accessible for use.
The zone cache is cleared after the "Zone" area will have finished its work.
MEMORY based on JavaScript Map.
The MemoryGlobalCache service and @MemoryCached decorator are accessible for use.
The memory cache is cleared after F5.
STORAGE based on the Window.sessionStorage (in progress)
SESSION based on the Window.sessionStorage (in progress)
FILE based on the chrome.fileSystem (in progress)
INSTANCE based (in progress)
Also, the zoneCachedDate, memoryCachedDate cached date pipes are accessible now for use.
Installation
First you need to install the npm module:
npm install angular2-cache --save
Use
main.ts
We should integrate the cache providers at first.
import{CacheModule}from'angular2-cache/index';
@NgModule({
bootstrap:[ApplicationComponent],
imports:[
CacheModule,
...
],
...
})
exportclassApplicationModule{
constructor(...){
}
}
app.ts
Then you should inject the appropriate the cache service (NgZoneGlobalCache, MemoryGlobalCache, etc..). The each cache
service has the public methods for setting configuration (setEnableLogging, setEnable or setCachedValue for setting the not lazy presets values).
privateid:string;// Identifier of the service ("cloud-1", "cloud-2", ...)
privateexpiration:string;// Expiration date of the service ("Sun Jul 30 2017 03:00:00 GMT+0300 (Russia TZ 2 Standard Time)", ...)
...
@ZoneCached()
publicgetExpirationDate():Date{
returnthis.expiration
?newDate(this.expiration)
:null;
}
publicisExpired():boolean{
returnthis.getExpirationDate()!==null// The first invoke - the code of <getExpirationDate> is executed
&&this.getExpirationDate()>newDate('12/12/2019');// The second invoke - the code of <getExpirationDate> is NOT executed, and the result is taken from the cache
}
/**
* @override
*/
publictoString():string{
// It's very important to override the toString() if cached method has no input arguments because the engine
// uses the global cache key for identifying the result of "getExpirationDate()" for the each service instance
returnCacheKeyBuilder.make()
.appendObjectName(this)// Don't pass the "this" parameter to "append" method into "toString" code section!
.append(this.getId())
.build();// The composite key: entity type + entity Id
returnthis.getExpirationDateByProduct(product)!==null// The first invoke - the code of <getExpirationDate> is executed
&&this.getExpirationDateByProduct(product)>newDate('12/12/2019');// The second invoke - the code of <getExpirationDate> is NOT executed, and the result is taken from the cache
}
}
exportclassProduct{
privateid:string;// Identifier of the service ("product-1", "product-2", ...)
/**
* @override
*/
publictoString():string{
// It's very important to override the toString() because the engine uses the global cache key for
// identifying the product instance
returnCacheKeyBuilder.make()
.appendObjectName(this)// Don't pass the "this" parameter to "append" method into "toString" code section!
.append(this.getId())
.build();// The composite key: entity type + entity Id