码迷,mamicode.com
首页 > Windows程序 > 详细

C# Redis 帮助类

时间:2021-01-18 11:16:13      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:param   str   stat   exe   options   returns   text   int   alt   

 

总结下,后期会陆续更新

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StackExchange.Redis;
using System.Threading;
using System.Data;
using System.Configuration;

namespace WebCommon
{
    public class RedisHelper
    {
        private static string connectStr = ConfigurationManager.AppSettings["RedisServer"];
        private static ConfigurationOptions configurationOptions = ConfigurationOptions.Parse(connectStr);
        //the lock for singleton
        private static readonly object Locker = new object();
        //singleton
        private static ConnectionMultiplexer redisConn;

        /// <summary>
        /// 连接redis
        /// </summary>
        /// <returns></returns>
        public static ConnectionMultiplexer getRedisConn()
        {
            if (redisConn == null)
            {
                lock (Locker)
                {
                    if (redisConn == null || !redisConn.IsConnected)
                    {
                        redisConn = ConnectionMultiplexer.Connect(configurationOptions);
                    }
                }
            }
            return redisConn;
        }

        #region Redis 字符串操作

        /// <summary>
        ///      设置一个key的值
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="strValue"></param>
        public void InsertStr(string strKey, string strValue)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            //set get
            bool setResult = db.StringSet(strKey, strValue);
        }
        /// <summary>
        ///    设置一个key的值并设定过期时间
        /// </summary>
        /// <param name="strKey">key</param>
        /// <param name="strValue">values</param>
        /// <param name="expirytime">过期时间</param>
        /// <param name="databasenum"></param>
        public void InsertStr(string strKey, string strValue, DateTime expirytime, int databasenum)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase(databasenum);
            bool setResult = db.StringSet(strKey, strValue);
            db.KeyExpire(strKey, expirytime.ToUniversalTime());
        }

        
        /// <summary>
        /// 读取一个key值
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public string ReadStr(string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            return db.StringGet(strKey);
        }

        /// <summary>
        /// 删除一个key值
        /// </summary>
        /// <param name="strKey"></param>
        public void DeleteStr(string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            //set get
            bool setResult = db.KeyDelete(strKey);
        }
        /// <summary>
        /// 删除一个key值
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="databasenum"></param>
        /// <returns></returns>
        public bool DeleteStr(string strKey, int databasenum)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase(databasenum);
            return db.KeyDelete(strKey);
        }

        /// <summary>
        ///  判断key是否存在
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public bool Exist(string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            return db.KeyExists(strKey);
        }
        #endregion

        #region Redis哈希操作 

        
        /// <summary>
        /// 添加一个key值
        /// </summary>
        /// <param name="tablename"></param>
        /// <param name="strKey"></param>
        /// <param name="strValue"></param>
        public void InsertHash(string tablename, string strKey, string strValue)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();

            //set get
            bool setResult = db.HashSet(tablename, strKey, strValue);
        }

        /// <summary>
        /// 读取一个key值
        /// </summary>
        /// <param name="tablename"></param>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public string ReadHash(string tablename, string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            
            return db.HashGet(tablename, strKey);
        }

        /// <summary>
        /// 判断一个key值是否存在
        /// </summary>
        /// <param name="tablename"></param>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public bool ExistHash(string tablename, string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            return db.HashExists(tablename, strKey);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tablename"></param>
        /// <param name="strKey"></param>
        public void DeleteHash(string tablename, string strKey)
        {
            redisConn = getRedisConn();
            var db = redisConn.GetDatabase();
            //set get
            bool setResult = db.HashDelete(tablename,strKey);
        } 

        #endregion

    }
}

 

C# Redis 帮助类

标签:param   str   stat   exe   options   returns   text   int   alt   

原文地址:https://www.cnblogs.com/for917157ever/p/14288307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!