Note that there are some explanatory texts on larger screens.

plurals
  1. POMicrosoft Enterprise Library Caching Application Block not thread safe?
    primarykey
    data
    text
    <p>I created a super simple console app to test out the Enterprise Library Caching Application Block, and the behavior is baffling. I'm hoping I screwed something that's easy to fix in the setup. I have each item expire after 5 seconds for testing purposes.</p> <p>Basic setup -- "Every second pick a number between 0 and 2. If the cache doesn't already have it, put it in there -- otherwise just grab it from the cache. Do this inside a LOCK statement to ensure thread safety.</p> <p>APP.CONFIG: </p> <pre><code>&lt;configuration&gt; &lt;configSections&gt; &lt;section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /&gt; &lt;/configSections&gt; &lt;cachingConfiguration defaultCacheManager="Cache Manager"&gt; &lt;cacheManagers&gt; &lt;add expirationPollFrequencyInSeconds="1" maximumElementsInCacheBeforeScavenging="1000" numberToRemoveWhenScavenging="10" backingStoreName="Null Storage" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Cache Manager" /&gt; &lt;/cacheManagers&gt; &lt;backingStores&gt; &lt;add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Null Storage" /&gt; &lt;/backingStores&gt; &lt;/cachingConfiguration&gt; &lt;/configuration&gt; </code></pre> <p>C#:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.EnterpriseLibrary.Common; using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations; namespace ConsoleApplication1 { class Program { public static ICacheManager cache = CacheFactory.GetCacheManager("Cache Manager"); static void Main(string[] args) { while (true) { System.Threading.Thread.Sleep(1000); // sleep for one second. var key = new Random().Next(3).ToString(); string value; lock (cache) { if (!cache.Contains(key)) { cache.Add(key, key, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromSeconds(5))); } value = (string)cache.GetData(key); } Console.WriteLine("{0} --&gt; '{1}'", key, value); //if (null == value) throw new Exception(); } } } } </code></pre> <p>OUTPUT -- How can I prevent the cache from returning nulls?</p> <pre><code>2 --&gt; '2' 1 --&gt; '1' 2 --&gt; '2' 0 --&gt; '0' 2 --&gt; '2' 0 --&gt; '0' 1 --&gt; '' 0 --&gt; '0' 1 --&gt; '1' 2 --&gt; '' 0 --&gt; '0' 2 --&gt; '2' 0 --&gt; '0' 1 --&gt; '' 2 --&gt; '2' 1 --&gt; '1' Press any key to continue . . . </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload