Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Thread safety? #204

Closed
hcldan opened this issue Apr 16, 2024 · 3 comments
Closed

[Question] Thread safety? #204

hcldan opened this issue Apr 16, 2024 · 3 comments

Comments

@hcldan
Copy link

hcldan commented Apr 16, 2024

Are caches like cached::stores::TimedCache thread safe on their own or should I wrap them in a RwLock?

What about if I use the cache macro on a function? Is that function thread-safe?

@jaemk
Copy link
Owner

jaemk commented Apr 17, 2024

The in-memory cache stores have no synchronization mechanisms themselves so they need to be wrapped in your choice of mutex/rwlock if you are using them directly. If you are using them via the macros, then the macros automatically wrap them in the most appropriate synchronization type and the resulting functions are thread-safe.

@squadgazzz
Copy link

squadgazzz commented Apr 19, 2024

Is it possible to use the TimedCache with an acquired read lock from the RwLock? get_cache requires a mutable self reference, which means a read lock can't be used here. Any workaround for that? Currently, I am not really able to use the cache in an async environment since all the threads get blocked even on the read operation, which is inefficient because I have to use Mutex.

@hcldan hcldan closed this as completed Apr 22, 2024
@hcldan
Copy link
Author

hcldan commented Apr 22, 2024

Thank you @jaemk

@squadgazzz I think I ran into the same problem. The ref must be mutable probably because evictions are processed during read operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants