added TTL to cached objects

pull/24/head
Keivan Beigi 11 years ago
parent fa5dda8e2f
commit 121f3b973d

@ -0,0 +1,17 @@
using System;
using NzbDrone.Api.REST;
namespace NzbDrone.Api.Config
{
public class NamingConfigResource : RestResource
{
public Boolean IncludeEpisodeTitle { get; set; }
public Boolean ReplaceSpaces { get; set; }
public Boolean RenameEpisodes { get; set; }
public Int32 MultiEpisodeStyle { get; set; }
public Int32 NumberStyle { get; set; }
public String Separator { get; set; }
public Boolean IncludeQuality { get; set; }
public Boolean IncludeSeriesTitle { get; set; }
}
}

@ -0,0 +1,33 @@
using FluentValidation;
using NzbDrone.Core.Organizer;
namespace NzbDrone.Api.Config
{
public class NamingModule : NzbDroneRestModule<NamingConfigResource>
{
private readonly INamingConfigService _namingConfigService;
public NamingModule(INamingConfigService namingConfigService)
: base("config/naming")
{
_namingConfigService = namingConfigService;
GetResourceSingle = GetNamingConfig;
UpdateResource = UpdateNamingConfig;
SharedValidator.RuleFor(c => c.MultiEpisodeStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.NumberStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.Separator).Matches(@"\s|\s\-\s|\.");
}
private NamingConfigResource UpdateNamingConfig(NamingConfigResource resource)
{
return ToResource<NamingConfig>(_namingConfigService.Save, resource);
}
private NamingConfigResource GetNamingConfig()
{
return ToResource(_namingConfigService.GetConfig);
}
}
}

@ -2,54 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using Nancy; using Nancy;
using NzbDrone.Api.Extensions; using NzbDrone.Api.Extensions;
using NzbDrone.Api.REST;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Organizer;
using FluentValidation;
namespace NzbDrone.Api.Config namespace NzbDrone.Api.Config
{ {
public class NamingModule : NzbDroneRestModule<NamingConfigResource>
{
private readonly INamingConfigService _namingConfigService;
public NamingModule(INamingConfigService namingConfigService)
: base("config/naming")
{
_namingConfigService = namingConfigService;
GetResourceSingle = GetNamingConfig;
UpdateResource = UpdateNamingConfig;
SharedValidator.RuleFor(c => c.MultiEpisodeStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.NumberStyle).InclusiveBetween(0, 3);
SharedValidator.RuleFor(c => c.Separator).Matches(@"\s|\s\-\s|\.");
}
private NamingConfigResource UpdateNamingConfig(NamingConfigResource resource)
{
return ToResource<NamingConfig>(_namingConfigService.Save, resource);
}
private NamingConfigResource GetNamingConfig()
{
return ToResource(_namingConfigService.GetConfig);
}
}
public class NamingConfigResource : RestResource
{
public Boolean IncludeEpisodeTitle { get; set; }
public Boolean ReplaceSpaces { get; set; }
public Boolean RenameEpisodes { get; set; }
public Int32 MultiEpisodeStyle { get; set; }
public Int32 NumberStyle { get; set; }
public String Separator { get; set; }
public Boolean IncludeQuality { get; set; }
public Boolean IncludeSeriesTitle { get; set; }
}
public class SettingsModule : NzbDroneApiModule public class SettingsModule : NzbDroneApiModule
{ {
private readonly IConfigService _configService; private readonly IConfigService _configService;
@ -65,6 +21,19 @@ namespace NzbDrone.Api.Config
Get["/host"] = x => GetHostSettings(); Get["/host"] = x => GetHostSettings();
Post["/host"] = x => SaveHostSettings(); Post["/host"] = x => SaveHostSettings();
Get["/log"] = x => GetLogSettings();
Post["/log"] = x => SaveLogSettings();
}
private Response SaveLogSettings()
{
throw new NotImplementedException();
}
private Response GetLogSettings()
{
throw new NotImplementedException();
} }
private Response SaveHostSettings() private Response SaveHostSettings()

@ -101,6 +101,8 @@
<Compile Include="Client\ClientSettings.cs" /> <Compile Include="Client\ClientSettings.cs" />
<Compile Include="Commands\CommandModule.cs" /> <Compile Include="Commands\CommandModule.cs" />
<Compile Include="Commands\CommandResource.cs" /> <Compile Include="Commands\CommandResource.cs" />
<Compile Include="Config\NamingConfigResource.cs" />
<Compile Include="Config\NamingModule.cs" />
<Compile Include="Directories\DirectoryModule.cs" /> <Compile Include="Directories\DirectoryModule.cs" />
<Compile Include="Episodes\EpisodeModule.cs" /> <Compile Include="Episodes\EpisodeModule.cs" />
<Compile Include="Episodes\EpisodeResource.cs" /> <Compile Include="Episodes\EpisodeResource.cs" />

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Threading;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
@ -28,8 +29,6 @@ namespace NzbDrone.Common.Test.CacheTests
} }
[Test] [Test]
public void multiple_calls_should_return_same_result() public void multiple_calls_should_return_same_result()
{ {
@ -37,35 +36,8 @@ namespace NzbDrone.Common.Test.CacheTests
var second = _cachedString.Get("Test", _worker.GetString); var second = _cachedString.Get("Test", _worker.GetString);
first.Should().Be(second); 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");
} }
[Test]
public void get_without_callback_should_throw_on_invalid_key()
{
Assert.Throws<KeyNotFoundException>(() => _cachedString.Get("InvalidKey"));
}
[Test] [Test]
public void should_be_able_to_update_key() public void should_be_able_to_update_key()
@ -73,7 +45,7 @@ namespace NzbDrone.Common.Test.CacheTests
_cachedString.Set("Key", "Old"); _cachedString.Set("Key", "Old");
_cachedString.Set("Key", "New"); _cachedString.Set("Key", "New");
_cachedString.Get("Key").Should().Be("New"); _cachedString.Find("Key").Should().Be("New");
} }
[Test] [Test]
@ -85,14 +57,34 @@ namespace NzbDrone.Common.Test.CacheTests
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
_cachedString.Get("key", () => _cachedString.Get("key", () =>
{ {
hitCount++; hitCount++;
return null; return null;
}); });
} }
hitCount.Should().Be(1); hitCount.Should().Be(1);
} }
[Test]
public void should_honor_ttl()
{
int hitCount = 0;
_cachedString = new Cached<string>();
for (int i = 0; i < 100; i++)
{
_cachedString.Get("key", () =>
{
hitCount++;
return null;
}, TimeSpan.FromMilliseconds(200));
Thread.Sleep(10);
}
hitCount.Should().BeInRange(4, 6);
}
} }
public class Worker public class Worker

