none
Add strings to list and expiry each item in certain period of time RRS feed

  • Question

  • Hi sir,

    please let me know the best approach for the below requirements.

    i am trying to add string items to the list, duplicate keys are not allowed.

    i wanted to  set the expiry time for each item. When time elapse key should remove from list. 

    please help me to achieve above requirements.

    Thanks, 

    Ravi


    • Edited by RaviRaju Friday, July 6, 2018 12:27 PM
    Friday, July 6, 2018 12:22 PM

Answers

  • If you cannot stick with the cache as suggested, you can create your own solution with the following structure:
    1. Create a class called "MyData" holds two properties. The first one would be the data as string type, and the other one is for the expiry date as DateTime data type.

    2. Create a class which would have List<MyData> called DataManager.

    3. Inside the "DataManager" class, add public method which accepts one parameter of the type MyData class. Make sure this method validates for the duplicate as well.

    4. Create an object of the Timer class with interval of 100 milliseconds for example to loop on all the items inside the DataManager class and to validate for the expiry date then to delete them if they are expired.

    Does it make sense?
    You can just implement this solution in less than an hour.

    Good luck!

    • Marked as answer by RaviRaju Tuesday, August 6, 2019 1:34 PM
    Monday, July 9, 2018 4:49 PM

All replies

  • I think a cache would be the best option here as it can expire items automatically. Take a look at MemoryCache if you are fine with it being in process.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, July 6, 2018 2:12 PM
    Moderator
  • Thank you so much for your response.

    Here i am using .net framework 3.5.  MemoryCache is under .net 4.0 framework.

    please help in alternate approach.

    Friday, July 6, 2018 3:29 PM
  • There are a couple of options to stick with the caching approach (which I recommend).

    1) Use System.Web.Caching.Cache instead. This was the original .NET caching class for web apps. While you'll take a dependency on System.Web it should work in non-web apps as well. I don't know that I've ever verified it properly expires items in the cache in a non-web app but that should be easy to test.

    2) Go to the source code for MemoryCache and copy out the subset of functionality that you need. There is a lot of code there related to stats and whatnot but for simply storing data and expiring then it is not too bad.

    3) Use one of the free third-party libraries that provide caching support.

    3) Write your own. To do this you'd need to create a wrapper around a dictionary that stores objects based upon key values. This dictionary would need to be thread safe. The data it stores would need to be a custom object that contains the core cache data like lifetime, priority, the actual data, last accessed, etc. Depending upon how little or much you want flexibility you'd need to implement priority, sliding vs absolute expiration, memory constraints, etc.

    To ensure the cache cleans up you'd also need to create a timer that periodically runs and removes any items that have expired. During all this work it has to be thread safe so multiple threads won't collide. You'll also want to be very careful about locking things for too long because it'll stall anyone using the cache.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, July 6, 2018 3:59 PM
    Moderator
  • Hi RaviRaju,

    Here is a document about how to Add Items to the Cache for your reference.

    https://msdn.microsoft.com/en-us/library/18c1wd61.aspx

    You could also write a custom static variable which like below to manage your list.

    public static class Class1
        {
            public static List<string> MyCache { get; set; }
    
            public static void AddMyCache(string test)
            {
                if (!MyCache.Contains(test))
                {
                    MyCache.Add(test);
                }
            }
    
            public static void ClearCache()
            {
                MyCache.Clear();
            }     
        }

    and create a new thread to execute ClearCache method a period of time.

    while (true)
    {
          Class1.ClearCache();
          Thread.Sleep(1000000); // a period of time.
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 9, 2018 2:27 AM
    Moderator
  • If you cannot stick with the cache as suggested, you can create your own solution with the following structure:
    1. Create a class called "MyData" holds two properties. The first one would be the data as string type, and the other one is for the expiry date as DateTime data type.

    2. Create a class which would have List<MyData> called DataManager.

    3. Inside the "DataManager" class, add public method which accepts one parameter of the type MyData class. Make sure this method validates for the duplicate as well.

    4. Create an object of the Timer class with interval of 100 milliseconds for example to loop on all the items inside the DataManager class and to validate for the expiry date then to delete them if they are expired.

    Does it make sense?
    You can just implement this solution in less than an hour.

    Good luck!

    • Marked as answer by RaviRaju Tuesday, August 6, 2019 1:34 PM
    Monday, July 9, 2018 4:49 PM