2013-08-12 15:06:12 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A cache provider that caches items in the HttpContext.Items
|
|
|
|
|
|
/// </summary>
|
2013-12-16 16:21:38 +11:00
|
|
|
|
internal class HttpRequestCacheProvider : DictionaryCacheProviderBase
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
|
|
|
|
|
private readonly Func<HttpContextBase> _context;
|
|
|
|
|
|
|
|
|
|
|
|
public HttpRequestCacheProvider(HttpContext context)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
// create wrapper only once!
|
|
|
|
|
|
var wrapper = new HttpContextWrapper(context);
|
|
|
|
|
|
_context = () => wrapper;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public HttpRequestCacheProvider(Func<HttpContextBase> context)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context = context;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
protected override IEnumerable<DictionaryEntry> GetDictionaryEntries()
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
const string prefix = CacheItemPrefix + "-";
|
|
|
|
|
|
return _context().Items.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string && ((string)x.Key).StartsWith(prefix));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void RemoveEntry(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
_context().Items.Remove(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override object GetEntry(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return _context().Items[key];
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
#region Get
|
|
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
public override object GetCacheItem(string cacheKey, Func<object> getCacheItem)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
|
|
|
|
|
Lazy<object> result;
|
|
|
|
|
|
|
|
|
|
|
|
using (var lck = new UpgradeableReadLock(Locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = _context().Items[cacheKey] as Lazy<object>; // null if key not found
|
2014-06-17 18:42:00 +02:00
|
|
|
|
if (result == null || (result.IsValueCreated && GetSafeLazyValue(result) == null)) // get exceptions as null
|
2014-05-27 12:16:41 +02:00
|
|
|
|
{
|
|
|
|
|
|
lck.UpgradeToWriteLock();
|
|
|
|
|
|
|
|
|
|
|
|
result = new Lazy<object>(getCacheItem);
|
|
|
|
|
|
_context().Items[cacheKey] = result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-17 18:42:00 +02:00
|
|
|
|
// this may throw if getCacheItem throws, but this is the only place where
|
|
|
|
|
|
// it would throw as everywhere else we use GetLazySaveValue() to hide exceptions
|
|
|
|
|
|
// and pretend exceptions were never inserted into cache to begin with.
|
2014-05-27 12:16:41 +02:00
|
|
|
|
return result.Value;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
2014-05-27 12:16:41 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Insert
|
|
|
|
|
|
#endregion
|
2013-08-12 15:06:12 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
2013-08-08 19:46:58 +10:00
|
|
|
|
}
|