mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Making ICacheService and ICacheServiceProviders generic.
This commit is contained in:
@@ -17,15 +17,15 @@ namespace Orchard.Caching.Services {
|
||||
_prefix = shellSettings.Name;
|
||||
}
|
||||
|
||||
public object Get(string key) {
|
||||
return _cacheStorageProvider.Get(BuildFullKey(key));
|
||||
public T Get<T>(string 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace Orchard.Caching.Services {
|
||||
}
|
||||
|
||||
private string BuildFullKey(string key) {
|
||||
return String.Concat(_prefix, "_", key);
|
||||
return String.Concat(_prefix, ":", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using System.Web;
|
||||
namespace Orchard.Caching.Services {
|
||||
public class DefaultCacheStorageProvider : ICacheStorageProvider {
|
||||
|
||||
public void Put(string key, object value) {
|
||||
public void Put<T>(string key, T value) {
|
||||
HttpRuntime.Cache.Insert(
|
||||
key,
|
||||
value,
|
||||
@@ -17,7 +17,7 @@ namespace Orchard.Caching.Services {
|
||||
null);
|
||||
}
|
||||
|
||||
public void Put(string key, object value, TimeSpan validFor) {
|
||||
public void Put<T>(string key, T value, TimeSpan validFor) {
|
||||
HttpRuntime.Cache.Insert(
|
||||
key,
|
||||
value,
|
||||
@@ -44,8 +44,13 @@ namespace Orchard.Caching.Services {
|
||||
}
|
||||
}
|
||||
|
||||
public object Get(string key) {
|
||||
return HttpRuntime.Cache.Get(key);
|
||||
public T Get<T>(string key) {
|
||||
var value = HttpRuntime.Cache.Get(key);
|
||||
if (value is T) {
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
return default(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,23 +2,18 @@
|
||||
|
||||
namespace Orchard.Caching.Services {
|
||||
public interface ICacheService : IDependency {
|
||||
T Get<T>(string key);
|
||||
|
||||
object Get(string key);
|
||||
void Put(string key, object value);
|
||||
void Put(string key, object value, TimeSpan validFor);
|
||||
void Put<T>(string key, T value);
|
||||
void Put<T>(string key, T value, TimeSpan validFor);
|
||||
|
||||
void Remove(string key);
|
||||
void Clear();
|
||||
}
|
||||
|
||||
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) {
|
||||
var result = cacheService.Get(key);
|
||||
var result = cacheService.Get<T>(key);
|
||||
if (result == null) {
|
||||
var computed = factory();
|
||||
cacheService.Put(key, computed);
|
||||
@@ -26,11 +21,11 @@ namespace Orchard.Caching.Services {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
var result = cacheService.Get(key);
|
||||
var result = cacheService.Get<T>(key);
|
||||
if (result == null) {
|
||||
var computed = factory();
|
||||
cacheService.Put(key, computed, validFor);
|
||||
@@ -38,7 +33,7 @@ namespace Orchard.Caching.Services {
|
||||
}
|
||||
|
||||
// try to convert to T
|
||||
return (T)result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace Orchard.Caching.Services {
|
||||
public interface ICacheStorageProvider : IDependency {
|
||||
object Get(string key);
|
||||
void Put(string key, object value);
|
||||
void Put(string key, object value, TimeSpan validFor);
|
||||
T Get<T>(string key);
|
||||
void Put<T>(string key, T value);
|
||||
void Put<T>(string key, T value, TimeSpan validFor);
|
||||
void Remove(string key);
|
||||
void Clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user