Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Redis缓存中添加/更新/删除值_C#_Redis_.net Core - Fatal编程技术网

C# 如何在Redis缓存中添加/更新/删除值

C# 如何在Redis缓存中添加/更新/删除值,c#,redis,.net-core,C#,Redis,.net Core,我刚刚接触分布式缓存,下面有以下代码,如果数据库表中不存在用户列表值,则将其存储在缓存中 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.De

我刚刚接触分布式缓存,下面有以下代码,如果数据库表中不存在用户列表值,则将其存储在缓存中

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);
}
var cacheKey=“samplekey”;
var userListdata=//包含数据库表中的用户列表
var data=_distributedCache.GetString(cacheKey);
如果(!string.IsNullOrEmpty(数据))
{
var tModel=JsonConvert.DeserializeObject(数据);
返回新的CommandActionResult(tModel,CommandActionStatus.NothingModified);
}
其他的
{
var jsonData=JsonConvert.SerializeObject(userListdata);
_SetString(cacheKey,jsonData);
返回新的CommandActionResult(null,CommandActionStatus.NothingModified);
}

如果有新用户要从表中添加/删除/更新,如何添加/更新/删除samplekey缓存键?

您可以更改方法。在userListdata中保存每个用户,就像在redis缓存中保存唯一的keyValuePair一样。然后您可以将其用于插入/更新/删除

// 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...

你在用什么缓存?Redis?@NgùHùngPhúc是Redis