|
|
@ -1,130 +1,133 @@
|
|
|
|
#region Copyright
|
|
|
|
#region Copyright
|
|
|
|
// /************************************************************************
|
|
|
|
// /************************************************************************
|
|
|
|
// Copyright (c) 2016 Jamie Rees
|
|
|
|
// Copyright (c) 2016 Jamie Rees
|
|
|
|
// File: MemoryCacheProvider.cs
|
|
|
|
// File: MemoryCacheProvider.cs
|
|
|
|
// Created By: Jamie Rees
|
|
|
|
// Created By: Jamie Rees
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
// ************************************************************************/
|
|
|
|
// ************************************************************************/
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
using System.Runtime.Caching;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PlexRequests.Helpers
|
|
|
|
namespace PlexRequests.Helpers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class MemoryCacheProvider : ICacheProvider
|
|
|
|
public class MemoryCacheProvider : ICacheProvider
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private ObjectCache Cache => MemoryCache.Default;
|
|
|
|
private ObjectCache Cache => MemoryCache.Default;
|
|
|
|
|
|
|
|
private readonly object _lock = new object();
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Gets the item from the cache, if the item is not present
|
|
|
|
/// <summary>
|
|
|
|
/// then we will get that item and store it in the cache.
|
|
|
|
/// Gets the item from the cache, if the item is not present
|
|
|
|
/// </summary>
|
|
|
|
/// then we will get that item and store it in the cache.
|
|
|
|
/// <typeparam name="T">Type to store in the cache.</typeparam>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <typeparam name="T">Type to store in the cache.</typeparam>
|
|
|
|
/// <param name="itemCallback">The item callback. This will be called if the item is not present in the cache.
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// </param>
|
|
|
|
/// <param name="itemCallback">The item callback. This will be called if the item is not present in the cache.
|
|
|
|
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
|
|
|
/// </param>
|
|
|
|
/// <returns>A copy of the cached object.</returns>
|
|
|
|
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
|
|
|
/// <remarks>If the <c><![CDATA[Func<T>]]> itemCallback</c> is null and the item is not in the cache it will throw a <see cref="NullReferenceException"/>.
|
|
|
|
/// <returns>A copy of the cached object.</returns>
|
|
|
|
/// <para>If you do not want to change the object in the cache (since it's a copy returned and not a reference) you will need to <see cref="Remove"/>
|
|
|
|
/// <remarks>If the <c><![CDATA[Func<T>]]> itemCallback</c> is null and the item is not in the cache it will throw a <see cref="NullReferenceException"/>.
|
|
|
|
/// the cached item and then <see cref="Set"/> it, or just call this method.</para></remarks>
|
|
|
|
/// <para>If you do not want to change the object in the cache (since it's a copy returned and not a reference) you will need to <see cref="Remove"/>
|
|
|
|
public T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class
|
|
|
|
/// the cached item and then <see cref="Set"/> it, or just call this method.</para></remarks>
|
|
|
|
{
|
|
|
|
public T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class
|
|
|
|
var item = Get<T>(key);
|
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
var item = Get<T>(key);
|
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
item = itemCallback();
|
|
|
|
{
|
|
|
|
if (item != null)
|
|
|
|
item = itemCallback();
|
|
|
|
{
|
|
|
|
if (item != null)
|
|
|
|
Set(key, item, cacheTime);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Set(key, item, cacheTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Return a copy, not the stored cache reference
|
|
|
|
|
|
|
|
// The cached object will not change
|
|
|
|
// Return a copy, not the stored cache reference
|
|
|
|
// If we
|
|
|
|
// The cached object will not change
|
|
|
|
return item.CloneJson();
|
|
|
|
// If we
|
|
|
|
}
|
|
|
|
return item.CloneJson();
|
|
|
|
|
|
|
|
}
|
|
|
|
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class
|
|
|
|
|
|
|
|
{
|
|
|
|
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class
|
|
|
|
var item = Get<T>(key);
|
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
var item = Get<T>(key);
|
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
item = await itemCallback();
|
|
|
|
{
|
|
|
|
if (item != null)
|
|
|
|
item = await itemCallback();
|
|
|
|
{
|
|
|
|
if (item != null)
|
|
|
|
Set(key, item, cacheTime);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Set(key, item, cacheTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// Return a copy, not the stored cache reference
|
|
|
|
|
|
|
|
// The cached object will not change
|
|
|
|
// Return a copy, not the stored cache reference
|
|
|
|
return item.CloneJson();
|
|
|
|
// The cached object will not change
|
|
|
|
}
|
|
|
|
return item.CloneJson();
|
|
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Gets the specified item from the cache.
|
|
|
|
/// <summary>
|
|
|
|
/// </summary>
|
|
|
|
/// Gets the specified item from the cache.
|
|
|
|
/// <typeparam name="T">Type to get from the cache</typeparam>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <typeparam name="T">Type to get from the cache</typeparam>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
public T Get<T>(string key) where T : class
|
|
|
|
/// <returns></returns>
|
|
|
|
{
|
|
|
|
public T Get<T>(string key) where T : class
|
|
|
|
lock (key)
|
|
|
|
{
|
|
|
|
return Cache.Get(key) as T;
|
|
|
|
lock (_lock)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
return Cache.Get(key) as T;
|
|
|
|
/// <summary>
|
|
|
|
}
|
|
|
|
/// Set/Store the specified object in the cache
|
|
|
|
}
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <summary>
|
|
|
|
/// <param name="data">The object we want to store.</param>
|
|
|
|
/// Set/Store the specified object in the cache
|
|
|
|
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
|
|
|
/// </summary>
|
|
|
|
public void Set(string key, object data, int cacheTime = 20)
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
{
|
|
|
|
/// <param name="data">The object we want to store.</param>
|
|
|
|
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) };
|
|
|
|
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
|
|
|
lock (key)
|
|
|
|
public void Set(string key, object data, int cacheTime = 20)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Cache.Remove(key);
|
|
|
|
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) };
|
|
|
|
Cache.Add(new CacheItem(key, data), policy);
|
|
|
|
lock (_lock)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Cache.Remove(key);
|
|
|
|
|
|
|
|
Cache.Add(new CacheItem(key, data), policy);
|
|
|
|
/// <summary>
|
|
|
|
}
|
|
|
|
/// Removes the specified object from the cache.
|
|
|
|
}
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
/// <summary>
|
|
|
|
public void Remove(string key)
|
|
|
|
/// Removes the specified object from the cache.
|
|
|
|
{
|
|
|
|
/// </summary>
|
|
|
|
var keys = Cache.Where(x => x.Key.Contains(key));
|
|
|
|
/// <param name="key">The key.</param>
|
|
|
|
foreach (var k in keys)
|
|
|
|
public void Remove(string key)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock (key)
|
|
|
|
var keys = Cache.Where(x => x.Key.Contains(key));
|
|
|
|
{
|
|
|
|
foreach (var k in keys)
|
|
|
|
Cache.Remove(k.Key);
|
|
|
|
{
|
|
|
|
}
|
|
|
|
lock (_lock)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
Cache.Remove(k.Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|