mirror of https://github.com/SixLabors/ImageSharp
14 changed files with 420 additions and 428 deletions
@ -0,0 +1,231 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="CacheManager.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Encapsulates methods that allow the caching and retrieval of objects.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Caching |
|||
{ |
|||
#region Using
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Runtime.Caching; |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Encapsulates methods that allow the caching and retrieval of objects.
|
|||
/// </summary>
|
|||
public static class CacheManager |
|||
{ |
|||
#region Fields
|
|||
/// <summary>
|
|||
/// The cache
|
|||
/// </summary>
|
|||
private static readonly ObjectCache Cache = System.Runtime.Caching.MemoryCache.Default; |
|||
|
|||
/// <summary>
|
|||
/// An internal list of cache keys to allow bulk removal.
|
|||
/// </summary>
|
|||
private static readonly Dictionary<string, string> CacheItems = new Dictionary<string, string>(); |
|||
#endregion
|
|||
|
|||
#region Methods
|
|||
/// <summary>
|
|||
/// Adds an item to the cache.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// A unique identifier for the cache entry.
|
|||
/// </param>
|
|||
/// <param name="value">
|
|||
/// The object to insert.
|
|||
/// </param>
|
|||
/// <param name="policy">
|
|||
/// Optional. An <see cref="T:System.Runtime.Caching.CacheItemPolicy"/> object that contains eviction details for the cache entry. This object
|
|||
/// provides more options for eviction than a simple absolute expiration. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <param name="regionName">
|
|||
/// Optional. A named region in the cache to which the cache entry can be added,
|
|||
/// if regions are implemented. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// True if the insertion try succeeds, or false if there is an already an entry
|
|||
/// in the cache with the same key as key.
|
|||
/// </returns>
|
|||
public static bool AddItem(string key, object value, CacheItemPolicy policy = null, string regionName = null) |
|||
{ |
|||
bool isAdded; |
|||
|
|||
lock (Cache) |
|||
{ |
|||
if (policy == null) |
|||
{ |
|||
// Create a new cache policy with the default values
|
|||
policy = new CacheItemPolicy(); |
|||
} |
|||
|
|||
isAdded = Cache.Add(key, value, policy, regionName); |
|||
|
|||
if (isAdded) |
|||
{ |
|||
CacheItems.Add(key, regionName); |
|||
} |
|||
} |
|||
|
|||
return isAdded; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Fetches an item matching the given key from the cache.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// A unique identifier for the cache entry.
|
|||
/// </param>
|
|||
/// <param name="regionName">
|
|||
/// Optional. A named region in the cache to which the cache entry can be added,
|
|||
/// if regions are implemented. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The cache entry that is identified by key.
|
|||
/// </returns>
|
|||
public static object GetItem(string key, string regionName = null) |
|||
{ |
|||
return Cache.Get(key, regionName); |
|||
} |
|||
|
|||
//public static bool
|
|||
|
|||
/// <summary>
|
|||
/// Updates an item to the cache.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// A unique identifier for the cache entry.
|
|||
/// </param>
|
|||
/// <param name="value">
|
|||
/// The object to insert.
|
|||
/// </param>
|
|||
/// <param name="policy">
|
|||
/// Optional. An <see cref="T:System.Runtime.Caching.CacheItemPolicy"/> object that contains eviction details for the cache entry. This object
|
|||
/// provides more options for eviction than a simple absolute expiration. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <param name="regionName">
|
|||
/// Optional. A named region in the cache to which the cache entry can be added,
|
|||
/// if regions are implemented. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// True if the update try succeeds, or false if there is an already an entry
|
|||
/// in the cache with the same key as key.
|
|||
/// </returns>
|
|||
public static bool UpdateItem(string key, object value, CacheItemPolicy policy = null, string regionName = null) |
|||
{ |
|||
bool isUpDated = true; |
|||
|
|||
// Remove the item from the cache if it already exists. MemoryCache will
|
|||
// not add an item with an existing name.
|
|||
if (GetItem(key, regionName) != null) |
|||
{ |
|||
isUpDated = RemoveItem(key, regionName); |
|||
} |
|||
|
|||
if (policy == null) |
|||
{ |
|||
// Create a new cache policy with the default values
|
|||
policy = new CacheItemPolicy(); |
|||
} |
|||
|
|||
if (isUpDated) |
|||
{ |
|||
isUpDated = AddItem(key, value, policy, regionName); |
|||
} |
|||
|
|||
return isUpDated; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes an item matching the given key from the cache.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// A unique identifier for the cache entry.
|
|||
/// </param>
|
|||
/// <param name="regionName">
|
|||
/// Optional. A named region in the cache to which the cache entry can be added,
|
|||
/// if regions are implemented. The default value for the optional parameter
|
|||
/// is null.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// True if the removal try succeeds, or false if there is an already an entry
|
|||
/// in the cache with the same key as key.
|
|||
/// </returns>
|
|||
public static bool RemoveItem(string key, string regionName = null) |
|||
{ |
|||
bool isRemoved; |
|||
|
|||
lock (Cache) |
|||
{ |
|||
isRemoved = Cache.Remove(key, regionName) != null; |
|||
|
|||
if (isRemoved) |
|||
{ |
|||
CacheItems.Remove(key); |
|||
} |
|||
} |
|||
|
|||
return isRemoved; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Clears the cache.
|
|||
/// </summary>
|
|||
/// <param name="regionName">
|
|||
/// The region name.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// The <see cref="bool"/>.
|
|||
/// </returns>
|
|||
public static bool Clear(string regionName = null) |
|||
{ |
|||
bool isCleared = false; |
|||
|
|||
lock (CacheItems) |
|||
{ |
|||
// 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.
|
|||
Dictionary<string, string> tempDictionary = new Dictionary<string, string>(); |
|||
|
|||
foreach (KeyValuePair<string, string> cacheItem in CacheItems) |
|||
{ |
|||
// Does the cached key come with a region.
|
|||
if ((cacheItem.Value == null) || (cacheItem.Value != null && cacheItem.Value.Equals(regionName, StringComparison.OrdinalIgnoreCase))) |
|||
{ |
|||
isCleared = RemoveItem(cacheItem.Key, cacheItem.Value); |
|||
|
|||
if (isCleared) |
|||
{ |
|||
tempDictionary.Add(cacheItem.Key, cacheItem.Value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (isCleared) |
|||
{ |
|||
// Loop through and clear out the dictionary of cache keys.
|
|||
foreach (KeyValuePair<string, string> cacheItem in tempDictionary) |
|||
{ |
|||
CacheItems.Remove(cacheItem.Key); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return isCleared; |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
// <copyright file="CleanupImage.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// <summary>
|
|||
// Describes a cached image for cleanup.
|
|||
// </summary>
|
|||
// --------------------------------------------------------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Caching |
|||
{ |
|||
#region Using
|
|||
using System; |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Describes a cached image for cleanup
|
|||
/// </summary>
|
|||
public sealed class CleanupImage |
|||
{ |
|||
/// <summary>
|
|||
/// Gets or sets the value of the cached image.
|
|||
/// </summary>
|
|||
public string Path { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets when the cached image should expire from the cache.
|
|||
/// </summary>
|
|||
public DateTime ExpiresUtc { get; set; } |
|||
} |
|||
} |
|||
@ -1,321 +0,0 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="LockedDictionary.cs" company="James South">
|
|||
// Copyright (c) James South.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace ImageProcessor.Web.Helpers |
|||
{ |
|||
#region Using
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
#endregion
|
|||
|
|||
/// <summary>
|
|||
/// Represents a collection of keys and values that are thread safe.
|
|||
/// </summary>
|
|||
/// <typeparam name="TKey">
|
|||
/// The type of the keys in the dictionary.
|
|||
/// </typeparam>
|
|||
/// <typeparam name="TVal">
|
|||
/// The type of the values in the dictionary.
|
|||
/// </typeparam>
|
|||
internal class LockedDictionary<TKey, TVal> : IDictionary<TKey, TVal> |
|||
{ |
|||
/// <summary>
|
|||
/// The _inner.
|
|||
/// </summary>
|
|||
private readonly Dictionary<TKey, TVal> innerDictionary = new Dictionary<TKey, TVal>(); |
|||
|
|||
#region Constructors
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="LockedDictionary{TKey,TVal}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="val">
|
|||
/// The value to initialize the LockedDictionary with.
|
|||
/// </param>
|
|||
public LockedDictionary(IEnumerable<KeyValuePair<TKey, TVal>> val = null) |
|||
{ |
|||
if (val != null) |
|||
{ |
|||
this.innerDictionary = val.ToDictionary(x => x.Key, x => x.Value); |
|||
} |
|||
} |
|||
#endregion
|
|||
|
|||
#region Properties
|
|||
/// <summary>
|
|||
/// Gets a collection containing the keys in the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </summary>
|
|||
public ICollection<TKey> Keys |
|||
{ |
|||
get |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.Keys.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a collection containing the values in the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </summary>
|
|||
public ICollection<TVal> Values |
|||
{ |
|||
get |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.Values.ToArray(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the number of key/value pairs contained in the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </summary>
|
|||
public int Count |
|||
{ |
|||
get |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.Count; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether the <see cref="LockedDictionary{TKey,TVal}"/> is read only.
|
|||
/// </summary>
|
|||
public bool IsReadOnly |
|||
{ |
|||
get { return false; } |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets or sets the value associated with the specified key.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// The key of the value to get or set.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// TThe value associated with the specified key. If the specified key is not found,
|
|||
/// a get operation throws a <see cref="T:System.Collections.Generic.KeyNotFoundException"/> ,
|
|||
/// and a set operation creates a new element with the specified key.
|
|||
/// </returns>
|
|||
public TVal this[TKey key] |
|||
{ |
|||
get |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary[key]; |
|||
} |
|||
} |
|||
|
|||
set |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
this.innerDictionary[key] = value; |
|||
} |
|||
} |
|||
} |
|||
#endregion
|
|||
|
|||
#region Methods
|
|||
/// <summary>
|
|||
/// Adds the specified key and value to the dictionary.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// The key of the element to add.
|
|||
/// </param>
|
|||
/// <param name="value">
|
|||
/// The value of the element to add. The value can be null for reference types.
|
|||
/// </param>
|
|||
public void Add(TKey key, TVal value) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
this.innerDictionary.Add(key, value); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Determines whether the LockedDictionary contains the specified key.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// The key to locate in the LockedDictionary.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// true if the LockedDictionary contains the key; otherwise, false.
|
|||
/// </returns>
|
|||
public bool ContainsKey(TKey key) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.ContainsKey(key); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes the value with the specified key from the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// The key of the element to remove.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// true if the element is successfully found and removed; otherwise, false.
|
|||
/// This method returns false if key is not found in the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </returns>
|
|||
public bool Remove(TKey key) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.Remove(key); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the value associated with the specified key.
|
|||
/// </summary>
|
|||
/// <param name="key">
|
|||
/// The key of the value to get.
|
|||
/// </param>
|
|||
/// <param name="value">
|
|||
/// When this method returns, contains the value associated with the specified key, if the key is found;
|
|||
/// otherwise, the default value for the type of the value parameter. This parameter is passed uninitialized.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// true if the <see cref="LockedDictionary{TKey,TVal}"/> contains an element with
|
|||
/// the specified key; otherwise, false.
|
|||
/// </returns>
|
|||
public bool TryGetValue(TKey key, out TVal value) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.TryGetValue(key, out value); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Adds the specified key and value to the dictionary.
|
|||
/// </summary>
|
|||
/// <param name="item">
|
|||
/// The <see cref="System.Collections.Generic.KeyValuePair{TKey, TVal}"/> representing
|
|||
/// the item to add.
|
|||
/// </param>
|
|||
public void Add(KeyValuePair<TKey, TVal> item) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
this.innerDictionary.Add(item.Key, item.Value); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes all keys and values from the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </summary>
|
|||
public void Clear() |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
this.innerDictionary.Clear(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Determines whether the <see cref="LockedDictionary{TKey,TVal}"/> contains the specified key.
|
|||
/// </summary>
|
|||
/// <param name="item">
|
|||
/// The <see cref="System.Collections.Generic.KeyValuePair{TKey, TVal}"/> representing
|
|||
/// the item to locate.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// true if the <see cref="LockedDictionary{TKey,TVal}"/> contains an element
|
|||
/// with the specified key; otherwise, false.
|
|||
/// </returns>
|
|||
public bool Contains(KeyValuePair<TKey, TVal> item) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
var inner = this.innerDictionary as IDictionary<TKey, TVal>; |
|||
return inner.Contains(item); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Copies the elements of an <see cref="LockedDictionary{TKey,TVal}"/> to a one-dimensional
|
|||
/// <see cref="T:System.Array"/> starting at a particular <see cref="T:System.Array"/> index.
|
|||
/// </summary>
|
|||
/// <param name="array">
|
|||
/// The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied
|
|||
/// from <see cref="LockedDictionary{TKey,TVal}"/>.KeyCollection.
|
|||
/// The <see cref="T:System.Array"/> must have zero-based indexing.
|
|||
/// </param>
|
|||
/// <param name="arrayIndex">
|
|||
/// The zero-based index in array at which copying begins.
|
|||
/// </param>
|
|||
public void CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
var inner = this.innerDictionary as IDictionary<TKey, TVal>; |
|||
inner.CopyTo(array, arrayIndex); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Removes the item with the specified <see cref="System.Collections.Generic.KeyValuePair{TKey, TVal}"/>
|
|||
/// from the <see cref="LockedDictionary{TKey,TVal}"/>
|
|||
/// </summary>
|
|||
/// <param name="item">
|
|||
/// The <see cref="System.Collections.Generic.KeyValuePair{TKey, TVal}"/>representing the item to remove.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// This method returns false if item is not found in the <see cref="LockedDictionary{TKey,TVal}"/>.
|
|||
/// </returns>
|
|||
public bool Remove(KeyValuePair<TKey, TVal> item) |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
var inner = this.innerDictionary as IDictionary<TKey, TVal>; |
|||
return inner.Remove(item); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an enumerator that iterates through the <see cref="LockedDictionary{TKey,TVal}"/>.KeyCollection.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// A <see cref="System.Collections.Generic.Dictionary{TKey,TValue}.KeyCollection.Enumerator"/>
|
|||
/// for the <see cref="System.Collections.Generic.Dictionary{TKey,TValue}.KeyCollection"/>.
|
|||
/// </returns>
|
|||
public IEnumerator<KeyValuePair<TKey, TVal>> GetEnumerator() |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.ToList().GetEnumerator(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns an enumerator that iterates through the <see cref="LockedDictionary{TKey,TVal}"/>.KeyCollection.
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// A <see cref="System.Collections.Generic.Dictionary{TKey,TValue}.Enumerator"/>
|
|||
/// for the <see cref="System.Collections.Generic.Dictionary{TKey,TValue}"/>.
|
|||
/// </returns>
|
|||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|||
{ |
|||
lock (this.innerDictionary) |
|||
{ |
|||
return this.innerDictionary.ToArray().GetEnumerator(); |
|||
} |
|||
} |
|||
#endregion
|
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
<StyleCopSettings Version="105"> |
|||
<Analyzers> |
|||
<Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules"> |
|||
<AnalyzerSettings> |
|||
<StringProperty Name="CompanyName">James South</StringProperty> |
|||
<StringProperty Name="Copyright">Copyright (c) James South. |
|||
Licensed under the Apache License, Version 2.0.</StringProperty> |
|||
</AnalyzerSettings> |
|||
</Analyzer> |
|||
</Analyzers> |
|||
</StyleCopSettings> |
|||
Loading…
Reference in new issue