Eddie Garmon's Weblog

some architecture, some c#

More on Syncronized - upgrade reader to writer

In using my new sync wrapper to clean up a bunch of older code, I came across the need to be able to upgrade from a reader to a writer for certain operations. One example of this it to clean up expired items in my my implementation of a Cache.

protected Synchronized<Dictionary<string, ICacheItem>> _syncDictionary =
    new Synchronized<Dictionary<string, ICacheItem>>(new Dictionary<string, ICacheItem>());
...
private void PurgeExpired() {
    _syncDictionary.ReadUpgradeableToWrite((dictionary, reader) => {
        List<ICacheItem> expired = new List<ICacheItem>();
        foreach (ICacheItem cacheItem in dictionary.Values) {
            if (cacheItem.IsExpired) {
                expired.Add(cacheItem);
            }
        }
        if (expired.Count > 0) {
            reader.UpgradeToWriter();
            foreach (ICacheItem cacheItem in expired) {
                ItemRemovedInternal(cacheItem);
                dictionary.Remove(cacheItem.CacheKey);
            }
        }
        return true;
    });
}

I cleaned up the code a bit, and am making it available here. Free to use, no waranty on fitness.

 

Comments

Christopher Steen said:

Link Collections Interesting Finds: July 11, 2003 - short edition [Via: Jason Haley ] The Morning Brew...

# July 12, 2008 10:35 AM

Christopher Steen said:

Link Listing - July 11, 2008

# July 12, 2008 10:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)