#21068: Fixing caching of value types

Work Item: 21068
This commit is contained in:
Sebastien Ros
2014-11-14 11:20:41 -08:00
parent a0390c88bd
commit 065e4fdf42
4 changed files with 12 additions and 9 deletions

View File

@@ -4,7 +4,6 @@ using Orchard.Environment.Configuration;
namespace Orchard.Caching.Services {
/// <summary>
/// Provides a per tenant <see cref="ICacheService"/> implementation.
/// Default timeout is 20 minutes.
/// </summary>
public class DefaultCacheService : ICacheService {
private readonly ICacheStorageProvider _cacheStorageProvider;
@@ -17,7 +16,7 @@ namespace Orchard.Caching.Services {
_prefix = shellSettings.Name;
}
public T Get<T>(string key) {
public object Get<T>(string key) {
return _cacheStorageProvider.Get<T>(BuildFullKey(key));
}

View File

@@ -39,14 +39,16 @@ namespace Orchard.Caching.Services {
}
}
public T Get<T>(string key) {
var value = _cache.Get(key) ;
public object Get<T>(string key) {
var value = _cache.Get(key);
// if the provided expression is non-null, and the provided object can
// be cast to the provided type without causing an exception to be thrown
if(value is T) {
return (T)value;
}
return default(T);
return null;
}
private CacheItemPolicy GetCacheItemPolicy(DateTimeOffset absoluteExpiration) {

View File

@@ -2,7 +2,7 @@
namespace Orchard.Caching.Services {
public interface ICacheService : IDependency {
T Get<T>(string key);
object Get<T>(string key);
void Put<T>(string key, T value);
void Put<T>(string key, T value, TimeSpan validFor);
@@ -12,8 +12,9 @@ namespace Orchard.Caching.Services {
}
public static class CachingExtensions {
public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory) {
public static object Get<T>(this ICacheService cacheService, string key, Func<T> factory) {
var result = cacheService.Get<T>(key);
if (result == null) {
var computed = factory();
cacheService.Put(key, computed);
@@ -24,8 +25,9 @@ namespace Orchard.Caching.Services {
return result;
}
public static T Get<T>(this ICacheService cacheService, string key, Func<T> factory, TimeSpan validFor) {
public static object Get<T>(this ICacheService cacheService, string key, Func<T> factory, TimeSpan validFor) {
var result = cacheService.Get<T>(key);
if (result == null) {
var computed = factory();
cacheService.Put(key, computed, validFor);

View File

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