mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 05:53:33 -07:00
added new in memory cache.
This commit is contained in:
parent
d1eec340e0
commit
098036d49a
7 changed files with 202 additions and 0 deletions
55
NzbDrone.Common/Cache/Cached.cs
Normal file
55
NzbDrone.Common/Cache/Cached.cs
Normal file
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using NzbDrone.Common.EnsureThat;
|
||||
|
||||
namespace NzbDrone.Common.Cache
|
||||
{
|
||||
public class Cached<T> : ICached<T>
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, T> _store;
|
||||
|
||||
public Cached()
|
||||
{
|
||||
_store = new ConcurrentDictionary<string, T>();
|
||||
}
|
||||
|
||||
public void Set(string key, T value)
|
||||
{
|
||||
Ensure.That(() => key).IsNotNullOrWhiteSpace();
|
||||
_store.TryAdd(key, value);
|
||||
}
|
||||
|
||||
public T Get(string key, Func<T> function)
|
||||
{
|
||||
Ensure.That(() => key).IsNotNullOrWhiteSpace();
|
||||
|
||||
T value;
|
||||
|
||||
if (!_store.TryGetValue(key, out value))
|
||||
{
|
||||
value = function();
|
||||
Set(key, value);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue