cached icon indicating copy to clipboard operation
cached copied to clipboard

cached: Add sync_writes_by_key

Open raimannma opened this issue 1 year ago • 6 comments

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:

  • https://github.com/jaemk/cached/issues/158
  • https://github.com/jaemk/cached/issues/62
  • https://github.com/jaemk/cached/issues/81

raimannma avatar Sep 19 '24 10:09 raimannma

@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?

raimannma avatar Sep 19 '24 10:09 raimannma

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

omid avatar Sep 19 '24 12:09 omid

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.

raimannma avatar Sep 19 '24 12:09 raimannma

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

omid avatar Sep 19 '24 13:09 omid

Applied all the refactorings :+1:

raimannma avatar Sep 19 '24 14:09 raimannma

have to rewrite tests .....

raimannma avatar Sep 19 '24 14:09 raimannma

@jaemk Have you had a chance to look at this PR?

raimannma avatar Mar 02 '25 12:03 raimannma

Thanks @raimannma (and @omid for reviewing)! Released under 0.55.1 - I changed one thing though, reverting the #[once] macro's sync_writes to a boolean since there was only one variant and the key doesn't apply

jaemk avatar Mar 03 '25 03:03 jaemk

Thanks @raimannma (and @omid for reviewing)! Released under 0.55.1 - I changed one thing though, reverting the #[once] macro's sync_writes to a boolean since there was only one variant and the key doesn't apply

Great, thanks 👍

raimannma avatar Mar 03 '25 03:03 raimannma