简体   繁体   English

维修清单 <T> 在像数据库一样的缓存中

[英]Maintaing List<T> in cache like database

I have a requirement where in i need to maintain a list in memory. 我有一个要求,我需要在内存中维护一个列表。

Eg - 例如-

list<products>

every user will run the application and add new product/update product/ remove product from the list and all the changes should be reflected on that list. 每个用户都将运行该应用程序并从列表中添加新产品/更新产品/删除产品,所有更改应反映在该列表上。

I am trying to store the list in the objectcache. 我试图将列表存储在对象缓存中。

but when i run the application it creates products but the moment i run it second time the list is not in the cache. 但是,当我运行该应用程序时,它会创建产品,但是当我第二次运行它时,该列表不在缓存中。 need a help. 需要帮助。

Following is the code - 以下是代码-

public class ProductManagement
{

    List<Productlist> _productList;
    ObjectCache cache= MemoryCache.Default;

    public int Createproduct(int id,string productname)
    {
            if (cache.Contains("Productlist"))
            {
                _productList = (List<Productlist>)cache.Get("Productlist");
            }
            else
            {
                _productList = new List<Productlist>();
            }

            Product pro = new Product();
            pro.ID = id;
            pro.ProductName = productname;

            _productList.Add(pro);

            cache.AddOrGetExisting("Productlist", _productList, DateTime.MaxValue);

            return id;
    }

    public Product GetProductbyId(int id)
    {
            if (cache.Contains("Productlist"))
            {
                _productList = (List<Productlist>)cache.Get("Productlist");
            }
            else
            {
                _productList = new List<Productlist>();
            }

            var product = _productList.Single(i => i.ID == id);
            return product;
    }

}

how i can make sure that the list is persistent even when the application is not running, can this be possible. 我怎么能确保即使应用程序未运行时该列表也是持久的,这是否有可能。 also how to keep the cache alive and to retrieve the same cache next time. 还介绍了如何使缓存保持活动状态以及下次如何检索相同的缓存。

Many thanks for help. 非常感谢您的帮助。

Memory can be different http://en.wikipedia.org/wiki/Computer_memory . 内存可以是不同的http://en.wikipedia.org/wiki/Computer_memory MemoryCache stores information in Process Memory, which exists only while Process exists. MemoryCache将信息存储在进程内存中,该内存仅在进程存在时才存在。

If your requirement is to maintain list in process memory - your task is done, and more than you do not need to use ObjectCache cache= MemoryCache.Default; 如果您的要求是维护进程内存中的列表,则您的任务已经完成,并且不需要使用ObjectCache cache= MemoryCache.Default; you can just keep the list as a field for ProductManagement . 您可以将列表保留为ProductManagement的字段。

If you need to keep the list between application launches - you need to do additional work to write the list to file when you close application and read it when you open application. 如果需要在应用程序启动之间保留列表-您需要做其他工作以在关闭应用程序时将列表写入文件,并在打开应用程序时将其读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM