topic. I'm gonna create caching values in map for some app, and there's one problem. Soooo, writing to it is no problem, I can just pass the value into channel, which will do the thing, but how do I read the value from cache? I see one simple, but may-not-be-the-best solution. It's to create mutex for cache. The other one is to do some magic with channels. I was thinking, like get method that returns channel which will produce only one value you want to get, or we can pass callback function to get method and do whatever we want with the value we get. What will you recommend me to do?
Using a shared map together with an RWMutex
“magic with channels” is always going to be slower than a mutex, and what you want is an RWMutex here instead of Mutex because caches are read-heavy > method that returns channel so for every read you want to allocate a channel? That’s what I call inefficient 🙂 My advice would be: implement an RWMutex protected map, benchmark it and then try to implement it differently and see whether it gets any faster in your case.
Обсуждают сегодня