NETCORE - Redis的使用2
NETCORE - Redis的使用
Redis 在netFramework中的应用 也一样
新建.NETCORE(webapi)项目
安装NuGet
//查询NuGet语句 Find-Package [-StackExchange.Redis]
//安装NuGet包 Install-Package StackExchange.Redis
在项目中新建Redis操作类
public class RedisHelper { private ConnectionMultiplexer redis { get; set; } private IDatabase db { get; set; } public RedisHelper(string connection) { redis = ConnectionMultiplexer.Connect(connection); db = redis.GetDatabase(); } /// <summary> /// 增加/修改 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetValue(string key, string value) { return db.StringSet(key, value); } /// <summary> /// 查询 /// </summary> /// <param name="key"></param> /// <returns></returns> public string GetValue(string key) { return db.StringGet(key); } /// <summary> /// 删除 /// </summary> /// <param name="key"></param> /// <returns></returns> public bool DeleteKey(string key) { return db.KeyDelete(key); } }
调用方式
RedisHelper redisHelper = new RedisHelper("127.0.0.1:6379"); //增加 bool r1 = redisHelper.SetValue("mykey", "abcdefg"); //查询 string saveValue = redisHelper.GetValue("mykey"); //更新 bool r2 = redisHelper.SetValue("mykey", "NewValue"); saveValue = redisHelper.GetValue("mykey"); //删除 bool r3 = redisHelper.DeleteKey("mykey"); //查询 - null string uncacheValue = redisHelper.GetValue("mykey");
OK!
引用:https://www.cnblogs.com/Lulus/p/7877688.html