Reputation: 811
Is there any tool for custom cache control in angular 2? Local storage?
Went through docs, didn't find any references.
As a temporary solution may use jQuery, but than what the point to use angular?
Regards
Upvotes: 1
Views: 3939
Reputation: 907
I agree with @micronyks and believe that @ngrx/store is a must-have package while developing an Angular app - for state management.
However, I believe there's a more suitable package for cache management: the @ngx-cache/core which contains the Cached
method decorator and caching methods (has
, get
, set
) using the caching API.
The following example shows the use of API methods:
anyclass.ts
...
import { CacheService } from '@ngx-cache/core';
@Injectable()
export class AnyClass {
constructor(private readonly cache: CacheService) {
// note that CacheService is injected into a private property of AnyClass
}
// will retrieve 'some string value'
getSomeStringValue(): string {
if (this.cache.has('some-string'))
return this.cache.get('some-string');
this.cache.set('some-string', 'some string value');
return 'some string value';
}
}
And this example shows the use of Cached
method decorator, and CacheKey
params decorator:
anyclass.ts
...
import { Cached, CacheKey } from '@ngx-cache/core';
export class AnyClass {
// will retrieve 'some string value'
@Cached('some-string')
getSomeStringValue(): string {
return 'some string value';
}
@Cached('some-string')
getSomeStringValue2(@CacheKey param1: string): string {
return 'some string value: ' + param1;
}
}
...
// these are the first executions
console.log(anyClass.getSomeStringValue2('p1'));
console.log(anyClass.getSomeStringValue2('p2'));
...
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p1'));
// will retrieve 'some string value: p1' from `CACHE`
console.log(anyClass.getSomeStringValue2('p2'));
Here are the list of packages, both for client-side and server-side caching:
Upvotes: 1
Reputation: 55443
You can also take a look into this. This is kinda state management provider for Angular2. I don't know if its suitable for your requirement atm,
Upvotes: 0