Clearing the cache with a named thread

ian r
  • ian r
    ian r


    I usually use
    to clear the cache for the current thread.

    Recently I noticed that this was no longer working inside NUnit tests - I think this co-incided with NUnit changing their threading system

    I notice in the comments it says that
    .. will not work if the Thread has a name - this turns out to be the case for NUnit

    in which case should I use this instead:
    string threadName = Thread.CurrentThread.Name;

    Does that method (Clear( scopeDelimiter )) just clear the specified thread? Or does it do something else?

    I don't want to clear anything more than the specified thread - that could be disastrous

    (NB: I am using <UniqingScope>Thread</UniqingScope>, obviously, and Gentle

    • Morten Mertner
      Morten Mertner

      Although it's been some time since I wrote this, the caching code seems to have been written with named threads in mind. That is, if the thread has a name then that is actually used as the scopeDelimiter in preference of the threads id (you can verify this in the SystemSettings.ThreadIdentity property getter, which is called from the CacheManager.GetScopeDelimiter method).

      As such, calling CacheManager.Clear( Thread.CurrentThread.Name ) should work fine. However, calling CacheManager.Clear( SystemSettings.ThreadIdentity ) would work even when the thread has not been assigned a name.

      Hope this helps :)