如果它存在而不是数据库,我如何缓存对象并从内存中读取?

时间:2013-02-21 10:13:19

标签: c# caching interface

我有以下四个课程:

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

现在,我想在内存中对缓存部分执行特定时间,以便从缓存中读取部分(如果存在),否则从数据库中读取部分。

3 个答案:

答案 0 :(得分:19)

简单可行的方法,您可以使用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);

    }

答案 1 :(得分:2)

通过添加具有超时的新类来执行缓存。当您第一次阅读时,直接从数据库中读取数据并将数据写入新类的属性并生成时间戳。在下一个读取操作中,检查新类以查看是否已达到超时。如果没有,则从新类中读取数据。否则,您从数据库中读取并将其放入缓存类并更新超时。

答案 2 :(得分: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);
    }
}