c# - 使用泛型避免缓存不同类型时的强制转换

标签 c# generics caching casting stackexchange.redis

我正在使用 ASP.NET Core 和 Redis 缓存。我试图在缓存中存储不同类型的不同对象,并且我想避免显式转换。

这是我的 Redis 缓存包装

public class RedisCacheStorage : Contracts.ICacheStorage
{
    private CachingFramework.Redis.Context _context = null;

    public RedisCacheStorage(string configuration)
    {
        _context = new CachingFramework.Redis.Context(configuration, new CachingFramework.Redis.Serializers.JsonSerializer());
    }
    public void SetItem<T>(string key, T value)
    {
        _context.Cache.SetObject<T>(key, value);
    }
    public T GetItem<T>(string key)
    {
        return _context.Cache.GetObject<T>(key);
    }

    public T GetItem<T>(string key, Func<T> loadCacheFunc)
    {
        return _context.Cache.FetchObject<T>(key, loadCacheFunc);
    }

然后我将 ICacheStorage 注入(inject) CacheManager(它实现了 ICacheManager)。我试图隔离依赖关系并保持 CacheStorage 简单,因此当我需要更改缓存类型时,我只需实现 ICacheStorage。在 CacheManager 中,我们注入(inject)所有在传递特殊键时获取一些数据的服务。

缓存管理器:

  public class CacheManager : Contracts.ICacheManager
{
    private Contracts.ICacheStorage _cacheStorage;
    private SecurityCore.ServiceContracts.IParametersService _paramService;
    public CacheManager(Contracts.ICacheStorage cacheStorage, SecurityCore.ServiceContracts.IParametersService paramService)
    {
        _cacheStorage = cacheStorage;
        _paramService = paramService;
    }
    public Object GetItem(string key)
    {
        if (key == Constants.CacheKeys.SecuritySystemParams)
            return _cacheStorage.GetItem<Dictionary<string, string>>(key, _paramService.GetSystemParameters);

        //if (key == Constants.CacheKeys.EffectivePermissions)
        //   return  List of Effective Permissions

        return _cacheStorage.GetItem<Object>(key);
    }

_cacheStorage.GetItem<Dictionary<string, string>>(key, _paramService.GetSystemParameters);

传递一个使用Redis的Fetch方法的函数,如果缓存为空,则调用服务,然后将数据存储到缓存中并返回。

我的问题是我需要避免强制转换,因为我可能会返回不同的对象,我如何才能继续使用泛型,所以我传递返回的对象的类型。

正如您在下面看到的编译错误,由于无法将类型对象转换为 Dictionay,这需要显式转换才能解决。

是否有更好、更优雅的方式来实现整个想法?

enter image description here

最佳答案

阅读错误消息。
您需要显式指定类型参数。

您可以使用类型安全键来改善这一点:

class CacheKey<T> {
    public string Name { get; }
    public string ToString() => Name;
    public CacheKey(string name) { Name = name; }
}

public T GetItem<T>(CacheKey<T> key) { ... }

public CacheKey<Dictionary<string, string>> SecuritySystemParams { get; } = new CacheKey<Dictionary<string, string>>("SecuritySystemParams");

这将使 GetItem() 从键中推断出 T,并防止您传递错误的类型。

关于c# - 使用泛型避免缓存不同类型时的强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40468614/

相关文章:

java - Oracle 的 Collection<Object> 教程令人困惑

Kotlin 对 "supposedly"正确类型的类型推断

java - 缓存 XML 文件 Java

caching - 如何为动态 bean 内的方法启用缓存

c# - 访问IsolatedStorageBackingStore时出现HRESULT 0X80131468的原因

c# - 我该如何解决这个线程问题?

c# - 删除换行符和空格

c# - 二维网格的 .NET 集合?

javascript - 在javascript中清除浏览器关闭事件的缓存?

javascript - TypeScript 中类型参数的可选计数