在.Net Core 2.1中简单使用Redis做缓存

一、在Windows10下安装Redis

1、下载免安装的Redis版本。下载地址:https://github.com/MicrosoftArchive/redis/releases

Redis安装包选择

Redis解压后的文件目录

2、在该文件夹下运行命令:redis-server.exe redis.windows.conf

安装成功

3、将redis安装成服务。运行redis-server.exe --service-install redis.windows.conf

服务

4、使用命令行操作redis

set:保存数据或修改数据,例 set name lily
get: 取数据,例 get name

更多请参考:https://www.cnblogs.com/zqr99/p/7899701.html

二、在.Net Core 2.1中使用

1、安装依赖包:Nugget: StackExchange.Redis
2、新建RedisHelper

        public class RedisHelper
        {
            private ConnectionMultiplexer Redis { get; set; }
            private IDatabase DB { get; set; }
            public RedisHelper(string connection)
            {
                Redis = ConnectionMultiplexer.Connect(connection);
                DB = Redis.GetDatabase();
            }

            /// 
            /// 增加/修改
            /// 
            /// 
            /// 
            /// 
            public bool SetValue(string key, string value)
            {
                return DB.StringSet(key, value);
            }

            /// 
            /// 查询
            /// 
            /// 
            /// 
            public string GetValue(string key)
            {
                return DB.StringGet(key);
            }

            /// 
            /// 删除
            /// 
            /// 
            /// 
            public bool DeleteKey(string key)
            {
                return DB.KeyDelete(key);
            }
        }

3、在控制台中使用

        RedisHelper redisHelper = new RedisHelper("127.0.0.1:6379");
        string value = "测试数据";
        bool testValue = redisHelper.SetValue("key", value);
        string saveValue = redisHelper.GetValue("key");

        Console.WriteLine(saveValue);

        bool newValue = redisHelper.SetValue("key", "NewValue");
        saveValue = redisHelper.GetValue("key");

        Console.WriteLine(saveValue);

        bool deleteKey = redisHelper.DeleteKey("key");
        string empty = redisHelper.GetValue("key");

        Console.WriteLine(empty);

        Console.ReadKey();
三、开源Redis可视化软件 AnotherRedisDesktopManager

1、下载地址:https://github.com/qishibo/AnotherRedisDesktopManager

安装效果

你可能感兴趣的:(在.Net Core 2.1中简单使用Redis做缓存)