Closed gdgagan696 closed 4 years ago
Yes, use DOMStorageStrategy. Find more about it in the readme.
The default storage strategy is InMemoryStorageStrategy
which stores everything in-memory through a closure.
Typescript decorators are static and they can't refer anything from the instance. That said, you need to take our your cache buster from the class and put it somewhere outside of it.
Foor example const cacheBuster$ = new Subject()
;
Hy Thanks. It worked. I got some more issues. I agree with this.The default storage strategy is InMemoryStorageStrategy which stores everything in-memory through a closure.
Also, DOMStorageStrategy worked for me.Thanks for that.
For now in updateUser(id:number) method,I am deleting data from cache,It will be much better if somehow I am able to update data in cache.That will increase performance more.
@Cacheable({
maxCacheCount: 2,
cacheBusterObserver: cacheBuster$.asObservable()
})
getUser(id:number){
return this.httpClient.getgetUser/${id}
));
}
@CacheBuster({
cacheBusterNotifier: cacheBuster$
})
updateUser(id:number){
return this.httpClient.put(this.URL.concat(updateUser/${id}
),{});
}
InMemoryStorageStrategy
.
For the DOMStorageStrategy
, you will normally find it under the CACHE_STORAGE
localStorage key.
*Can I update cache data if it get changed ? Like in case of redis cache on server side, I am able to update cache data if user tries to update data.
Check this answer out. It should answer your question :)Thanks @angelnikolov .
Hi,
Is there anyway to persist data even after page refresh?
Where does this cache stores data as I couldn't find it in browser cache?
I have worked this way but its not working.
export class UserService {
cacheBuster$ = new Subject();
URL="http://localhost:8030/users/";
constructor(private httpClient:HttpClient) { }
@Cacheable({ cacheBusterObserver: this.cacheBuster$.asObservable() }) getUser(id:number){ return this.httpClient.get(this.URL.concat(
getUser/${id}
)).pipe(tap(data=>{ this.cacheBuster$.next(data); })); } @CacheBuster({ cacheBusterNotifier: this.cacheBuster$s }) updateUser(id:number){ return this.httpClient.put(this.URL.concat(updateUser/${id}
),{}); }