Question

I have deployed an Azure WebRole with Co-located caching. I am using the following default configuration for the clients.

<dataCacheClient name="default">
  <autoDiscover isEnabled="true" identifier="[name]" />
  <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
</dataCacheClient>

and currently I run the following code every time I access the Cache

DataCacheFactory CacheFactory = new DataCacheFactory(); _Cache = CacheFactory.GetDefaultCache();

This causes my Application Pool get terminated frequently. How DataCacheFactory and Re-use it whenever it is needed.

Thanks in advance

Était-ce utile?

La solution

I suggest that you use the ASP.NET Application State to keep the DataChache Factory object.

You can write a helper class to get the Data Cache Factory object. Something like (never tested):

public class DataCacheHelper
{
    public DataCacheHelper()
    {
        DataCacheFactory factory = new DataCacheFactory();
        HttpContext.Current.Application.Lock();
        HttpContext.Current.Application["dcf"] = factory;
        HttpContext.Current.Application.Unock();
    }

    public DataCacheFactory GetFactory()
    {
        var factory = HttpContext.Current.Application["dcf"];
        if (factory == null)
        {
            factory = new DataCacheFactory();
            HttpContext.Current.Application.Lock();
            HttpContext.Current.Application["dcf"] = factory;
            HttpContext.Current.Application.Unock();
        }
        return factory;
    }
}

Or, if you are using ASP.NET MVC - you can create a base Controller class, which has GetCacheFactory method (which exactly what the helper method do), and have all your Controllers inherit this base instead the framework one. Same could be achieved for Web Forms.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top