2022-02-03 15:26:44 +00:00
|
|
|
namespace ASC.Collections;
|
|
|
|
|
|
|
|
public sealed class HttpRequestDictionary<T> : CachedDictionaryBase<T>
|
|
|
|
{
|
2022-02-11 13:17:55 +00:00
|
|
|
private readonly HttpContext _httpContext;
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
public HttpRequestDictionary(HttpContext httpContext, string baseKey)
|
|
|
|
{
|
|
|
|
Condition = (T) => true;
|
|
|
|
BaseKey = baseKey;
|
|
|
|
_httpContext = httpContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Reset(string rootKey, string key)
|
|
|
|
{
|
|
|
|
if (_httpContext != null)
|
|
|
|
{
|
|
|
|
var builtkey = BuildKey(key, rootKey);
|
|
|
|
_httpContext.Items[builtkey] = null;
|
2022-02-01 14:51:29 +00:00
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
public override void Add(string rootkey, string key, T newValue)
|
|
|
|
{
|
|
|
|
if (_httpContext != null)
|
|
|
|
{
|
|
|
|
var builtkey = BuildKey(key, rootkey);
|
|
|
|
_httpContext.Items[builtkey] = new CachedItem(newValue);
|
2022-02-01 14:51:29 +00:00
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
}
|
2022-02-01 14:51:29 +00:00
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
protected override object GetObjectFromCache(string fullKey)
|
|
|
|
{
|
|
|
|
return _httpContext?.Items[fullKey];
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
protected override bool FitsCondition(object cached)
|
|
|
|
{
|
|
|
|
return cached is CachedItem;
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
protected override T ReturnCached(object objectCache)
|
|
|
|
{
|
|
|
|
return ((CachedItem)objectCache).Value;
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
|
|
|
|
protected override void OnHit(string fullKey) { }
|
|
|
|
|
|
|
|
protected override void OnMiss(string fullKey) { }
|
|
|
|
|
|
|
|
protected override void InsertRootKey(string rootKey)
|
|
|
|
{
|
|
|
|
//We can't expire in HtppContext in such way
|
|
|
|
}
|
|
|
|
|
2022-02-11 13:17:55 +00:00
|
|
|
private sealed class CachedItem
|
2022-02-03 15:26:44 +00:00
|
|
|
{
|
|
|
|
internal T Value { get; set; }
|
|
|
|
|
2022-02-08 11:07:28 +00:00
|
|
|
internal CachedItem(T value)
|
|
|
|
{
|
|
|
|
Value = value;
|
|
|
|
}
|
2022-02-03 15:26:44 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|