"is memorycache.set() thread-safe?" Code Answer


yes, the memorycache class is thread safe:

system.runtime.caching.memorycache is threadsafe. multiple concurrent threads can read and write a memorycache instance. internally thread-safety is automatically handled to ensure the cache is updated in a consistent manner.

what this might be referring to is that data stored within the cache may itself not be threadsafe. for example if a list is placed in the cache, and two separate threads both get a reference to the cached list, the two threads will end up stepping on each other if they both attempt to update the list simultaneously.

this being said the get and set methods are thread safe but if the data structure you might be storing into this cache is not thread safe you might get into trouble. imagine for example that you stored a dictionary inside this cache. then while thread1 uses get to fetch the dictionary and starts reading from it, thread2 uses get to fetch this same dictionary and tries to write to it. while the get operation will be thread safe what will happen next could be pretty nasty.

By Rafal Zajac on April 29 2022

Answers related to “is memorycache.set() thread-safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.