ksysoev / wasabi

Toolkit for Creating WebSocket API Gateways
MIT License
4 stars 2 forks source link

Caching middleware #37

Closed ksysoev closed 1 week ago

ksysoev commented 2 months ago

It could be common feature request to cache read queries for many apis. we should design and implement middleware that will be caching responses from BE and sending them back to the client

shan-96 commented 1 month ago

So are you saying the this will be a server side cache?

ksysoev commented 1 month ago

@shan-96 yes the idea to have some thing similar to rate limiter middleware, https://github.com/ksysoev/wasabi/blob/main/middleware/request/ratelimiter.go#L17... to accept cache key generator from the user and only focus on caching logic. For now we can keep implementations simple and use some in memory cache, try to find some lru cache with ttl support. for handling concurrent request to the same key we can use https://pkg.go.dev/golang.org/x/sync/singleflight

ksysoev commented 1 month ago

also for catching api response we can use connection wrapper https://github.com/ksysoev/wasabi/blob/main/channel/connection_wrapper.go

shan-96 commented 4 weeks ago

I'm adding some proposals here. Let me know if a document with details are needed

Caching lib prospects

image

image

image

I personally favor remember-go just because it is aiming to provide a caching interface rather than a caching library. However I also would like to keep things simple and use the most simple cache and write custom interfaces for users to implement themselves. We can also stick to single implementation rather than providing options and go with ristretto.

Would like to know what direction you want to go with

ksysoev commented 4 weeks ago

@shan-96 ristretto looks good, we can start with it, as a next steps we can provide option to provide customer cache storage that satisfy the interface.

ksysoev commented 1 week ago

@shan-96 do you have any concern if i takeover this issue?

shan-96 commented 1 week ago

@ksysoev go ahead

ksysoev commented 1 week ago

Implemented in #97, I decided to go with https://github.com/jellydator/ttlcache as it looks way simpler and looks enough for current needs.

shan-96 commented 1 week ago

ah yes ttlcache is a good alternative