Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:09:24+00:00 2026-05-26T16:09:24+00:00

I use a System.Runtime.Caching.MemoryCache to hold items which never expire. However, at times I

  • 0

I use a System.Runtime.Caching.MemoryCache to hold items which never expire. However, at times I need the ability to clear the entire cache. How do I do that?

I asked a similar question here concerning whether I could enumerate the cache, but that is a bad idea as it needs to be synchronised during enumeration.

I’ve tried using .Trim(100) but that doesn’t work at all.

I’ve tried getting a list of all the keys via Linq, but then I’m back where I started because evicting items one-by-one can easily lead to race conditions.

I thought to store all the keys, and then issue a .Remove(key) for each one, but there is an implied race condition there too, so I’d need to lock access to the list of keys, and things get messy again.

I then thought that I should be able to call .Dispose() on the entire cache, but I’m not sure if this is the best approach, due to the way it’s implemented.

Using ChangeMonitors is not an option for my design, and is unnecassarily complex for such a trivial requirement.

So, how do I completely clear the cache?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:09:25+00:00Added an answer on May 26, 2026 at 4:09 pm

    You should not call dispose on the Default member of the MemoryCache if you want to be able to use it anymore:

    The state of the cache is set to indicate that the cache is disposed.
    Any attempt to call public caching methods that change the state of
    the cache, such as methods that add, remove, or retrieve cache
    entries, might cause unexpected behavior. For example, if you call the
    Set method after the cache is disposed, a no-op error occurs. If you
    attempt to retrieve items from the cache, the Get method will always
    return Nothing.
    http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.dispose.aspx

    About the Trim, it’s supposed to work:

    The Trim property first removes entries that have exceeded either an absolute or sliding expiration. Any callbacks that are registered
    for items that are removed will be passed a removed reason of Expired.

    If removing expired entries is insufficient to reach the specified trim percentage, additional entries will be removed from the cache
    based on a least-recently used (LRU) algorithm until the requested
    trim percentage is reached.

    But two other users reported it doesnt work on same page so I guess you are stuck with Remove() http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.trim.aspx

    Update
    However I see no mention of it being singleton or otherwise unsafe to have multiple instances so you should be able to overwrite your reference.

    But if you need to free the memory from the Default instance you will have to clear it manually or destroy it permanently via dispose (rendering it unusable).

    Based on your question you could make your own singleton-imposing class returning a Memorycache you may internally dispose at will.. Being the nature of a cache 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making use of the new .NET 4.0 Caching namespace: System.Runtime.Caching . Now, i'm
I need to use system-specific functions, e.g. ftello() (defined in stdio.h as per POSIX
I'm trying to use System.Runtime.InteropServices.ComTypes.IStream from C#, but I'm having some trouble. According to
I have a TCP Client,which puts a packet in a structure using System.Runtime.InteropServices; [StructLayoutAttribute(LayoutKind.Sequential)]
I'm trying to use a System.Dynamic.ExpandoObject so I can dynamically create properties at runtime.
I am wondering if anyone knows if I can use System.Runtime.Serialization.Json (4.0) in monodroid.
When testing runtime, I use System.nanotime() in the following way: startTime = System.nanotime(); //
I'd prefer to use Microsofts System.Runtime.Serialization.Json.DataContractJsonSerializer to serialize my objects in JSON so I
I use System.Web.Services.WebMethodAttribute to make a public static method of an ASP.NET page callable
Is it possible to use system.currency. It says system.currency is inaccessible due to its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.