angelnikolov / ts-cacheable

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

Odd cache/busting behavior in different AWS environments #41

Closed kthomas182 closed 5 years ago

kthomas182 commented 5 years ago

I have the same application code deployed in (2) different AWS s3 buckets.

In my DEV environment I have a cache busting method when AddEmployee() is invoked. Whenever I return to my employees page it always calls GetEmployees() method with a new API call since I busted the cache.

In my QA environment I have the same cache busting method when AddEmployee() is invoked. Whenever I return to my employees page it always calls GetEmployees() method and returns the cached data, but I busted the cache with the AddEmployee() call.

This is extremely odd behavior. Have you noticed this before?

Service `@Cacheable({ cacheBusterObserver: employeeCache$.asObservable(), }) getEmployees( customerId?: number, locationId?: number, ): Observable<Employee[]> {}

@CacheBuster({ cacheBusterNotifier: employeeCache$, }) @CacheBuster({ cacheBusterNotifier: customerCacheBuster$, }) public addEmployee(employee: Employee): Observable {}`

Could it be possibly because I have (2) cacheBusterNotifier decorators?

angelnikolov commented 5 years ago

Hmm, it could be. Can't you use one cacheBuster? What is the reasoning of having two cache busters on the same method?

kthomas182 commented 5 years ago

When we add/update an employee our customer API returns a total count of employees, so we need to bust the cache or both employees and customers APIs so they don’t return stale data.

angelnikolov commented 5 years ago

Hmm.. I guess you could invert the dependncy and provide the cache buster subject from this service and import in the others, rather than importing two different subjects from there?

kthomas182 commented 5 years ago

@angelnikolov

I'm not sure what you mean by inverting the dependency.

I have it set up like this:

employee.service.ts --> employee-cache.service.ts

customer.service.ts --> customer-cache.service.ts

customer-cache.service.ts - customerCacheBuster$ import { Subject } from 'rxjs/internal/Subject'; export const customerCacheBuster$ = new Subject();

employee-cache.service.ts - employeeCache$ import { Subject } from 'rxjs/index'; export const employeeCache$ = new Subject();

angelnikolov commented 5 years ago

So you can remove both customerCacheBuster$ and employeeCache$ and just export a new subject employeCacheBuster$ *from the service** where you want to bust the cache and then import them in both customer-cache.service.ts and employee-cache.service.ts rather than exporting separate subjects from there.

angelnikolov commented 5 years ago

@kthomas182 Did it work for you?

kthomas182 commented 5 years ago

Hi @angelnikolov

We haven’t had a chance to try it yet. I’ll close this for now and we can revisit it later. Thanks for your help!