2013-03-23 04:01:52 +06:00
|
|
|
|
using System;
|
2013-09-25 19:23:41 +10:00
|
|
|
|
using System.Collections;
|
2014-05-22 09:36:08 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-09 11:16:10 +10:00
|
|
|
|
using System.Linq;
|
2014-06-28 12:09:54 +02:00
|
|
|
|
using System.Threading;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
using System.Web.Caching;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
using CacheItemPriority = System.Web.Caching.CacheItemPriority;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A CacheProvider that wraps the logic of the HttpRuntime.Cache
|
|
|
|
|
|
/// </summary>
|
2013-12-16 16:02:34 +11:00
|
|
|
|
internal class HttpRuntimeCacheProvider : DictionaryCacheProviderBase, IRuntimeCacheProvider
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2014-06-28 12:09:54 +02:00
|
|
|
|
// locker object that supports upgradeable read locking
|
|
|
|
|
|
// does not need to support recursion if we implement the cache correctly and ensure
|
|
|
|
|
|
// that methods cannot be reentrant, ie we do NOT create values while holding a lock.
|
|
|
|
|
|
private readonly ReaderWriterLockSlim _locker = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
|
|
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
private readonly System.Web.Caching.Cache _cache;
|
2014-05-27 12:16:41 +02:00
|
|
|
|
|
2015-07-08 11:07:53 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used for debugging
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal Guid InstanceId { get; private set; }
|
|
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
public HttpRuntimeCacheProvider(System.Web.Caching.Cache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cache = cache;
|
2015-07-08 11:07:53 +02:00
|
|
|
|
InstanceId = Guid.NewGuid();
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
protected override IEnumerable<DictionaryEntry> GetDictionaryEntries()
|
2014-05-26 15:56:29 +02:00
|
|
|
|
{
|
|
|
|
|
|
const string prefix = CacheItemPrefix + "-";
|
|
|
|
|
|
return _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string && ((string) x.Key).StartsWith(prefix));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
protected override void RemoveEntry(string key)
|
2014-05-22 09:36:08 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
_cache.Remove(key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
protected override object GetEntry(string key)
|
2014-05-22 09:36:08 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
return _cache.Get(key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-28 12:09:54 +02:00
|
|
|
|
#region Lock
|
|
|
|
|
|
|
|
|
|
|
|
protected override IDisposable ReadLock
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return new ReadLock(_locker); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IDisposable WriteLock
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return new WriteLock(_locker); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
#region Get
|
|
|
|
|
|
|
2014-05-22 09:36:08 +02:00
|
|
|
|
/// <summary>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with all of the default parameters
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-08-08 20:25:26 +10:00
|
|
|
|
public override object GetCacheItem(string cacheKey, Func<object> getCacheItem)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
return GetCacheItem(cacheKey, getCacheItem, null, dependentFiles: null);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// This overload is here for legacy purposes
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="removedCallback"></param>
|
|
|
|
|
|
/// <param name="dependency"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <returns></returns>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
internal object GetCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
2014-05-16 16:48:31 +02:00
|
|
|
|
// NOTE - because we don't know what getCacheItem does, how long it will take and whether it will hang,
|
|
|
|
|
|
// getCacheItem should run OUTSIDE of the global application lock else we run into lock contention and
|
|
|
|
|
|
// nasty performance issues.
|
|
|
|
|
|
|
|
|
|
|
|
// So.... we insert a Lazy<object> in the cache while holding the global application lock, and then rely
|
|
|
|
|
|
// on the Lazy lock to ensure that getCacheItem runs once and everybody waits on it, while the global
|
|
|
|
|
|
// application lock has been released.
|
|
|
|
|
|
|
2014-06-28 12:09:54 +02:00
|
|
|
|
// NOTE
|
|
|
|
|
|
// The Lazy value creation may produce a null value.
|
|
|
|
|
|
// Must make sure (for backward compatibility) that we pretend they are not in the cache.
|
|
|
|
|
|
// So if we find an entry in the cache that already has its value created and is null,
|
|
|
|
|
|
// pretend it was not there. If value is not already created, wait... and return null, that's
|
|
|
|
|
|
// what prior code did.
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE
|
|
|
|
|
|
// The Lazy value creation may throw.
|
2014-05-23 10:44:58 +02:00
|
|
|
|
|
|
|
|
|
|
// So... the null value _will_ be in the cache but never returned
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
|
|
|
|
|
Lazy<object> result;
|
|
|
|
|
|
|
2014-06-28 12:09:54 +02:00
|
|
|
|
// Fast!
|
|
|
|
|
|
// Only one thread can enter an UpgradeableReadLock at a time, but it does not prevent other
|
|
|
|
|
|
// threads to enter a ReadLock in the meantime -- only upgrading to WriteLock will prevent all
|
|
|
|
|
|
// reads. We first try with a normal ReadLock for maximum concurrency and take the penalty of
|
|
|
|
|
|
// having to re-lock in case there's no value. Would need to benchmark to figure out whether
|
|
|
|
|
|
// it's worth it, though...
|
|
|
|
|
|
using (new ReadLock(_locker))
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
result = _cache.Get(cacheKey) as Lazy<object>; // null if key not found
|
2014-06-28 12:09:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
var value = result == null ? null : GetSafeLazyValue(result);
|
|
|
|
|
|
if (value != null) return value;
|
|
|
|
|
|
|
|
|
|
|
|
using (var lck = new UpgradeableReadLock(_locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = _cache.Get(cacheKey) as Lazy<object>; // null if key not found
|
|
|
|
|
|
|
|
|
|
|
|
// cannot create value within the lock, so if result.IsValueCreated is false, just
|
|
|
|
|
|
// do nothing here - means that if creation throws, a race condition could cause
|
|
|
|
|
|
// more than one thread to reach the return statement below and throw - accepted.
|
2013-08-09 11:16:10 +10:00
|
|
|
|
|
2014-06-28 12:09:54 +02:00
|
|
|
|
if (result == null || GetSafeLazyValue(result, true) == null) // get non-created as NonCreatedValue & exceptions as null
|
|
|
|
|
|
{
|
2015-05-19 09:16:05 +02:00
|
|
|
|
result = GetSafeLazy(getCacheItem);
|
2014-05-16 16:48:31 +02:00
|
|
|
|
var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value));
|
|
|
|
|
|
var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration);
|
2014-06-28 12:09:54 +02:00
|
|
|
|
|
|
|
|
|
|
lck.UpgradeToWriteLock();
|
2014-05-16 16:48:31 +02:00
|
|
|
|
_cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
2015-05-19 09:16:05 +02:00
|
|
|
|
// using GetSafeLazy and GetSafeLazyValue ensures that we don't cache
|
|
|
|
|
|
// exceptions (but try again and again) and silently eat them - however at
|
|
|
|
|
|
// some point we have to report them - so need to re-throw here
|
|
|
|
|
|
|
|
|
|
|
|
// this does not throw anymore
|
|
|
|
|
|
//return result.Value;
|
|
|
|
|
|
|
|
|
|
|
|
value = result.Value; // will not throw (safe lazy)
|
|
|
|
|
|
var eh = value as ExceptionHolder;
|
|
|
|
|
|
if (eh != null) throw eh.Exception; // throw once!
|
|
|
|
|
|
return value;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-09 11:16:10 +10:00
|
|
|
|
public object GetCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null)
|
2013-04-03 22:34:40 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
CacheDependency dependency = null;
|
|
|
|
|
|
if (dependentFiles != null && dependentFiles.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
dependency = new CacheDependency(dependentFiles);
|
|
|
|
|
|
}
|
|
|
|
|
|
return GetCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency);
|
2013-04-03 22:34:40 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Insert
|
|
|
|
|
|
|
2013-04-03 22:34:40 +06:00
|
|
|
|
/// <summary>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// This overload is here for legacy purposes
|
2013-04-03 22:34:40 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="removedCallback"></param>
|
|
|
|
|
|
/// <param name="dependency"></param>
|
|
|
|
|
|
internal void InsertCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2014-05-16 16:48:31 +02:00
|
|
|
|
// NOTE - here also we must insert a Lazy<object> but we can evaluate it right now
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// and make sure we don't store a null value.
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
2015-05-19 09:16:05 +02:00
|
|
|
|
var result = GetSafeLazy(getCacheItem);
|
2014-06-17 18:42:00 +02:00
|
|
|
|
var value = result.Value; // force evaluation now - this may throw if cacheItem throws, and then nothing goes into cache
|
2014-05-23 10:44:58 +02:00
|
|
|
|
if (value == null) return; // do not store null values (backward compat)
|
2013-08-09 11:16:10 +10:00
|
|
|
|
|
|
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
|
|
|
|
|
var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value));
|
|
|
|
|
|
var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration);
|
|
|
|
|
|
|
2014-06-28 12:09:54 +02:00
|
|
|
|
using (new WriteLock(_locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
_cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback);
|
|
|
|
|
|
}
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-09 11:16:10 +10:00
|
|
|
|
public void InsertCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
CacheDependency dependency = null;
|
|
|
|
|
|
if (dependentFiles != null && dependentFiles.Any())
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
dependency = new CacheDependency(dependentFiles);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2013-08-09 11:16:10 +10:00
|
|
|
|
InsertCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2014-05-27 12:16:41 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|