none
Best Practice to Serialize MemoryCache to Disk? RRS feed

  • General discussion

  • I have a WinForms app that needs to get data from a feed limited to 15 minutes for the same query.

    MemoryCache seems the best option, however, during debugging and demos I will need to restart the system which clears the cache.  Unless I am missing something I gather I will need to serialize the cache to disk.

    But MemoryCache does not support Serialization.  I figure I will need to iterate through the cache and serialize each item pair separately.  I plan to use XMLSerialization because I am looking for maintenance and debugging over performance.

    I do not wish to use 3rd party caching for this project.

    Am I following the best practice on this use case?  Anything to watch out for?  Any suggestions?

    Thanks, Paul


    RTX NET Developer Network http://rtxdn.inscriber.com/

    Tuesday, August 14, 2012 5:42 PM

All replies

  • If you're getting data from a feed, why bother with serialization?  Why not just re-fetch from teh feed on startup, so you're guaranteed to have fresh data?

    Otherwise, yes - there is no built-in mechanism to serialize the memory cache, so you'd need to write it to disk (or a DB, etc).  This would require handling the process yourself.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, August 14, 2012 5:56 PM
    Moderator
  • I neglected to mention that the feed data feed changes while the app is running so I will want to get it when I can without exceeding the 15 min limitation.

    So it sounds like I am following best practice?


    RTX NET Developer Network http://rtxdn.inscriber.com/

    Tuesday, August 14, 2012 6:05 PM
  • I neglected to mention that the feed data feed changes while the app is running so I will want to get it when I can without exceeding the 15 min limitation.

    So it sounds like I am following best practice?


    RTX NET Developer Network http://rtxdn.inscriber.com/

    Most likely yes.  Is there a reason you're using MemoryCache, and not just keeping the feed data in a custom memory structure?  MemoryCache is really intended for fairly specialized scenarios - it might be easier to just keep the feed data in your own data structure directly...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, August 14, 2012 6:16 PM
    Moderator
  • I already have the data structure for the feed.  MemoryCache allows me to keep it as is without polluting it with caching information.  Plus then I have to write and test the code to query caching info – stuff that I get for free with MemoryCache.

    Are you saying that my use of MemoryCache is not a Best Practice?


    RTX NET Developer Network http://rtxdn.inscriber.com/

    Wednesday, August 15, 2012 12:43 PM
  • Well, I'm just saying that, if you're (completely) replacing the data with a new data set on a regular interval, the caching doesn't really give you anything except added complexity...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, August 15, 2012 3:33 PM
    Moderator
  • Except from the original post "feed limited to 15 minutes for the same query"

    So I will be make hundreds of queries to the feed but I cannot make the same query within 15 minutes - easy implementation with MemoryCache.

    Only problem is the restart during debugging which clears the cache.  I have gone ahead and serialized the items in the cache and that works well without much code complexity. 

    But looking at the code I am thinking the best practice for such serialization is to extend the runtime cache class.


    RTX NET Developer Network http://rtxdn.inscriber.com/

    Thursday, August 16, 2012 1:26 PM
  • Hi Paul And  Reed,

    I think both of you make much sense. And thanks for Reed for contributing to this forum.

    @Paul,

    I have changed the type of this thread to Discussion since different people can give different "best practice".

    Thank you for your understanding and support.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 17, 2012 3:26 AM
    Moderator