From 5b2410da3faf4c3478948d3f3e2ced947ccf0296 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Thu, 30 May 2013 16:32:50 -0700 Subject: [PATCH] non-working cached repository. --- NzbDrone.Common/Cache/CacheManger.cs | 16 +++++++- NzbDrone.Common/Cache/Cached.cs | 15 +++++++ NzbDrone.Common/Cache/ICached.cs | 14 +++++-- NzbDrone.Core/Datastore/BasicRepository.cs | 11 +++++ .../Datastore/CachedBasicRepository.cs | 40 +++++++++++++++++++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 NzbDrone.Core/Datastore/CachedBasicRepository.cs diff --git a/NzbDrone.Common/Cache/CacheManger.cs b/NzbDrone.Common/Cache/CacheManger.cs index b5ab02844..beba81651 100644 --- a/NzbDrone.Common/Cache/CacheManger.cs +++ b/NzbDrone.Common/Cache/CacheManger.cs @@ -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 GetCache(Type host, string name); ICached GetCache(Type host); + //ICollection> Caches { get;} + void Clear(); + ICollection Caches { get; } } public class CacheManger : ICacheManger { - private readonly ICached _cache; + private readonly ICached _cache; public CacheManger() { - _cache = new Cached(); + _cache = new Cached(); } @@ -25,6 +29,14 @@ public ICached GetCache(Type host) return GetCache(host, host.FullName); } + + public void Clear() + { + _cache.Clear(); + } + + public ICollection Caches { get { return _cache.Values; } } + public ICached GetCache(Type host, string name) { Ensure.That(() => host).IsNotNull(); diff --git a/NzbDrone.Common/Cache/Cached.cs b/NzbDrone.Common/Cache/Cached.cs index 59ac622c7..de5680b8e 100644 --- a/NzbDrone.Common/Cache/Cached.cs +++ b/NzbDrone.Common/Cache/Cached.cs @@ -64,5 +64,20 @@ public void Remove(string key) T value; _store.TryRemove(key, out value); } + + public ICollection Values + { + get + { + return _store.Values; + } + } + public ICollection Keys + { + get + { + return _store.Keys; + } + } } } \ No newline at end of file diff --git a/NzbDrone.Common/Cache/ICached.cs b/NzbDrone.Common/Cache/ICached.cs index cd4fc9e28..599479f7c 100644 --- a/NzbDrone.Common/Cache/ICached.cs +++ b/NzbDrone.Common/Cache/ICached.cs @@ -1,15 +1,23 @@ using System; +using System.Collections.Generic; namespace NzbDrone.Common.Cache { - public interface ICached + public interface ICached { - void Set(string key, T value); - T Get(string key, Func function); bool ContainsKey(string key); void Clear(); void Remove(string key); + } + + public interface ICached : ICached + { + void Set(string key, T value); + T Get(string key, Func function); T Get(string key); T Find(string key); + + ICollection Values { get; } + ICollection Keys { get; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 18f6bf3b7..0a9bdb453 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -32,6 +32,7 @@ namespace NzbDrone.Core.Datastore TModel Single(); } + public class BasicRepository : IBasicRepository where TModel : ModelBase, new() { private readonly IDatabase _database; @@ -202,6 +203,16 @@ private void PublishModelEvent(TModel model, RepositoryAction action) } } + protected virtual void OnModelChanged(IEnumerable models) + { + + } + + protected virtual void OnModelDeleted(IEnumerable models) + { + + } + protected virtual bool PublishModelEvents { get { return false; } diff --git a/NzbDrone.Core/Datastore/CachedBasicRepository.cs b/NzbDrone.Core/Datastore/CachedBasicRepository.cs new file mode 100644 index 000000000..1938af390 --- /dev/null +++ b/NzbDrone.Core/Datastore/CachedBasicRepository.cs @@ -0,0 +1,40 @@ +using System.Collections.Generic; +using NzbDrone.Common.Cache; +using NzbDrone.Common.Messaging; + +namespace NzbDrone.Core.Datastore +{ + public abstract class CachedBasicRepository : BasicRepository where TModel : ModelBase, new() + { + private readonly ICacheManger _cacheManger; + + protected CachedBasicRepository(IDatabase database, IMessageAggregator messageAggregator) + : base(database, messageAggregator) + { + _cacheManger = new CacheManger(); + } + + protected ICached GetCache(string name) + { + return _cacheManger.GetCache(GetType(), name); + } + + protected override void OnModelChanged(IEnumerable models) + { + PurgeCache(); + } + + protected override void OnModelDeleted(IEnumerable models) + { + PurgeCache(); + } + + private void PurgeCache() + { + foreach (var model in _cacheManger.Caches) + { + model.Clear(); + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 5f2559af5..bc485a87d 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -193,6 +193,7 @@ +