• .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

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

  • 相关阅读:
    68、成员列表初始化?
    67、类成员初始化方式?构造函数的执行顺序 ?为什么用成员初始化列表会快一 些?
    64、malloc申请的存储空间能用delete释放吗?
    63、new和delete的实现原理, delete是如何知道释放内存的大小的额?
    62、delete p、delete [] p、allocator都有什么作用?
    60、C++模板是什么,你知道底层怎么实现的?
    nyoj--814--又见拦截导弹(动态规划+贪心)
    hdoj--1950--Bridging signals(二分查找+LIS)
    nyoj--214--单调递增子序列(二)(二分查找+LIS)
    hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
  • 原文地址:https://www.cnblogs.com/fishyues/p/9915242.html
Copyright © 2020-2023  润新知