Are Session and Cache variables thread safe?

How thread safe are the Session and Cache variables?

If I have one endpoint testing an integer variable every 50 milliseconds to see when a different endpoint changes it, can the get sometimes interfere with the set? I’m seeing some odd, intermittent behavior that fits that explanation.

Instead of a simple integer, should I use a variable type designed for multithreading, such as a ConcurrentDictionary?

Thanks,
Tim Curwick

1 Like

The Session and Cache variables use the Memory Cache C# type for storage.

This type is thread safe and you should be able to get and set values without the get interfering with the set.

There is a chance that the interaction between the PS provider API and the memory cache itself is causing some weird threading issues but I would have to dig into it to see what is going on there. If you have a simple reproduction step for this it would be very helpful.