2013-02-21 26 views
7

我有4類如下:如何緩存對象並從內存中讀取,如果它存在而不是數據庫?

public class Section 
    { 
     public int SectionId { get; set; } 
     public string Name { get; set; } 
     public string Title { get; set; } 
     public string MetaTag { get; set; } 
     public string MetaDescription { get; set; } 
     public string UrlSafe { get; set; } 
     public string Header { get; set; } 
     public string ImageName { get; set; } 
    }  

public interface ISectionRepository 
    { 
     List<Section> GetAllSections(); 
    } 

public class SectionRepository : ISectionRepository 
    { 
     Context context = new Context(); 

     public List<Section> GetAllSections() 
     { 
      return context.Sections.ToList(); 
     } 
    } 

public class SectionApplication 
    { 
     SectionRepository sectionRepo = new SectionRepository(); 

     public List<Section> GetAllSections() 
     { 
      return sectionRepo.GetAllSections(); 
     } 
    } 

,在我的控制,我有:

public class SectionController : Controller 
    { 
     SectionApplication sectionApp = new SectionApplication(); 

     public ActionResult Index() 
     { 
      return View(sectionApp.GetAllSections()); 
     } 
    } 

現在,我想這樣做: 我想chache內存部分在特定時間爲了從緩存中讀取部分(如果存在),否則從數據庫讀取它。

+0

哪個組件與DB的作品? – sll 2013-02-21 10:16:49

+0

看看。 http://stackoverflow.com/questions/1276569/caching-in-c-net – Jharis 2013-02-21 10:16:58

回答

17

簡單可行的方法,你可以使用MemoryCache,代碼看起來像:

public List<Section> GetAllSections() 
    { 
     var memoryCache = MemoryCache.Default; 

     if (!memoryCache.Contains("section")) 
     { 
      var expiration = DateTimeOffset.UtcNow.AddMinutes(5); 
      var sections = context.Sections.ToList(); 

      memoryCache.Add("section", section, expiration); 
     } 

     return memoryCache.Get("section", null); 

    } 
+0

我有一個錯誤: 不能隱式地將類型'對象'轉換爲System.Collections.Generic.List 」。存在明確的轉換(您是否缺少演員?) 我該怎麼辦? – 2013-02-26 05:34:29

+1

@HamidReza:你應該在獲得objet出緩存時進行明確的轉換 – 2013-02-26 06:07:36

+0

OW.Yes.I做到了。謝謝。 – 2013-02-26 06:21:22

1

你可以通過添加一個帶有超時的新類來進行緩存。當您第一次閱讀直接從數據庫讀取數據並將數據寫入新類的屬性並製作時間戳時。在下一次閱讀操作中,如果達到超時,請檢查新班級。如果不是,則讀取數據庫中讀取的新類的數據,並將其放入緩存類並更新超時。

1
public interface IRepositoryCollection 
{ 
    DateTime dateCreated { get; set; } 
} 

public class Cache<T> : Dictionary<string, T> 
{ 
    private int cacheDuration = 1; 
    private int maxCacheSize = 20; 

    public Cache(int cacheDuration, int maxCacheSize) 
    { 
     this.cacheDuration = cacheDuration; 
     this.maxCacheSize = maxCacheSize; 
    } 

    public new void Add(string key, T invoices) 
    { 
     base.Add(key, invoices); 
     RemoveOld(); 
     RemoveOldest(); 
    } 

    public void RemoveOld() 
    { 
     foreach (KeyValuePair<string, T> cacheItem in this) 
     { 
      Interfaces.IRepositoryCollection currentvalue = (Interfaces.IRepositoryCollection)cacheItem.Value; 

      if (currentvalue.dateCreated < DateTime.Now.AddHours(-cacheDuration)) 
      { 
       this.Remove(cacheItem.Key); 
      } 
     } 
    } 

    public void RemoveOldest() 
    { 
     do 
     { 
      this.Remove(this.First().Key); 
     } 
     while (this.Count > maxCacheSize); 
    } 
} 


public class ProformaInvoiceCache 
{ 
    private static Cache<ProformaInvoices> cache = new Cache<ProformaInvoices>(1, 20); 

    public static string AddInvoiceCollection(ProformaInvoices invoices) 
    { 
     // Adds invoice collection to static instance of cache, returns guid required to retrieve item from cache 
     return cache.Add(invoices); 
    } 

    public static ProformaInvoices GetInvoiceCollection(string guid) 
    { 
     // Gets invoice collection from cache corresponding to passed guid 
     return cache.Get(guid); 
    } 
} 
相關問題