"microsoft enterprise library caching application block not thread safe?" Code Answer

4

i notice that you seem to be getting null back from the cache whenever that item hasn't been accessed during the previous 5 loop iterations (ie, 5 seconds). could this be related to your 5 second expiry time?

it seems unlikely, but maybe you have a race condition and the items are dropping out of the cache between the contains check and the getdata retrieval.

try this change and see if it makes any difference to the output:

while (true)
{
    system.threading.thread.sleep(1000);

    var key = new random().next(3).tostring();
    string value;

    lock (cache)
    {
        value = (string)cache.getdata(key);
        if (value == null)
        {
            value = key;
            cache.add(key, value, cacheitempriority.normal, null,
                new slidingtime(timespan.fromseconds(5)));
        }
    }
    console.writeline("{0} --> '{1}'", key, value);
}
By daluq on May 30 2022

Answers related to “microsoft enterprise library caching application block not thread safe?”

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