程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 用匿名函數來更方便的使用緩存

用匿名函數來更方便的使用緩存

編輯:關於C#

一個數據緩存,一般由數據來源,緩存方案,緩存讀取來完成,現在有了匿名函數,可以把獲取數據 來源變為匿名函數。這樣就可以直接聲明一個緩存:

protected static CacheManager<Company> companyCache = new  CacheManager<Company>(new SmsDatabaseCache<Company>(), (key) =>
              {
                  return SmsDatabase.Instance.Context.Company.Where(c  => c.ShortNumber == (string)key).SingleOrDefault();
              });

SmsDatabaseCache是一個緩存方案,還可以使用SimpleCache,TimeoutCache等實現了ICache接口的類 ,因為我的ICacheManager從ICache繼承,所以還可以實現級連緩存喲,例如:

SmsDatebaseCache是一個依SmsDatabase.Instance.Context的緩存方案,在LinqContextDispose時, 會進行清空數據操作。

SmsDatabaseCache 

    public class SmsDatabaseCache<TData> : ICache<TData>
     {
         private Dictionary<object, TData> cache = new  Dictionary<object, TData>();

         public SmsDatabaseCache()
         {
             SmsDatabase.Instance.PreDisposing += (s, e) =>
                 {
                     cache.Clear();
                 };
         }

         public TData Get(object key)
         {
             return cache[key];
         }

         public void Set(object key, TData data)
         {
             cache[key] = data;
         }

     }

實現代碼如下:

緩存代碼

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Evlon.Utils
{
     public interface ICache<TData>
     {
         TData Get(object key);
         void Set(object key, TData data);
     }

     public interface ICacheManager<TData> : ICache<TData>
     {
         Func<object, TData> SourceGetter { get; }
     }

     public class SimpleCache<TData> : ICache<TData>
     {
         private Dictionary<object, TData> cache = new  Dictionary<object, TData>();

         #region ICacheManager<TData> 成員

         public TData Get(object key)
         {
             return cache[key];
         }

         public void Set(object key, TData data)
         {
             cache[key] = data;
         }

         #endregion
     }

     public class TimeoutCache<TData> : ICache<TData>
     {
         public TimeSpan Expire {get;set;}
         private DateTime timeLastScan = DateTime.Now;
         private Dictionary<object, KeyValuePair<TData, DateTime>>  cache = new Dictionary<object, KeyValuePair<TData, DateTime>>();

         public TimeoutCache()
         {
             Expire = TimeSpan.FromMinutes(5);
         }

         private void TryClearExpireObject()
         { 
             TimeSpan ts = DateTime.Now - timeLastScan;
             if (ts.TotalSeconds > Expire.TotalSeconds / 2)
             {
                 timeLastScan = DateTime.Now;
                 cache.Where(kv => kv.Value.Value  >  DateTime.Now.Subtract(Expire)).ToList().ForEach(kv =>
                     {
                         cache.Remove(kv.Key);
                     });
             }
         }

         #region ICacheManager<TData> 成員

         public TData Get(object key)
         {
             TryClearExpireObject();

             if(!cache.ContainsKey(key))
                 return default(TData);

             KeyValuePair<TData, DateTime> kv = cache[key];
             if (kv.Value.Add(Expire) < DateTime.Now)
             {
                 cache.Remove(key);
                 return default(TData);
             }
             return kv.Key;
         }

         public void Set(object key, TData data)
         {
             cache[key] = new KeyValuePair<TData, DateTime>(data,  DateTime.Now);
         }

         #endregion
     }

     public class CacheManager<TData> : ICacheManager<TData>
     {
         private ICache<TData> cache = null;

         public Func<object, TData> SourceGetter { get; private set;  }
         public CacheManager(ICache<TData> cache, Func<object,  TData> sourceGetter)
         {
             this.cache = cache;
             this.SourceGetter = sourceGetter;
         }

         #region ICacheGetter<TData> 成員

         public TData Get(object key)
         {
             TData data = cache.Get(key);
             if (data != null)
                 return data;

             data = this.SourceGetter(key);

             cache.Set(key, data);

             return data;
         }

         public void Set(object key, TData data)
         {
             cache.Set(key, data);
         }

         #endregion
     }

}

技術交流,歡迎轉載。轉載請注明出處 http://evlon.cnblogs.com QQ:[email protected]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved