1

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.

outcoldman
  • 11,584
  • 2
  • 26
  • 30
Ish Tech
  • 239
  • 1
  • 2
  • 12

1 Answers1

0

Memory can be different http://en.wikipedia.org/wiki/Computer_memory. MemoryCache stores information in Process Memory, which exists only while Process exists.

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; you can just keep the list as a field for 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.

outcoldman
  • 11,584
  • 2
  • 26
  • 30
  • thnx for the reply, i would like to keep the list between multiple application launches , the list would something similar to database, which will persistent for a certain time period independent of the application process working or not, is there any other alternative other than storing the list in file, something in caching – Ish Tech Apr 08 '13 at 01:14
  • If you need to keep your cache between application launches you should use file or database. – outcoldman Apr 08 '13 at 01:56
  • Hi, i tried a scenario with 2 testcase, where in 1st testcase i create the cache and add the item in to the cache and access the items from the cache and it works, and then i run the second testcase which just access the cache and search for the item which was added in the in first testcase ,but instead of getting the old cache it creates a new cache with empty list .can u please let me know wht could be the problem and is there a possiblitiy to access a single cache in each test case – Ish Tech May 05 '13 at 22:53
  • what type of cache did you use? – outcoldman May 06 '13 at 04:07
  • i am using objectcache - ObjectCache cache= MemoryCache.Default; – Ish Tech May 06 '13 at 05:21
  • Hi have created a cache singleton class - which is as follows -public class CacheSingleton { private static CacheSingleton instance; private CacheSingleton() {cache = MemoryCache.Default;} public static CacheSingleton Instance { get { if (instance == null) { instance = new CacheSingleton(); } return instance; } } public ObjectCache cache { get; private set; } } – Ish Tech May 06 '13 at 05:55
  • Unit Tests should be unrelated from each other.I don't know which Unit Test Framework you are using, but I guess that it can be the case that each run in the separate App Domain. – outcoldman May 06 '13 at 14:19
  • I am using Microsoft VSTS testing framework , and in one testcase i add a product A in the cache and in the second test case i check whether that product A is already added or not and gets its details, but in the second testcase always fails as it creates new cache each time instead using the existing cache – Ish Tech May 06 '13 at 20:18
  • This is not how people write unit tests. As I said each unit test should be unrelated to each other. http://www.manning.com/osherove/ this is good book for start to learn about TDD and unit tests. – outcoldman May 06 '13 at 21:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29507/discussion-between-mahesh-and-outcoldman) – Ish Tech May 06 '13 at 21:44