@ngx-toolkit/cache
Angular Cache implementation for Browser & Server platforms.
Table of contents:
Installation
Install the npm package.
npm install @ngx-toolkit/cache --save
yarn add @ngx-toolkit/cache
Registered CacheModule
in the root Module of your application with forRoot(caches: Cache[])
static method.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CacheModule, MemoryCache } from '@ngx-toolkit/cache';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, CacheModule.forRoot([
new MemoryCache('myMemoryCache')
]) ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Annotations
CacheDefaults
@CacheDefaults(cacheName: string)
CacheResult
@CacheResult(params?: { cacheName?: string })
CachePut
@CachePut(params?: {cacheName?: string, afterInvocation: boolean = true})
CacheKey
@CacheKey()
CacheValue
@CacheValue()
CacheRemove
@CacheRemove(params?: {cacheName?: string, afterInvocation: boolean = true})
CacheRemoveAll
@CacheRemoveAll(params?: {cacheName?: string, afterInvocation: boolean = true})
Example:
@CacheDefaults('myCacheBean')
class CacheBean {
@CachePut()
myMethod(@CacheKey() id: number, @CacheValue() value: any): void {
...
}
@CacheResult()
get(id: number): any {
...
}
@CacheRemove()
refresh(id: number): void {
...
}
@CacheRemoveAll()
refreshAll(): void {
...
}
}
Cache
MemoryCache
Cache in memory
import { MemoryCache } from '@ngx-toolkit/cache';
new MemoryCache('myCacheName');
StorageCache
Cache in a storage
import { StorageCache } from '@ngx-toolkit/cache';
new StorageCache('myCacheName', window.sessionStorage || window.localStorage);
NoOpCache
No cache, do nothing...
import { NoOpCache } from '@ngx-toolkit/cache';
new NoOpCache('myCacheName');
Custom implementation
You can create your own implementation. You simply need to implement the Cache
interface:
export interface Cache {
readonly name: string;
get<T>(key: string): T;
put<T>(key: string, value: T): void;
evict(key: string): void;
clear(): void;
}
License
© 2018 Dewizz
MIT