我刚接触分布式缓存,我有下面的代码,如果数据库表中的用户列表值不存在,它会将其存储在缓存中。
var cacheKey ="samplekey";
var userListdata = //contains list of users from a db table
var data = _distributedCache.GetString(cacheKey);
if (!string.IsNullOrEmpty(data))
{
var tModel = JsonConvert.DeserializeObject<List<user>>(data);
return new CommandActionResult<List<user>>(tModel, CommandActionStatus.NothingModified);
}
else
{
var jsonData = JsonConvert.SerializeObject(userListdata);
_distributedCache.SetString(cacheKey, jsonData);
return new CommandActionResult<List<user>>(null, CommandActionStatus.NothingModified);
}
在我有一个新用户要从表中添加/删除/更新的情况下,如何添加/更新/删除samplekey缓存键?
发布于 2019-06-13 04:32:16
您可以更改方法。像保存redis缓存中的唯一keyValuePair一样,将每个用户保存在userListdata中。然后,您可以将其用于插入/更新/删除。
// add the users to azure redis cache
foreach (var user in userListdata)
{
_distributedCache.StringSet(user.Id, JsonConvert.SerializeObject(user));
}
// update/delete your records
var data = _distributedCache.GetString(cacheKey);
if (!string.IsNullOrEmpty(data)){
_distributedCache.KeyDelete(cacheKey);
_distributedCache.StringSet(cacheKey, newValues);
}
// else insert a new one...
https://stackoverflow.com/questions/51369828
复制相似问题