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.