jaemk / cached

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

[Question] Thread safety? #204

Closed hcldan closed 7 months ago

hcldan commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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.