using StackExchange.Redis;
public class RedisDataStorage
{
private readonly IConnectionMultiplexer _redis;
public RedisDataStorage(string connectionString)
{
_redis = ConnectionMultiplexer.Connect(connectionString);
}
public void SetData(string key, string value)
{
IDatabase database = _redis.GetDatabase();
database.StringSet(key, value);
}
public string GetData(string key)
{
IDatabase database = _redis.GetDatabase();
return database.StringGet(key);
}
}
使用SetData
方法可以将数据存储到Redis中,需要传递一个键(key)和对应的值(value)。例如:
string redisConnectionString = "your-redis-connection-string"; string key = "myKey"; string value = "myValue"; var storage = new RedisDataStorage(redisConnectionString); storage.SetData(key, value);
使用GetData
方法可以从Redis中检索数据,需要传递一个键(key),并返回对应的值(value)。例如:
string redisConnectionString = "your-redis-connection-string"; string key = "myKey"; var storage = new RedisDataStorage(redisConnectionString); string value = storage.GetData(key);
标签:string,NETCORE,redis,读写,storage,Redis,value,key
From: https://www.cnblogs.com/zzhsmallblog/p/17516124.html