2013-02-15 30 views
3

我有一個網頁緩存一些查詢字符串值爲30秒,以便它不會收到重複的值。我正在使用以下類:System.Runtime.Cache立即過期

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() + "]");   
} 
} 

這工作得很好,幾個星期後突然緩存不再保留值。 CacheRemoved回調在將項目插入緩存後立即觸發,並且我找到了刪除的原因:CacheSpecificEviction 這是在Windows Server 2008 SP1,IIS7.5與.NET 4.0上運行。在此期間沒有更改應用於操作系統或IIS。

有沒有辦法解決這個問題,如果沒有,是否有更好的緩存解決方案在網頁中使用?

預先感謝您。

+0

是否可以緩存已經達到了它的極限尺寸是多少?是否由於空間限制而刪除舊的項目? – 2013-02-15 10:27:10

+1

看到這個帖子的解釋。 http://stackoverflow.com/questions/5380875/caching-data-net-4-0-asp-net – 2013-02-15 10:42:42

回答

0

試試這個:

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

這就是我如何使用緩存:

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

不知道如何解決這個問題。正如他們所說,它有效一段時間後停止工作。 – 2013-02-15 10:46:54

+0

其實你是對的。不要以爲它會解決問題。但是,即使對於很多用戶來說,這個課程對我來說也是非常有效的。 – Maris 2013-02-15 10:48:56

+0

我有一個想法。也許你試圖用大關係鏈保存到你的緩存對象。類似於用戶 - > NxCategories - > N * MxProducts - > N * M * FxParametrs。你在緩存中持有什麼? – Maris 2013-02-15 10:51:34