System.Runtime.Cache expires instantly

I have a web page that caches some request values ​​for 30 seconds to avoid getting duplicate values. I am using the following class:

public class MyCache   {

private static ObjectCache cache = MemoryCache.Default;

public MyCache() { }


public void Insert(string key, string value)
{

    CacheItemPolicy policy = new CacheItemPolicy();
    policy.Priority = CacheItemPriority.Default;
    policy.SlidingExpiration = new TimeSpan(0, 0, 30);
    policy.RemovedCallback = new CacheEntryRemovedCallback(this.Cacheremovedcallback);

    cache.Set(key, value, policy);
}

public bool Exists(string key)
{
    return cache.Contains(key);
}

public void Remove(string key)
{
    cache.Remove(key);
}

private void Cacheremovedcallback(CacheEntryRemovedArguments arguments)
{      
    FileLog.LogToFile("Cache item removed. Reason: " + arguments.RemovedReason.ToString() +  "; Item: [" +  arguments.CacheItem.Key + ", " + arguments.CacheItem.Value.ToString() + "]");         
}
 }

      

This works great for a few weeks and then the cache no longer stores the values. The CacheRemoved callback is triggered instantly after the item is inserted into the cache and I get the remote reason: CacheSpecificEviction This is running on Windows Server 2008 SP1, IIS7.5 with .NET 4.0. During this time, no changes were applied to the OS or IIS.

Is there a way to solve this, and if not, is there a better cache solution to use in a web page?

Thanks in advance.

+3


source to share


1 answer


Try the following:

policy.AbsoluteExpiration = DateTime.Now.AddSeconds(30);

      



This is how I use Cache:

public static class CacheHelper
{
    private static ObjectCache _cache;
    private const Double ChacheExpirationInMinutes = 10;

    /// <summary>
    /// Insert value into the cache using
    /// appropriate name/value pairs
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="entity">item cached</param>
    /// <param name="key">Name of item</param>
    public static void Add<T>(T entity, string key) where T : class
    {
        if (_cache == null)
        {
            _cache = MemoryCache.Default;
        }
        if (_cache.Contains(key))
            _cache.Remove(key);
        CacheItemPolicy cacheItemPolicy = new CacheItemPolicy();
        cacheItemPolicy.AbsoluteExpiration = DateTime.Now.AddMinutes(ChacheExpirationInMinutes);
        _cache.Set(key, entity, cacheItemPolicy);
    }

    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void Clear(string key)
    {
        if (_cache == null)
        {
            _cache = MemoryCache.Default;
            return;
        }
        _cache.Remove(key);
    }

    /// <summary>
    /// Retrieve cached item
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="key">Name of cached item</param>
    /// <returns>Cached item as type</returns>
    public static T Get<T>(string key) where T : class
    {
        if (_cache == null)
        {
            _cache = MemoryCache.Default;
        }
        try
        {
            return (T)_cache.Get(key);
        }
        catch
        {
            return null;
        }
    }
}

      

0


source







All Articles