iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Redis学习笔记:C#中的使用
  • 923
分享到

Redis学习笔记:C#中的使用

Redis学习笔记:C#中的使用 2016-08-05 16:08:03 923人浏览 无得
摘要

    1、新建一个winform窗体,命名为Main:     2、在NuGet中安装StackExchange.Redis。     3、添加一个类,命名为RedisHelper。 public static

Redis学习笔记:C#中的使用

    1、新建一个winform窗体,命名为Main:

    2、在NuGet中安装StackExchange.Redis

    3、添加一个类,命名为RedisHelper。

    public static class RedisHelper
    {
        private static string Constr = "";
        private static readonly object _locker = new object();
        private static ConnectionMultiplexer _instance = null;

        /// 
        /// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。
        /// 
        public static ConnectionMultiplexer Instance
        {
            get
            {
                if (Constr.Length == 0)
                {
                    throw new Exception("连接字符串未设置!");
                }
                if (_instance == null)
                {
                    lock (_locker)
                    {
                        if (_instance == null || !_instance.IsConnected)
                        {
                            _instance = ConnectionMultiplexer.Connect(Constr);
                        }
                    }
                }
                //注册如下事件
                _instance.ConnectionFailed += MuxerConnectionFailed;
                _instance.ConnectionRestored += MuxerConnectionRestored;
                _instance.ErrORMessage += MuxerErrorMessage;
                _instance.ConfigurationChanged += MuxerConfigurationChanged;
                _instance.HashSlotMoved += MuxerHashSlotMoved;
                _instance.InternalError += MuxerInternalError;
                return _instance;
            }
        }

        static RedisHelper()
        {
        }

        public static void SetCon(string config)
        {
            Constr = config;
        }

        public static IDatabase GetDatabase()
        {
            return Instance.GetDatabase();
        }

        /// 
        /// 这里的 MergeKey 用来拼接 Key 的前缀,具体不同的业务模块使用不同的前缀。
        /// 
        /// 
        /// 
        private static string MergeKey(string key)
        {
            return key;
            //return BaseSystemInfo.SystemCode + key;
        }

        /// 
        /// 根据key获取缓存对象
        /// 
        /// 
        /// 
        /// 
        public static T Get(string key)
        {
            key = MergeKey(key);
            return Deserialize(GetDatabase().StringGet(key));
        }

        /// 
        /// 根据key获取缓存对象
        /// 
        /// 
        /// 
        public static object Get(string key)
        {
            key = MergeKey(key);
            return Deserialize<object>(GetDatabase().StringGet(key));
        }

        /// 
        /// 设置缓存
        /// 
        /// 
        /// 
        /// 
        public static void Set(string key, object value, int expireMinutes = 0)
        {
            key = MergeKey(key);
            if (expireMinutes > 0)
            {
                GetDatabase().StringSet(key, Serialize(value), TimeSpan.FromMinutes(expireMinutes));
            }
            else
            {
                GetDatabase().StringSet(key, Serialize(value));
            }
        }

        /// 
        /// 判断在缓存中是否存在该key的缓存数据
        /// 
        /// 
        /// 
        public static bool Exists(string key)
        {
            key = MergeKey(key);
            return GetDatabase().KeyExists(key); //可直接调用
        }

        /// 
        /// 移除指定key的缓存
        /// 
        /// 
        /// 
        public static bool Remove(string key)
        {
            key = MergeKey(key);
            return GetDatabase().KeyDelete(key);
        }

        /// 
        /// 异步设置
        /// 
        /// 
        /// 
        public static async Task SetAsync(string key, object value)
        {
            key = MergeKey(key);
            await GetDatabase().StringSetAsync(key, Serialize(value));
        }

        /// 
        /// 根据key获取缓存对象
        /// 
        /// 
        /// 
        public static async Task<object> GetAsync(string key)
        {
            key = MergeKey(key);
            object value = await GetDatabase().StringGetAsync(key);
            return value;
        }

        /// 
        /// 实现递增
        /// 
        /// 
        /// 
        public static long Increment(string key)
        {
            key = MergeKey(key);
            //三种命令模式
            //Sync,同步模式会直接阻塞调用者,但是显然不会阻塞其他线程//Async,异步模式直接走的是Task模型。
            //Fire - and - Forget,就是发送命令,然后完全不关心最终什么时候完成命令操作。
            //即发即弃:通过配置 CommandFlags 来实现即发即弃功能,在该实例中该方法会立即返回,如果是string则返回null 如果是int则返回0.这个操作将会继续在后台运行,一个典型的用法页面计数器的实现:
            return GetDatabase().StringIncrement(key, flags: CommandFlags.FireAndForget);
        }

        /// 
        /// 实现递减
        /// 
        /// 
        /// 
        /// 
        public static long Decrement(string key, string value)
        {
            key = MergeKey(key);
            return GetDatabase().HashDecrement(key, value, flags: CommandFlags.FireAndForget);
        }

        /// 
        /// 序列化对象
        /// 
        /// 
        /// 
        private static byte[] Serialize(object o)
        {
            if (o == null)
            {
                return null;
            }
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, o);
                byte[] objectDataAsStream = memoryStream.ToArray();
                return objectDataAsStream;
            }
        }

        /// 
        /// 反序列化对象
        /// 
        /// 
        /// 
        /// 
        private static T Deserialize(byte[] stream)
        {
            if (stream == null)
            {
                return default(T);
            }
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            using (MemoryStream memoryStream = new MemoryStream(stream))
            {
                T result = (T)binaryFormatter.Deserialize(memoryStream);
                return result;
            }
        }

        /// 
        /// 配置更改时
        /// 
        /// 
        /// 
        private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
        {
            //LogHelper.SafeLogMessage("Configuration changed: " + e.EndPoint);
        }

        /// 
        /// 发生错误时
        /// 
        /// 
        /// 
        private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
        {
            //LogHelper.SafeLogMessage("ErrorMessage: " + e.Message);
        }

        /// 
        /// 重新建立连接之前的错误
        /// 
        /// 
        /// 
        private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
        {
            //LogHelper.SafeLogMessage("ConnectionRestored: " + e.EndPoint);
        }

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

        /// 
        /// 更改集群
        /// 
        /// 
        /// 
        private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
        {
            //LogHelper.SafeLogMessage("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
        }

        /// 
        /// redis类库错误
        /// 
        /// 
        /// 
        private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
        {
            //LogHelper.SafeLogMessage("InternalError:Message" + e.Exception.Message);
        }

        //场景不一样,选择的模式便会不一样,大家可以按照自己系统架构情况合理选择长连接还是Lazy。
        //建立连接后,通过调用ConnectionMultiplexer.GetDatabase 方法返回对 Redis Cache 数据库的引用。从 GetDatabase 方法返回的对象是一个轻量级直通对象,不需要进行存储。

        /// 
        /// 使用的是Lazy,在真正需要连接时创建连接。
        /// 延迟加载技术
        /// 微软Azure中的配置 连接模板
        /// 
        //private static Lazy lazyConnection = new Lazy(() =>
        //{
        //    //var options = ConfigurationOptions.Parse(constr);
        //    ////options.ClientName = GetAppName(); // only known at runtime
        //    //options.AllowAdmin = true;
        //    //return ConnectionMultiplexer.Connect(options);
        //    ConnectionMultiplexer muxer = ConnectionMultiplexer.Connect(Coonstr);
        //    muxer.ConnectionFailed += MuxerConnectionFailed;
        //    muxer.ConnectionRestored += MuxerConnectionRestored;
        //    muxer.ErrorMessage += MuxerErrorMessage;
        //    muxer.ConfigurationChanged += MuxerConfigurationChanged;
        //    muxer.HashSlotMoved += MuxerHashSlotMoved;
        //    muxer.InternalError += MuxerInternalError;
        //    return muxer;
        //});


        #region  当作消息代理中间件使用 一般使用更专业的消息队列来处理这种业务场景

        /// 
        /// 当作消息代理中间件使用
        /// 消息组建中,重要的概念便是生产者、消费者、消息中间件。
        /// 
        /// 
        /// 
        /// 
        public static long Publish(string channel, string message)
        {
            ISubscriber sub = Instance.GetSubscriber();
            //return sub.Publish("messages", "hello");
            return sub.Publish(channel, message);
        }

        /// 
        /// 在消费者端得到该消息并输出
        /// 
        /// 
        /// 
        public static void Subscribe(string channelFrom)
        {
            ISubscriber sub = Instance.GetSubscriber();
            sub.Subscribe(channelFrom, (channel, message) =>
            {
                Console.WriteLine(message);
            });
        }

        #endregion

        /// 
        /// GetServer方法会接收一个EndPoint类或者一个唯一标识一台服务器的键值对
        /// 有时候需要为单个服务器指定特定的命令
        /// 使用IServer可以使用所有的shell命令,比如:
        /// DateTime lastSave = server.LastSave();
        /// ClientInfo[] clients = server.ClientList();
        /// 如果报错在连接字符串后加 ,allowAdmin=true;
        /// 
        /// 
        public static IServer GetServer(string host, int port)
        {
            IServer server = Instance.GetServer(host, port);
            return server;
        }

        /// 
        /// 获取全部终结点
        /// 
        /// 
        public static EndPoint[] GetEndPoints()
        {
            EndPoint[] endpoints = Instance.GetEndPoints();
            return endpoints;
        }
    }
