来源:http://www.cnblogs.com/qtqq/p/5951201.html,https://www.cnblogs.com/xsj1989/p/9104185.html

Redis安装:http://www.runoob.com/redis/redis-install.html

Configuration:https://www.cnblogs.com/ArvinZhao/p/6007043.html

Redis可视化工具Redis Desktop Manager:https://redisdesktop.com/download

千万要记住,给Redis设置访问密码,Redis的连接字符串中也要设置好密码。

Redis设置密码:https://blog.csdn.net/qq_35357001/article/details/56835919

启动服务:dos中 cd Redis安装目录,运行 redis-server.exe redis.windows.conf;

启动命令:dos中 cd Redis安装目录,运行 redis-cli.exe -h 127.0.0.1 -p 6379;

设置密码:config set requirepass xsj10011 注意:密码尽量设置长一点

获取密码:config get requirepass  注意:获取密码之前需要先验证密码,不然获取不到

验证密码:auth xsj10011

密码改变之后,代码中的连接字符串里的密码也要对应改变,不然操作不了Redis。对应的可视化工具里的密码也要改。

1.打开VS的NuGet》搜索:StackExchange.Redis,安装到项目中

2.配置文件:

<appSettings>
    <!-- 系统前缀 -->
    <add key="redisKey" value="jay"/>
  </appSettings>
  <connectionStrings>
    <!-- Redis连接字符串 -->
    <add name="RedisExchangeHosts" connectionString="127.0.0.1:6379,allowadmin=true,password=xsj10011,keepAlive=180"/>
  </connectionStrings>

web.config
View Code

3.封装好的Redis操作类(感谢前辈的努力,让我们可以直接拿来用,恕我懒惰):

using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace RedisTest.RedisCommon
{
    /// <summary>
    /// ConnectionMultiplexer对象管理帮助类
    /// </summary>
    public static class RedisConnectionHelp
    {
        //配置:系统自定义Key前缀
        public static readonly string SysCustomKey = ConfigurationManager.AppSettings["redisKey"] ?? "";
        //配置:Redis连接字符串:127.0.0.1:6379,allowadmin=true
        private static readonly string RedisConnectionString = ConfigurationManager.ConnectionStrings["RedisExchangeHosts"].ConnectionString;

        private static readonly object Locker = new object();
        private static ConnectionMultiplexer _instance;
        private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();

        /// <summary>
        /// 单例获取
        /// </summary>
        public static ConnectionMultiplexer Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (Locker)
                    {
                        if (_instance == null || !_instance.IsConnected)
                        {
                            _instance = GetManager();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 缓存获取
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
        {
            if (!ConnectionCache.ContainsKey(connectionString))
            {
                ConnectionCache[connectionString] = GetManager(connectionString);
            }
            return ConnectionCache[connectionString];
        }

        private static ConnectionMultiplexer GetManager(string connectionString = null)
        {
            connectionString = connectionString ?? RedisConnectionString;
            var connect = ConnectionMultiplexer.Connect(connectionString);

            //注册如下事件
            connect.ConnectionFailed += MuxerConnectionFailed;
            connect.ConnectionRestored += MuxerConnectionRestored;
            connect.ErrorMessage += MuxerErrorMessage;
            connect.ConfigurationChanged += MuxerConfigurationChanged;
            connect.HashSlotMoved += MuxerHashSlotMoved;
            connect.InternalError += MuxerInternalError;

            return connect;
        }

        #region 事件

        /// <summary>
        /// 配置更改时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
        {
            Console.WriteLine("Configuration changed: " + e.EndPoint);
        }

        /// <summary>
        /// 发生错误时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
        {
            Console.WriteLine("ErrorMessage: " + e.Message);
        }

        /// <summary>
        /// 重新建立连接之前的错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
        {
            Console.WriteLine("ConnectionRestored: " + e.EndPoint);
        }

        /// <summary>
        /// 连接失败 , 如果重新连接成功你将不会收到这个通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
        {
            Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
        }

        /// <summary>
        /// 更改集群
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
        {
            Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
        }

        /// <summary>
        /// redis类库错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
        {
            Console.WriteLine("InternalError:Message" + e.Exception.Message);
        }

        #endregion 事件
    }
}

RedisConnectionHelp
View Code

相关文章:

  • 2021-06-09
  • 2022-02-10
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-12
  • 2021-08-16
  • 2021-07-24
  • 2021-08-11
相关资源
相似解决方案