0.1.1 • Published 4 years ago
angular-cacheable v0.1.1
AngularCacheable
An angular library for cacheing your method responses using decorators Compatible with Angular 9+
Installation
Install it with npm
npm install angular-cacheable --save
Migration from v0.0.4 to v0.1.1
Breaking changes in v0.1.1
Remove all
key
paramters fromCacheable
. Exmaple:@Cacheable({ key: 'posts' })
=>@Cacheable()
Default TTL is now 1 hour.
Usage example
- Add a
Cacheable
decorator to your method that returns anObservable
.
import { Cacheable } from 'angular-cacheable';
// ...
/** Basic usage */
@Cacheable()
public getCategories() {
return this.httpService.get('my-url');
}
/** Specifing a TTL: 1 second */
@Cacheable({ ttl: 1000 })
public getCategories() {
return this.httpService.get('my-url');
}
/** Custom key */
@Cacheable({ key: 'categories-list' })
public getCategories() {
return this.httpService.get('my-url');
}
/** Custom function key */
@Cacheable({ key: (args: any[]) => {
return 'categories-page-' + args[0]; // args[0] is the first method argument. Here it is `page`.
} })
public getCategories(page: number, other: string) {
return this.httpService.get('my-url', { page, other });
}
To invalidate cache call
CacheableService.getInstance().invalidate( yourCacheKey );
Decorator parameters
Function Name | Description |
---|---|
key | A key for cache (must be unique) or a function that |
returns a key. Defaults to method name concatenated | |
with hashed arguments. | |
ttl | TTL in milliseconds, default 1 hour |
License
MIT © Evgeny Gushchin