jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.57k stars 95 forks source link

cached: Add sync_writes_by_key #226

Open raimannma opened 2 months ago

raimannma commented 2 months ago

Sync Writes by Key

This PR implements the sync_writes_by_key attribute, which works like sync_writes, but uses an individual lock per key.

This way you can call a method concurrently with different cache keys without locking the method (globally).

Related Issues:

raimannma commented 2 months ago

@jaemk Can you review this? I tried to keep the changes very minimal.

The only thing I am not sure about is whether we need to worry about cleaning up the hashmap. So if a cache is invalid, we could remove the key from the hashmap.

Also I haven't tested with Redis cache storages, could there be a problem with redis?

omid commented 2 months ago

What's the use case of sync_writes then? I mean, why not to always lock by keys?

raimannma commented 2 months ago

What's the use case of sync_writes then?

Good point.

I guess there could still be a use case where you want to lock a method independently of the key. Also, using by_key is a bit more expensive because it has to keep track of multiple cache instances in the hashmap.

omid commented 2 months ago

Thanks for clarification. I'm not a maintainer, but just to give some feedbacks, I'll add my suggestions to the PR.

raimannma commented 2 months ago

Applied all the refactorings :+1:

raimannma commented 2 months ago

have to rewrite tests .....