added TTL to cached objects

pull/4/head
Keivan Beigi 12 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 Nancy;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.REST;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Organizer;
using FluentValidation;
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
{
private readonly IConfigService _configService;
@ -65,6 +21,19 @@ namespace NzbDrone.Api.Config
Get["/host"] = x => GetHostSettings();
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()

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

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

@ -7,7 +7,7 @@ using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.CacheTests
{
[TestFixture]
public class CachedManagerFixture:TestBase<ICacheManger>
public class CachedManagerFixture : TestBase<ICacheManger>
{
[Test]
public void should_return_proper_type_of_cache()
@ -17,7 +17,6 @@ namespace NzbDrone.Common.Test.CacheTests
result.Should().BeOfType<Cached<DateTime>>();
}
[Test]
public void multiple_calls_should_get_the_same_cache()
{
@ -26,9 +25,5 @@ namespace NzbDrone.Common.Test.CacheTests
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);
//ICollection<ICached<T>> Caches<T> { get;}
void Clear();
ICollection<ICached> Caches { get; }
}

@ -1,83 +1,99 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.EnsureThat;
namespace NzbDrone.Common.Cache
{
public class Cached<T> : ICached<T>
{
private readonly ConcurrentDictionary<string, T> _store;
public Cached()
private class CacheItem
{
_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[key] = value;
_store = new ConcurrentDictionary<string, CacheItem>();
}
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)
{
T value;
CacheItem 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();
CacheItem cacheItem;
T value;
if (!_store.TryGetValue(key, out value))
if (!_store.TryGetValue(key, out cacheItem) || cacheItem.IsExpired())
{
value = function();
Set(key, value);
Set(key, value, lifeTime);
}
else
{
value = cacheItem.Object;
}
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);
}
public ICollection<T> Values
{
get
{
return _store.Values;
}
}
public ICollection<string> Keys
{
get
{
return _store.Keys;
return _store.Values.Select(c => c.Object).ToList();
}
}
}
}

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

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

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

Loading…
Cancel
Save