View Code

    4、Main窗体代码:

    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();

            try
            {
                string redisconf = "127.0.0.1:6379,passWord=12345,DefaultDatabase=0";
                RedisHelper.SetCon(redisconf);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// 
        /// 设置
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                textBox4.Text = "请填写键";
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                textBox4.Text = "请填写值";
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                textBox4.Text = "请填写过期时间";
                return;
            }

            //键、值、过期时间
            RedisHelper.Set(textBox1.Text, textBox2.Text, int.Parse(textBox3.Text));
            textBox4.Text = "添加成功";
        }

        /// 
        /// 删除
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                if (RedisHelper.Exists(textBox1.Text))
                {
                    RedisHelper.Remove(textBox1.Text).ToString();
                    textBox4.Text = "删除成功";
                }
                else
                {
                    textBox4.Text = "已过期或不存在";
                }
            }
            else
            {
                textBox4.Text = "请填写键";
            }
        }

        /// 
        /// 获取
        /// 
        /// 
        /// 
        private void button3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                if (RedisHelper.Exists(textBox1.Text))
                {
                    textBox4.Text = RedisHelper.Get(textBox1.Text).ToString();
                }
                else
                {
                    textBox4.Text = "已过期或不存在";
                }
            }
            else
            {
                textBox4.Text = "请填写键";
            }
        }

        /// 
        /// 分钟数--非Backspace键或数字时处理
        /// 
        /// 
        /// 
        private void TextBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
View Code

    5、运行结果:

 

    参考自:

    https://blog.csdn.net/wu_zongwen/article/details/80318916

您可能感兴趣的文档:

--结束END--

本文标题: Redis学习笔记:C#中的使用

本文链接: https://www.lsjlt.com/news/4712.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-15
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-15
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-15
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-15
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-15
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-15
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-15
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-15
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-15
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-15
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作