added TTL to cached objects

This commit is contained in:
Keivan Beigi 2013-07-23 17:35:35 -07:00
commit 121f3b973d
11 changed files with 160 additions and 141 deletions

View file

@ -8,7 +8,6 @@ namespace NzbDrone.Common.Cache
{
ICached<T> GetCache<T>(Type host, string name);
ICached<T> GetCache<T>(Type host);
//ICollection<ICached<T>> Caches<T> { get;}
void Clear();
ICollection<ICached> Caches { get; }
}

View file

@ -1,83 +1,99 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.EnsureThat;
namespace NzbDrone.Common.Cache
{
public class Cached<T> : ICached<T>
{
private readonly ConcurrentDictionary<string, T> _store;
private class CacheItem
{
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;
}
}
private readonly ConcurrentDictionary<string, CacheItem> _store;
public Cached()
{
_store = new ConcurrentDictionary<string, T>();
_store = new ConcurrentDictionary<string, CacheItem>();
}
public void Set(string key, T value)
public void Set(string key, T value, TimeSpan? lifetime = null)
{
Ensure.That(() => key).IsNotNullOrWhiteSpace();
_store[key] = value;
}
public T Get(string key)
{
return Get(key, () => { throw new KeyNotFoundException(key); });
_store[key] = new CacheItem(value, lifetime);
}
public T Find(string key)
{
T value;
CacheItem value;
_store.TryGetValue(key, out value);
return value;
if (value == null)
{
return default(T);
}
if (value.IsExpired())
{
_store.TryRemove(key, out value);
return default(T);
}
return value.Object;
}
public T Get(string key, Func<T> function)
public T Get(string key, Func<T> function, TimeSpan? lifeTime = null)
{
Ensure.That(() => key).IsNotNullOrWhiteSpace();
CacheItem cacheItem;
T value;
if (!_store.TryGetValue(key, out value))
if (!_store.TryGetValue(key, out cacheItem) || cacheItem.IsExpired())
{
value = function();
Set(key, value);
Set(key, value, lifeTime);
}
else
{
value = cacheItem.Object;
}
return value;
}
public bool ContainsKey(string key)
{
Ensure.That(() => key).IsNotNullOrWhiteSpace();
return _store.ContainsKey(key);
}
public void Clear()
{
_store.Clear();
}
public void Remove(string key)
{
Ensure.That(() => key).IsNotNullOrWhiteSpace();
T value;
_store.TryRemove(key, out value);
}
public ICollection<T> Values
{
get
{
return _store.Values;
}
}
public ICollection<string> Keys
{
get
{
return _store.Keys;
return _store.Values.Select(c => c.Object).ToList();
}
}
}
}

View file

@ -5,19 +5,15 @@ namespace NzbDrone.Common.Cache
{
public interface ICached
{
bool ContainsKey(string key);
void Clear();
void Remove(string key);
}
public interface ICached<T> : ICached
{
void Set(string key, T value);
T Get(string key, Func<T> function);
T Get(string key);
void Set(string key, T value, TimeSpan? lifetime = null);
T Get(string key, Func<T> function, TimeSpan? lifeTime = null);
T Find(string key);
ICollection<T> Values { get; }
ICollection<string> Keys { get; }
}
}