mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-25 01:02:05 +00:00
added new in memory cache.
This commit is contained in:
parent
d1eec340e0
commit
098036d49a
7 changed files with 202 additions and 0 deletions
74
NzbDrone.Common.Test/CacheTests/CachedFixture.cs
Normal file
74
NzbDrone.Common.Test/CacheTests/CachedFixture.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Cache;
|
||||
|
||||
namespace NzbDrone.Common.Test.CacheTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CachedFixture
|
||||
{
|
||||
private Cached<string> _cachedString = new Cached<string>();
|
||||
private Worker _worker;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_cachedString = new Cached<string>();
|
||||
_worker = new Worker();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_call_function_once()
|
||||
{
|
||||
_cachedString.Get("Test", _worker.GetString);
|
||||
_cachedString.Get("Test", _worker.GetString);
|
||||
|
||||
_worker.HitCount.Should().Be(1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
[Test]
|
||||
public void multiple_calls_should_return_same_result()
|
||||
{
|
||||
var first = _cachedString.Get("Test", _worker.GetString);
|
||||
var second = _cachedString.Get("Test", _worker.GetString);
|
||||
|
||||
first.Should().Be(second);
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void should_remove_value_from_set()
|
||||
{
|
||||
_cachedString.Get("Test", _worker.GetString);
|
||||
|
||||
_cachedString.Remove("Test");
|
||||
|
||||
_cachedString.Get("Test", _worker.GetString);
|
||||
|
||||
|
||||
_worker.HitCount.Should().Be(2);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void remove_none_existing_should_break_things()
|
||||
{
|
||||
_cachedString.Remove("Test");
|
||||
}
|
||||
}
|
||||
|
||||
public class Worker
|
||||
{
|
||||
public int HitCount { get; private set; }
|
||||
|
||||
public string GetString()
|
||||
{
|
||||
HitCount++;
|
||||
return "Hit count is " + HitCount;
|
||||
}
|
||||
}
|
||||
}
|
33
NzbDrone.Common.Test/CacheTests/CachedManagerFixture.cs
Normal file
33
NzbDrone.Common.Test/CacheTests/CachedManagerFixture.cs
Normal file
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Cache;
|
||||
|
||||
namespace NzbDrone.Common.Test.CacheTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CachedManagerFixture
|
||||
{
|
||||
[Test]
|
||||
public void should_return_proper_type_of_cache()
|
||||
{
|
||||
var result = CacheManger.GetCache<DateTime>(typeof(string));
|
||||
|
||||
result.Should().BeOfType<Cached<DateTime>>();
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void multiple_calls_should_get_the_same_cache()
|
||||
{
|
||||
var result1 = CacheManger.GetCache<DateTime>(typeof(string));
|
||||
var result2 = CacheManger.GetCache<DateTime>(typeof(string));
|
||||
|
||||
result1.Should().BeSameAs(result2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -78,6 +78,8 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CacheTests\CachedManagerFixture.cs" />
|
||||
<Compile Include="CacheTests\CachedFixture.cs" />
|
||||
<Compile Include="ConfigFileProviderTest.cs" />
|
||||
<Compile Include="EventingTests\MessageAggregatorCommandTests.cs" />
|
||||
<Compile Include="EventingTests\MessageAggregatorEventTests.cs" />
|
||||
|
|
22
NzbDrone.Common/Cache/CacheManger.cs
Normal file
22
NzbDrone.Common/Cache/CacheManger.cs
Normal file
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using NzbDrone.Common.EnsureThat;
|
||||
|
||||
namespace NzbDrone.Common.Cache
|
||||
{
|
||||
public static class CacheManger
|
||||
{
|
||||
private static readonly ICached<object> Cache;
|
||||
|
||||
static CacheManger()
|
||||
{
|
||||
Cache = new Cached<object>();
|
||||
}
|
||||
|
||||
public static ICached<T> GetCache<T>(Type type)
|
||||
{
|
||||
Ensure.That(() => type).IsNotNull();
|
||||
|
||||
return (ICached<T>)Cache.Get(type.FullName, () => new Cached<T>());
|
||||
}
|
||||
}
|
||||
}
|
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);
|
||||
}
|
||||
}
|
||||
}
|
13
NzbDrone.Common/Cache/ICached.cs
Normal file
13
NzbDrone.Common/Cache/ICached.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
|
||||
namespace NzbDrone.Common.Cache
|
||||
{
|
||||
public interface ICached<T>
|
||||
{
|
||||
void Set(string key, T value);
|
||||
T Get(string key, Func<T> function);
|
||||
bool ContainsKey(string key);
|
||||
void Clear();
|
||||
void Remove(string key);
|
||||
}
|
||||
}
|
|
@ -84,6 +84,9 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArchiveProvider.cs" />
|
||||
<Compile Include="Cache\Cached.cs" />
|
||||
<Compile Include="Cache\CacheManger.cs" />
|
||||
<Compile Include="Cache\ICached.cs" />
|
||||
<Compile Include="ContainerBuilderBase.cs" />
|
||||
<Compile Include="EnsureThat\Ensure.cs" />
|
||||
<Compile Include="EnsureThat\EnsureBoolExtensions.cs" />
|
||||
|
|
Loading…
Reference in a new issue