Making ICacheService and ICacheServiceProviders generic.

This commit is contained in:
Piotr Szmyd
2014-10-15 02:02:36 +02:00
parent 507ddd13b8
commit dc6087e620
4 changed files with 24 additions and 24 deletions

View File

@@ -17,15 +17,15 @@ namespace Orchard.Caching.Services {
_prefix = shellSettings.Name; _prefix = shellSettings.Name;
} }
public object Get(string key) { public T Get<T>(string key) {
return _cacheStorageProvider.Get(BuildFullKey(key)); return _cacheStorageProvider.Get<T>(BuildFullKey(key));
} }
public void Put(string key, object value) { public void Put<T>(string key, T value) {
_cacheStorageProvider.Put(BuildFullKey(key), value); _cacheStorageProvider.Put(BuildFullKey(key), value);
} }
public void Put(string key, object value, TimeSpan validFor) { public void Put<T>(string key, T value, TimeSpan validFor) {
_cacheStorageProvider.Put(BuildFullKey(key), value, validFor); _cacheStorageProvider.Put(BuildFullKey(key), value, validFor);
} }
@@ -38,7 +38,7 @@ namespace Orchard.Caching.Services {
} }
private string BuildFullKey(string key) { private string BuildFullKey(string key) {
return String.Concat(_prefix, "_", key); return String.Concat(_prefix, ":", key);
} }
} }
} }

View File

@@ -6,7 +6,7 @@ using System.Web;
namespace Orchard.Caching.Services { namespace Orchard.Caching.Services {
public class DefaultCacheStorageProvider : ICacheStorageProvider { public class DefaultCacheStorageProvider : ICacheStorageProvider {
public void Put(string key, object value) { public void Put<T>(string key, T value) {
HttpRuntime.Cache.Insert( HttpRuntime.Cache.Insert(
key, key,
value, value,
@@ -17,7 +17,7 @@ namespace Orchard.Caching.Services {
null); null);
} }
public void Put(string key, object value, TimeSpan validFor) { public void Put<T>(string key, T value, TimeSpan validFor) {
HttpRuntime.Cache.Insert( HttpRuntime.Cache.Insert(
key, key,
value, value,
@@ -44,8 +44,13 @@ namespace Orchard.Caching.Services {
} }
} }
public object Get(string key) { public T Get<T>(string key) {
return HttpRuntime.Cache.Get(key); var value = HttpRuntime.Cache.Get(key);
if (value is T) {
return (T)value;
}
return default(T);
} }
} }
} }

View File

@@ -2,23 +2,18 @@
namespace Orchard.Caching.Services { namespace Orchard.Caching.Services {
public interface ICacheService : IDependency { public interface ICacheService : IDependency {
T Get<T>(string key);
object Get(string key); void Put<T>(string key, T value);
void Put(string key, object value); void Put<T>(string key, T value, TimeSpan validFor);
void Put(string key, object value, TimeSpan validFor);
void Remove(string key); void Remove(string key);
void Clear(); void Clear();
} }
public static class CachingExtensions { public static class CachingExtensions {
public static T Get<T>(this ICacheService cacheService, string key) {
return (T)cacheService.Get(key);
}
public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory) { public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory) {
var result = cacheService.Get(key); var result = cacheService.Get<T>(key);
if (result == null) { if (result == null) {
var computed = factory(); var computed = factory();
cacheService.Put(key, computed); cacheService.Put(key, computed);
@@ -26,11 +21,11 @@ namespace Orchard.Caching.Services {
} }
// try to convert to T // try to convert to T
return (T)result; return result;
} }
public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory, TimeSpan validFor) { public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory, TimeSpan validFor) {
var result = cacheService.Get(key); var result = cacheService.Get<T>(key);
if (result == null) { if (result == null) {
var computed = factory(); var computed = factory();
cacheService.Put(key, computed, validFor); cacheService.Put(key, computed, validFor);
@@ -38,7 +33,7 @@ namespace Orchard.Caching.Services {
} }
// try to convert to T // try to convert to T
return (T)result; return result;
} }
} }
} }

View File

@@ -2,9 +2,9 @@
namespace Orchard.Caching.Services { namespace Orchard.Caching.Services {
public interface ICacheStorageProvider : IDependency { public interface ICacheStorageProvider : IDependency {
object Get(string key); T Get<T>(string key);
void Put(string key, object value); void Put<T>(string key, T value);
void Put(string key, object value, TimeSpan validFor); void Put<T>(string key, T value, TimeSpan validFor);
void Remove(string key); void Remove(string key);
void Clear(); void Clear();
} }