2013-05-01 01:11:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-07-24 00:35:35 +00:00
|
|
|
|
using System.Linq;
|
2013-05-01 01:11:00 +00:00
|
|
|
|
using NzbDrone.Common.EnsureThat;
|
2020-06-03 22:11:45 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2013-05-01 01:11:00 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Cache
|
|
|
|
|
{
|
2013-07-24 00:35:35 +00:00
|
|
|
|
|
2013-05-01 01:11:00 +00:00
|
|
|
|
public class Cached<T> : ICached<T>
|
|
|
|
|
{
|
2013-07-24 00:35:35 +00:00
|
|
|
|
private class CacheItem
|
2013-05-01 01:11:00 +00:00
|
|
|
|
{
|
2013-07-24 00:35:35 +00:00
|
|
|
|
public T Object { get; private set; }
|
|
|
|
|
public DateTime? ExpiryTime { get; private set; }
|
|
|
|
|
|
|
|
|
|
public CacheItem(T obj, TimeSpan? lifetime = null)
|
|
|
|
|
{
|
|
|
|
|
Object = obj;
|
|
|
|
|
if (lifetime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
ExpiryTime = DateTime.UtcNow + lifetime.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsExpired()
|
|
|
|
|
{
|
|
|
|
|
return ExpiryTime.HasValue && ExpiryTime.Value < DateTime.UtcNow;
|
|
|
|
|
}
|
2013-05-01 01:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, CacheItem> _store;
|
2020-06-03 22:11:45 +00:00
|
|
|
|
private readonly TimeSpan? _defaultLifeTime;
|
|
|
|
|
private readonly bool _rollingExpiry;
|
2013-07-24 00:35:35 +00:00
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
public Cached(TimeSpan? defaultLifeTime = null, bool rollingExpiry = false)
|
2013-05-01 01:11:00 +00:00
|
|
|
|
{
|
2013-07-24 00:35:35 +00:00
|
|
|
|
_store = new ConcurrentDictionary<string, CacheItem>();
|
2020-06-03 22:11:45 +00:00
|
|
|
|
_defaultLifeTime = defaultLifeTime;
|
|
|
|
|
_rollingExpiry = rollingExpiry;
|
2013-05-23 05:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
public void Set(string key, T value, TimeSpan? lifeTime = null)
|
2013-05-23 05:12:01 +00:00
|
|
|
|
{
|
2013-11-30 23:53:07 +00:00
|
|
|
|
Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
|
2020-06-03 22:11:45 +00:00
|
|
|
|
_store[key] = new CacheItem(value, lifeTime ?? _defaultLifeTime);
|
2013-05-01 01:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 04:55:23 +00:00
|
|
|
|
public T Find(string key)
|
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
CacheItem cacheItem;
|
|
|
|
|
if (!_store.TryGetValue(key, out cacheItem))
|
2013-07-24 00:35:35 +00:00
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
if (cacheItem.IsExpired())
|
2013-07-24 00:35:35 +00:00
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
if (TryRemove(key, cacheItem))
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_store.TryGetValue(key, out cacheItem))
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_rollingExpiry && _defaultLifeTime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_store.TryUpdate(key, new CacheItem(cacheItem.Object, _defaultLifeTime.Value), cacheItem);
|
2013-07-24 00:35:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
return cacheItem.Object;
|
2013-05-30 04:55:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 22:14:44 +00:00
|
|
|
|
public void Remove(string key)
|
2013-08-30 17:27:17 +00:00
|
|
|
|
{
|
|
|
|
|
CacheItem value;
|
|
|
|
|
_store.TryRemove(key, out value);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
public int Count => _store.Count;
|
2013-12-01 01:45:30 +00:00
|
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
|
public T Get(string key, Func<T> function, TimeSpan? lifeTime = null)
|
2013-05-01 01:11:00 +00:00
|
|
|
|
{
|
2013-11-30 23:53:07 +00:00
|
|
|
|
Ensure.That(key, () => key).IsNotNullOrWhiteSpace();
|
2013-05-01 01:11:00 +00:00
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
lifeTime = lifeTime ?? _defaultLifeTime;
|
|
|
|
|
|
2013-07-24 00:35:35 +00:00
|
|
|
|
CacheItem cacheItem;
|
2013-05-01 01:11:00 +00:00
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
if (_store.TryGetValue(key, out cacheItem) && !cacheItem.IsExpired())
|
2013-05-01 01:11:00 +00:00
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
if (_rollingExpiry && lifeTime.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_store.TryUpdate(key, new CacheItem(cacheItem.Object, lifeTime), cacheItem);
|
|
|
|
|
}
|
2013-07-24 00:35:35 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
var newCacheItem = new CacheItem(function(), lifeTime);
|
|
|
|
|
if (cacheItem != null && _store.TryUpdate(key, newCacheItem, cacheItem))
|
|
|
|
|
{
|
|
|
|
|
cacheItem = newCacheItem;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cacheItem = _store.GetOrAdd(key, newCacheItem);
|
|
|
|
|
}
|
2013-05-01 01:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
return cacheItem.Object;
|
2013-05-01 01:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_store.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 00:30:09 +00:00
|
|
|
|
public void ClearExpired()
|
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
var collection = (ICollection<KeyValuePair<string, CacheItem>>)_store;
|
|
|
|
|
|
|
|
|
|
foreach (var cached in _store.Where(c => c.Value.IsExpired()).ToList())
|
2015-01-16 00:30:09 +00:00
|
|
|
|
{
|
2020-06-03 22:11:45 +00:00
|
|
|
|
collection.Remove(cached);
|
2015-01-16 00:30:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 23:32:50 +00:00
|
|
|
|
public ICollection<T> Values
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-07-24 00:35:35 +00:00
|
|
|
|
return _store.Values.Select(c => c.Object).ToList();
|
2013-05-30 23:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-24 00:35:35 +00:00
|
|
|
|
|
2020-06-03 22:11:45 +00:00
|
|
|
|
private bool TryRemove(string key, CacheItem value)
|
|
|
|
|
{
|
|
|
|
|
var collection = (ICollection<KeyValuePair<string, CacheItem>>)_store;
|
|
|
|
|
|
|
|
|
|
return collection.Remove(new KeyValuePair<string, CacheItem>(key, value));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-01 01:11:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|