zoukankan      html  css  js  c++  java
  • .net core 使用windows版redis

    在项目中为了减少程序占用内存(将结果保存在全局变量里面,会占用内存),要求使用redis。开始了爬坑的过程。o(╥﹏╥)o

    c#操作redis 基本就这3中情况:

    ServiceStack.Redis 是商业版,免费版有限制;如果大量对redis 读写 ,要花钱买(不知道老板批不批o(╥﹏╥)o),说使用3.0版本(回退版本,之前版本不收费),然后发布之后运行会出bug,想死

    StackExchange.Redis 是免费版,但是内核在 .NETCore 运行有问题经常 Timeout,暂无法解决;这个是最坑的,因为本地测试不出任何问题,一发布之后运行,会报很多的bug(访问的是windows版redis,linux不清楚)。

    最后找到了亲爱的这个:CSRedis,完美(不知道为啥,初步安装有问题,卸了重装就好了,我他妈也不知道为什么,你离成功只差一遍重装)

    CSRedis于2016年开始支持.NETCore一直跌代至今,实现了低门槛、高性能,和分区高级玩法的.NETCore redis-cli SDK;

    下面是封装的访问代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Runtime.Serialization.Formatters.Binary;
    
    using System.Collections;
    using System.Runtime.Serialization;
    using Newtonsoft.Json;
    using CSRedis;
    
    namespace DateCore.Utils
    {
        public class CSredisHelper
        {
            //var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=13,poolsize=50,ssl=false,writeBuffer=10240,prefix=key前辍");
    
            private static string connection = Convert.ToString(AppConfigurtaionServices.Configuration["redis:Url"]);
            private static int port = Convert.ToInt32(AppConfigurtaionServices.Configuration["redis:port"]);
    
             private static CSRedisClient redisClient = new CSRedisClient(connection + ":" + port+ "defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=key前辍");
           
    
            /// <summary>
            /// 增加/修改  如果保存的是对象或数组,序列化后保存
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            public bool SetValue<T>(string key, T value)
            {
    
                Type type = typeof(T);
                if (type != typeof(String) && (type.IsClass || type.IsArray))
                {  //String是特殊的引用类型,我也很难受
    
                    var str = JsonConvert.SerializeObject(value);
                    return redisClient.Set(key, str);//, TimeSpan.FromMinutes(1)
                }
                else   //
                {
    
                    return redisClient.Set(key, Convert.ToString(value));
                }
    
    
            }
    
            public bool SetHashValue(string key, string field, string value)
            {
    
                return redisClient.HSet(key, field, value);
    
    
            }
            public string GetHashValue(string key, string field)
            {
                return redisClient.HGet(key, field);
            }
    
    
    
            /// <summary>
            /// 查询  获取对象或数组
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public T GetValue<T>(string key)
            {
                Type type = typeof(T);
                T result = default(T);
                var value = redisClient.Get(key);
                result = JsonConvert.DeserializeObject<T>(value);
                return result;
    
            }
           //泛型获取基本类型,像int,decimal
            public T GetStringValue<T>(string key)
            {
    
                return redisClient.Get<T>(key);
            }
    
            /// <summary>
            /// 释放资源
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
    
            public void dispose()
            {
            
                redisClient.Dispose();
                
            }
           ///删除所有分区的所有数据库数据
            public void Deleteall()
            {
                redisClient.NodesServerManager.FlushAll();
            }
        }
    }
    

      访问方法:

      

    namespace DateCore.Repository
    {
        public class BeginT : IBeginT
        {  private static CSredisHelper Redis = new CSredisHelper();
             public void Test()
            {
                 
                Redis.SetValue<string>("TruckDownSchedule", "test");
                Redis.SetValue<int>("int", 12);
                Redis.GetStringValue<string>("TruckDownSchedule");
                Redis.GetStringValue<int>("int");
    
             }
      }
    }

    具体使用方法请参考以下路径:https://github.com/2881099/csredis  

    参考文档:http://www.cnblogs.com/kellynic/p/9803314.html

    只是因为分享而传播,我因为您的点赞而快乐。

  • 相关阅读:
    ECharts图形库
    python_flask 注册,登陆,退出思路 ---纯个人观点
    python基础-类的继承
    python基础-面向对象
    python基础-大杂烩
    python基础-异常处理
    python基础-文本操作
    python基础-模块
    python基础-函数
    python基础-字典
  • 原文地址:https://www.cnblogs.com/fishyues/p/9915242.html
Copyright © 2011-2022 走看看