jaemk / cached

Rust cache structures and easy function memoization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] Thread safety?

hcldan opened this issue · comments

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?

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.

commented

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.

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.