10

我有以下四个课程:

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

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

4

3 回答 3

20

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

    }
于 2013-02-21T10:24:01.883 回答
2

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

于 2013-02-21T10:18:29.180 回答
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);
    }
}
于 2015-03-05T18:06:42.873 回答