How can I cache objects and read from memory if they exist instead of a database?

I have 4 classes:

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

And in my controller, I have:

  public class SectionController : Controller { SectionApplication sectionApp = new SectionApplication(); public ActionResult Index() { return View(sectionApp.GetAllSections()); } } 

Now I want to do this: I want to read partitions in memory for a certain time, to read partitions from the cache, if it exists, and also read it from the database.

+7
source share
3 answers

A simple approach possible, you can use MemoryCache , the code will look like this:

 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); } 
+17
source

You perform caching by adding a new class with a timeout. When you read the first time, you read directly from the database and write data to a property of the new class and create a timestamp. In the next read operation, you check your new class if the timeout is reached. if not, you are reading data from the new class, otherwise you are reading from the database and putting it in the cache class and updating the timeout.

+1
source
 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); } } 
+1
source

All Articles