• RedisUtil(未完,持续更新中....)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    using StackExchange.Redis;
    
    namespace Common.Util
    {
      public  class RedisUtil
        {
            #region init
            private static object lockForRedis = new object();//连接实例锁
            private static ConnectionMultiplexer Instance;  //连接实例
            /// <summary>
            /// 默认开始就给连接
            /// </summary>
            static RedisUtil()
            {
               GetConn();   //--初始就实例化
            }
            /// <summary>
            /// 获取连接
            /// </summary>
            /// <returns></returns>
            private static ConnectionMultiplexer GetConn()
            {
                if (Instance == null)
                {
                    lock (lockForRedis)
                    {
                        if (Instance == null || !Instance.IsConnected)
                        {
                            Instance = ConnectionMultiplexer.Connect(GetConfig());
                           
                        }
                    }
                }
                return Instance;
            }
            /// <summary>
            /// 获取配置
            /// </summary>
            /// <returns></returns>
            private static ConfigurationOptions GetConfig()
            {
                ConfigurationOptions options = new ConfigurationOptions();
               
                
                options.EndPoints.Add("localhost",6379);
                options.ConnectTimeout = 6000;  //超时时间设置(默认5000)
                options.ConnectRetry = 1;       //连接重试周期
                return options;
            }
            #endregion
            #region String
            /// <summary>
            /// 设置String值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="timeSpan"></param>
            /// <param name="dbIndex"></param>
            /// <returns></returns>
            public static bool StringSet<T>(string key,T value,TimeSpan? timeSpan=null,int dbIndex=0)
            {
                try
                {
                    var db = GetConn().GetDatabase(dbIndex);
                    return db.StringSet(key, JsonConvert.SerializeObject(value),timeSpan);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return false;
                }
    
               
            }
            /// <summary>
            /// 获取String值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <param name="dbIndex"></param>
            /// <returns></returns>
            public static T StringGet<T>(string key,int dbIndex=0)
            {  
                try
                {
                    var db = GetConn().GetDatabase(dbIndex);
                    return JsonConvert.DeserializeObject<T>(db.StringGet(key));
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                    return default(T);
                }
                
            }
            /// <summary>
            /// 存入递增类型的键值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="dbIndex"></param>
            /// <returns></returns>
            public static double StringIncrement(string key, double value, int dbIndex = 0)
            {
                try
                {
                    var db = GetConn().GetDatabase(dbIndex);
                    return db.StringIncrement(key,value);
                }
                catch (Exception ex)
                {
    
                    return 0;
                }
            }
            /// <summary>
            /// 存入递减类型的键值
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="dbIndex"></param>
            /// <returns></returns>
            public static double StringDecrement(string key,double value,int dbIndex = 0)
            {
                try
                {
                    var db = GetConn().GetDatabase(dbIndex);
                    return db.StringDecrement(key, value);
                }
                catch (Exception ex)
                {
    
                    return 0;
                }
            }
            #endregion
        }
    }
    

      

    力争写最通俗易懂的文章,不添加任何防腐剂~~~
  • 相关阅读:
    根据EsLint配置WebStorm格式化代码风格
    Vue中使用vant-UI实现移动端自定义省市区三级联动
    Vue中使用Element-UI实现表格跨页多选
    Vue中使用iview-UI实现切换Tab页网络请求优化
    Vue中使用iview-UI按需引入Select组件下拉框无法生效问题
    Vue中使用iview-UI表格样式修改和使用自定义模板数据渲染相关
    Vue中使用Element-UI表单验证相关问题及解决
    Vue 3.0 多页面项目之商家平台练习
    五 创建道路模型(2 道路的挖填方量计算及条件部件)
    五 创建道路模型(1 道路三要素)
  • 原文地址:https://www.cnblogs.com/diligent-lsh/p/9939412.html
Copyright © 2020-2023  润新知