angelnikolov / ts-cacheable

Observable/Promise Cache Decorator
https://npmjs.com/package/ts-cacheable
ISC License
340 stars 42 forks source link

Where does this cache stores data?Is there any way to persist data even after refresh? #82

Closed gdgagan696 closed 4 years ago

gdgagan696 commented 4 years ago

Hi,

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}),{}); }

angelnikolov commented 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();

gdgagan696 commented 4 years ago

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.get(this.URL.concat(getUser/${id})); } @CacheBuster({ cacheBusterNotifier: cacheBuster$ }) updateUser(id:number){ return this.httpClient.put(this.URL.concat(updateUser/${id}),{}); }

angelnikolov commented 4 years ago
gdgagan696 commented 4 years ago

Thanks @angelnikolov .