Browse Source

Upgrade locker

Former-commit-id: 0e2625d0d4ef02589ab13f9298e649be41dcd334
Former-commit-id: a45e1898fff0dbdf3b4620611d1276e5c038c8e6
Former-commit-id: 27f90127fab95af155788f8fdfb04fb35ba193cc
pull/17/head
James South 11 years ago
parent
commit
9d19368b41
  1. 17
      src/ImageProcessor.Web/Caching/MemCache.cs

17
src/ImageProcessor.Web/Caching/MemCache.cs

@ -14,6 +14,9 @@ namespace ImageProcessor.Web.Caching
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Threading;
using ImageProcessor.Common.Helpers;
/// <summary>
/// Encapsulates methods that allow the caching and retrieval of objects from the in memory cache.
@ -25,6 +28,11 @@ namespace ImageProcessor.Web.Caching
/// </summary>
private static readonly ObjectCache Cache = MemoryCache.Default;
/// <summary>
/// The reader-writer lock implementation.
/// </summary>
private static readonly ReaderWriterLockSlim Locker = new ReaderWriterLockSlim();
/// <summary>
/// An internal list of cache keys to allow bulk removal.
/// </summary>
@ -56,8 +64,7 @@ namespace ImageProcessor.Web.Caching
public static bool AddItem(string key, object value, CacheItemPolicy policy = null, string regionName = null)
{
bool isAdded;
lock (Cache)
using (new WriteLock(Locker))
{
if (policy == null)
{
@ -100,7 +107,7 @@ namespace ImageProcessor.Web.Caching
/// </returns>
public static object GetItem(string key, string regionName = null)
{
lock (Cache)
using (new UpgradeableReadLock(Locker))
{
return Cache.Get(key, regionName);
}
@ -173,7 +180,7 @@ namespace ImageProcessor.Web.Caching
{
bool isRemoved;
lock (Cache)
using (new WriteLock(Locker))
{
isRemoved = Cache.Remove(key, regionName) != null;
@ -200,7 +207,7 @@ namespace ImageProcessor.Web.Caching
{
bool isCleared = false;
lock (CacheItems)
using (new WriteLock(Locker))
{
// You can't remove items from a collection whilst you are iterating over it so you need to
// create a collection to store the items to remove.

Loading…
Cancel
Save