2013-02-21 3 views
7

Mam 4 zajęcia następujące:Jak mogę buforować obiekty i czytać z pamięci, jeśli istnieje zamiast bazy danych?

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

i moim kontroler mam:

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

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

Teraz chcę to zrobić: Chcę chache rozdziały poświęcone pamięci dla określonego czasu w aby odczytać sekcje z pamięci podręcznej, jeśli istnieje, lub odczytać ją z bazy danych.

+0

Który składnik działa z DB? – sll

+0

Spójrz na. http://stackoverflow.com/questions/1276569/caching-in-c-net – Jharis

Odpowiedz

17

Proste możliwe podejście, można użyć MemoryCache, kod będzie wyglądać następująco:

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

Mam błąd: Nie można niejawnie przekonwertować typu "obiekt" na System.Collections.Generic.List ". Istnieje wyraźna konwersja (czy brakuje Ci obsady?) Co mam zrobić? –

+1

@HamidReza: Powinieneś rzucić okiem podczas otrzymywania objet z pamięci podręcznej –

+0

OW.Yes.I zrobiłem to. Dziękuję. –

1

Robisz buforowanie, dodając nową klasę z limitem czasu. Kiedy czytasz po raz pierwszy czytasz bezpośrednio z bazy danych i zapisujesz dane do właściwości nowej klasy i tworzysz znacznik czasu. W następnej operacji odczytu sprawdzasz nową klasę, jeśli upłynął limit czasu. jeśli nie, odczytujesz dane z nowej klasy, gdy czytasz z bazy danych i umieszczasz ją w klasie pamięci podręcznej i aktualizujesz limit czasu.

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); 
    } 
}