1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

non-working cached repository.

This commit is contained in:
Keivan Beigi 2013-05-30 16:32:50 -07:00
parent 23e9c725f3
commit 5b2410da3f
6 changed files with 92 additions and 5 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using NzbDrone.Common.EnsureThat;
namespace NzbDrone.Common.Cache
@ -7,15 +8,18 @@ public interface ICacheManger
{
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; }
}
public class CacheManger : ICacheManger
{
private readonly ICached<object> _cache;
private readonly ICached<ICached> _cache;
public CacheManger()
{
_cache = new Cached<object>();
_cache = new Cached<ICached>();
}
@ -25,6 +29,14 @@ public ICached<T> GetCache<T>(Type host)
return GetCache<T>(host, host.FullName);
}
public void Clear()
{
_cache.Clear();
}
public ICollection<ICached> Caches { get { return _cache.Values; } }
public ICached<T> GetCache<T>(Type host, string name)
{
Ensure.That(() => host).IsNotNull();

View File

@ -64,5 +64,20 @@ public void Remove(string key)
T value;
_store.TryRemove(key, out value);
}
public ICollection<T> Values
{
get
{
return _store.Values;
}
}
public ICollection<string> Keys
{
get
{
return _store.Keys;
}
}
}
}

View File

@ -1,15 +1,23 @@
using System;
using System.Collections.Generic;
namespace NzbDrone.Common.Cache
{
public interface ICached<T>
public interface ICached
{
void Set(string key, T value);
T Get(string key, Func<T> function);
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);
T Find(string key);
ICollection<T> Values { get; }
ICollection<string> Keys { get; }
}
}

View File

@ -32,6 +32,7 @@ namespace NzbDrone.Core.Datastore
TModel Single();
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
private readonly IDatabase _database;
@ -202,6 +203,16 @@ private void PublishModelEvent(TModel model, RepositoryAction action)
}
}
protected virtual void OnModelChanged(IEnumerable<TModel> models)
{
}
protected virtual void OnModelDeleted(IEnumerable<TModel> models)
{
}
protected virtual bool PublishModelEvents
{
get { return false; }

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Messaging;
namespace NzbDrone.Core.Datastore
{
public abstract class CachedBasicRepository<TModel> : BasicRepository<TModel> where TModel : ModelBase, new()
{
private readonly ICacheManger _cacheManger;
protected CachedBasicRepository(IDatabase database, IMessageAggregator messageAggregator)
: base(database, messageAggregator)
{
_cacheManger = new CacheManger();
}
protected ICached<T> GetCache<T>(string name)
{
return _cacheManger.GetCache<T>(GetType(), name);
}
protected override void OnModelChanged(IEnumerable<TModel> models)
{
PurgeCache();
}
protected override void OnModelDeleted(IEnumerable<TModel> models)
{
PurgeCache();
}
private void PurgeCache()
{
foreach (var model in _cacheManger.Caches)
{
model.Clear();
}
}
}
}

View File

@ -193,6 +193,7 @@
<Compile Include="DataAugmentation\Scene\SceneMappingProxy.cs" />
<Compile Include="DataAugmentation\Scene\SceneMappingRepository.cs" />
<Compile Include="DataAugmentation\Scene\UpdateSceneMappingCommand.cs" />
<Compile Include="Datastore\CachedBasicRepository.cs" />
<Compile Include="Datastore\Converters\BooleanIntConverter.cs" />
<Compile Include="Datastore\Converters\QualityIntConverter.cs" />
<Compile Include="Datastore\Converters\Int32Converter.cs" />