@ -7,7 +7,7 @@ using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.CacheTests namespace NzbDrone.Common.Test.CacheTests
{ {
[TestFixture] [TestFixture]
public class CachedManagerFixture:TestBase<ICacheManger> public class CachedManagerFixture : TestBase<ICacheManger>
{ {
[Test] [Test]
public void should_return_proper_type_of_cache() public void should_return_proper_type_of_cache()
@ -17,7 +17,6 @@ namespace NzbDrone.Common.Test.CacheTests
result.Should().BeOfType<Cached<DateTime>>(); result.Should().BeOfType<Cached<DateTime>>();
} }
[Test] [Test]
public void multiple_calls_should_get_the_same_cache() public void multiple_calls_should_get_the_same_cache()
{ {
@ -26,9 +25,5 @@ namespace NzbDrone.Common.Test.CacheTests
result1.Should().BeSameAs(result2); result1.Should().BeSameAs(result2);
} }
} }
} }

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

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

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

@ -118,25 +118,25 @@ namespace NzbDrone.Core.Configuration
public string GetValue(string key, object defaultValue) public string GetValue(string key, object defaultValue)
{ {
return _cache.Get(key, () => return _cache.Get(key, () =>
{ {
EnsureDefaultConfigFile(); EnsureDefaultConfigFile();
var xDoc = XDocument.Load(_configFile); var xDoc = XDocument.Load(_configFile);
var config = xDoc.Descendants("Config").Single(); var config = xDoc.Descendants("Config").Single();
var parentContainer = config; var parentContainer = config;
var valueHolder = parentContainer.Descendants(key).ToList(); var valueHolder = parentContainer.Descendants(key).ToList();
if (valueHolder.Count() == 1) if (valueHolder.Count() == 1)
return valueHolder.First().Value; return valueHolder.First().Value;
//Save the value //Save the value
SetValue(key, defaultValue); SetValue(key, defaultValue);
//return the default value //return the default value
return defaultValue.ToString(); return defaultValue.ToString();
}); });
} }
public void SetValue(string key, object value) public void SetValue(string key, object value)

@ -28,10 +28,10 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
{ {
lock (MigrationCache) lock (MigrationCache)
{ {
_announcer.Heading("Migrating " + connectionString);
if (MigrationCache.Contains(connectionString.ToLower())) return; if (MigrationCache.Contains(connectionString.ToLower())) return;
_announcer.Heading("Migrating " + connectionString);
var assembly = Assembly.GetExecutingAssembly(); var assembly = Assembly.GetExecutingAssembly();
var migrationContext = new RunnerContext(_announcer) var migrationContext = new RunnerContext(_announcer)

Loading…
Cancel
Save