Feat/cache package#80
Conversation
|
nicolasmelo1
left a comment
There was a problem hiding this comment.
Remind to do a git pull, there is lots of code that i have already changed. So it's bringing up 149 files changes hahahahahhaha.
Do a git pull origin main to get the changes from the main. Besides that i'll comment out the changes on the codebase.
Noted! I think we should try keep the main branch up to date to avoid merge conflicts. On this feature I'm gonna modify only the libs, packages and the cache-example dir, nothing that would bring merge conflicts on what you're doing |
nicolasmelo1
left a comment
There was a problem hiding this comment.
Everything is great, just don't forget to do a git pull origin main. I already applied some changes but it's appearing how it was before for them. (It's appearing 149 changes hahahahahahaha)
Besides that LGTM 👌
There was a problem hiding this comment.
Remember that i said to return a function from new on CacheAdapter?
Probably you will need some modifications here. You will need to cache the callback and the adapter instance.
Think with me here
const cacheAdapterInitializer = RedisCacheAdapter.new({});
and then
cacheAdapterInitializer() to get the actual instance.
So probably you need to store this callback here. And then it's up to you. The CacheAdapter instance you can store either here, or in the Cache (on src/cache.ts right above this file) instance directly.
This will need some modifications from your Cache class as well
There was a problem hiding this comment.
Remember that i said to return a function from
newonCacheAdapter?Probably you will need some modifications here. You will need to cache the callback and the adapter instance.
Think with me here
const cacheAdapterInitializer = RedisCacheAdapter.new({});
and then
cacheAdapterInitializer() to get the actual instance.
So probably you need to store this callback here. And then it's up to you. The CacheAdapter instance you can store either here, or in the
Cache(onsrc/cache.tsright above this file) instance directly.This will need some modifications from your Cache class as well
I think I miss the point here. What changes should I do on the Cache class? Do you mean on the #getCachedAdapter method specifically? I thought the callback that will be stored on cacheAdapterInitializer will be called on the load or ready step of the lifecycle.
…ad of class/inheritance approach
baa3ea6 to
d77d606
Compare
| }) | ||
| } | ||
|
|
||
| async set(_key: string, _value: any) { |
There was a problem hiding this comment.
add the option to have a TTL defined when setting a value
There was a problem hiding this comment.
The options should been passed as parameter on the set method or should be passed on the "new" method? What pattern palmares use for this?
There was a problem hiding this comment.
This is on the example folder. Create a cache adapter on the redis folder, What do you think about already creating an adapter for Redis?
There was a problem hiding this comment.
I think it's a great idea. Where should I put this? Inside the libs folder?
| return cachedAdapter?.get(key); | ||
| } | ||
|
|
||
| async set(key: string, value: any) { |
There was a problem hiding this comment.
Support TTL. And don't forget that caching by default is only strings, so you need to do a JSON.stringify and then a JSON.parse when retrieving the data.
There was a problem hiding this comment.
I think we can make TS better on this.
Cache could receive a generic like
Cache {}
Then the user could define the types for the data it store, we can also use it on set
cache.set('key', 'my-value')
We will write set as
async set<TKey extends string, TValue extends any>(key: TKey, value: TValue) {
return this as Promise<Cache<TData & { [Key in TKey]: TValue }>>
}
So pretty much whenever you do
cache.set()
we
Will return cache but typed for the user.
cache = await cache.set('value', 1)
cache.get('value') // Promise
There was a problem hiding this comment.
For better Typing we can even go further, remove the async on set function. With that we will need to be creative.
cache = cache.set('value', 1)
This will NOT remove the async on the adapter, the adapter will STILL be async.
Something like.
class Cache<TData extends object> {
#setPromisesByKey = {};
async get<TKey extends string>(key: TKey) {
if (this.#setPromisesByKey[key]) await this.#setPromisesByKey[key];
// get the value from the adapter
}
set<TKey extends string, TValue extends any>(key: TKey, value: TValue) {
/// set from adapter
// I think you can make this better but you got the idea.
const promiseToWait = this.#getCachedAdapter().then((cachedAdapter) => {
if (cachedAdapter) cachedAdapter.set(key, value).then(() => {
delete this.#setPromisesByKey[key]
})
})
this.#setPromisesByKey[key] = promiseToWait;
return this as Promise<Cache<TData & { [Key in TKey]: TValue }>>
}
}
cache-onlyexamplecachepackage