我正面临着一个棘手的问题:redis。当我为一个类使用redis时,它工作正常,但当我尝试为另一个实体实现相同的代码时,它给了我错误。
我的存储库类
private readonly IDBCommunication _dBCommunication;
private readonly IConfiguration _config;
private readonly IRedisCacheClient _redis;
private readonly ILog _log;
//Dependency INJECTION OF THE REDIS CLIENT
public MaterialsRepository(IDBCommunication dBCommunication, IConfiguration config, IRedisCacheClient redis,ILog log)
{
_dBCommunication = dBCommunication;
_config = config;
_redis = redis;
_log = log;
}
//STORING & FETCHING FROM THE REDIS CLIENT CACHE
public async Task<ApiResponse> GetMaterials()
{
MaterialCache materialCache1 = new MaterialCache(_redis);
var cacheResult = await materialCache1.Get();
if (cacheResult == null)
{
DataContext dataContext = new DataContext(_dBCommunication, _config);
var result = await dataContext.Materials();
if(result.errors.State == 1)
{
var br = await materialCache1.Save(result);
}
return result;
}
else
{
return new ApiResponse(cacheResult.ToArray<dynamic>().ToList<dynamic>(), new ResponseStatus { State = 1});
}
}
缓存类
//LOGIC FOR STORING & FETCHING THE REDIS CACHE
public class MaterialCache
{
private readonly IRedisCacheClient _redisCacheClient;
public MaterialCache(IRedisCacheClient redisCacheClient)
{
_redisCacheClient = redisCacheClient;
}
public async Task<bool> Save(ApiResponse materials)
{
bool isAdded = await _redisCacheClient.Db0.AddAsync(GlobalMessages.RedisCacheKeys.MaterialCacheAllKey, materials.data, DateTimeOffset.Now.AddMinutes(Novarch.API.Helpers.GlobalMessages.CachingExpireTime.MaterialCacheExpireTime));
return isAdded;
}
public async Task<List<Ms_MaterialModel>> Get()
{
var material = await _redisCacheClient.Db0.GetAsync<List<Ms_MaterialModel>>(GlobalMessages.RedisCacheKeys.MaterialCacheAllKey);
return material;
}
}
启动类设置
services.AddMemoryCache();
#region Regis Configuration
var redisConfiguration = Configuration.GetSection("Redis").Get<RedisConfiguration>();
services.AddControllersWithViews();
services.AddStackExchangeRedisExtensions<NewtonsoftSerializer>(redisConfiguration);
#endregion
services.AddScoped<IMaterialsRepository, MaterialsRepository>();
错误图像
暂无答案!
目前还没有任何答案,快来回答吧!