Clearing the cache with a named thread

ian r
2009-05-20
2013-04-03
  • ian r
    ian r
    2009-05-20

    Hi,

    I usually use
    CacheManager.Clear(Thread.CurrentThread.GetHashCode());
    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
    CacheManager.Clear(int);
    .. 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;
    CacheManager.Clear(threadName);

    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  1.2.9.1286)

     
    • Morten Mertner
      Morten Mertner
      2009-06-02

      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 :)

      Morten