提问人:Susmitha 提问时间:11/17/2023 更新时间:11/17/2023 访问量:11
如何在 4.8 版的 ASP.net Core Project 中实现分布式缓存
How to implement distributed Cache in the ASP.net Core Project of version 4.8
问:
我想使用以下代码在项目中实现分布式缓存,但它抛出空引用。
public class RedisCacheService : ICacheService
{
private readonly IDistributedCache _distributedcache;
private readonly ConnectionMultiplexer _redisConnection;
public RedisCacheService(IDistributedCache distributedcache)
{
var redisconstr = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString;
_redisConnection = ConnectionMultiplexer.Connect(redisconstr);
_distributedcache = distributedcache;
_distributedcache = (IDistributedCache)_redisConnection.GetDatabase();
}
public async Task<T> GetSlidingCache<T>(string key)
{
var redisObj = await _distributedcache.GetStringAsync(key);
if (string.IsNullOrEmpty(redisObj))
return default(T);
return JsonConvert.DeserializeObject<T>(redisObj
, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
}
public async Task<bool> SetSlidingCache<T>(string key, T value, TimeSpan timeSpan)
{
var strValue = JsonHandler.Serialize(value);
await _distributedcache.SetStringAsync(key, strValue, new DistributedCacheEntryOptions
{
SlidingExpiration = timeSpan
});
return true;
}
}
对于 GetCache,数据:
列表 CalendarRoles = _cacheService.GetSlidingCache<List>(Constants.CACHE_CALENDARROLES)。GetAwaiter() 中。GetResult();
对于 SetCache 数据:
CalendarRoles = ReadCalendarRoles( i_RoleId,i_VendorId);
CacheHelper.Cache.SetCache<List<ModelCalendarRole>>(Constants.CACHE_CALENDARROLES, CalendarRoles, timeSpanMinutes);
它使用以下代码来设置数据。它工作正常。
public class RedisCache : ICache
{
private readonly ConnectionMultiplexer _redisConnection;
private readonly IDatabase _Cache;
public RedisCache()
{
var redisconstr = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString;
_redisConnection = ConnectionMultiplexer.Connect(redisconstr);
_Cache = _redisConnection.GetDatabase();
}
public async Task<T> GetCache<T>(string key)
{
var redisObj = await _Cache.StringGetAsync(key);
if (!redisObj.HasValue)
return default(T);
return JsonConvert.DeserializeObject<T>(redisObj
, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
}
public async Task<bool> SetCache<T>(string key, T value,int timeSpan)
{
if (value == null)
return true;
var strVal = JsonConvert.SerializeObject(value
, Formatting.Indented
, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
var cacheTimeSpan = timeSpan > 0 ? TimeSpan.FromMinutes(timeSpan) : TimeSpan.FromMinutes(10);
//Take this settings from configuration
int Hours = 10;
return await _Cache.StringSetAsync(key, strVal,cacheTimeSpan);
}
}
但我想为项目实现分布式缓存。它不支持需要将 DistributedCacheEntryOptions 作为参数传递的 IDistributed 缓存接口。
我可以得到任何解决方案来在具有 4.8 版本的项目中实现滑动缓存技术吗?
答: 暂无答案
评论