博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net core 使用windows版redis
阅读量:4317 次
发布时间:2019-06-06

本文共 3625 字,大约阅读时间需要 12 分钟。

在项目中为了减少程序占用内存(将结果保存在全局变量里面,会占用内存),要求使用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前辍");               ///         /// 增加/修改  如果保存的是对象或数组,序列化后保存        ///         ///         ///         /// 
public bool SetValue
(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); } ///
/// 查询 获取对象或数组 /// ///
///
public T GetValue
(string key) { Type type = typeof(T); T result = default(T); var value = redisClient.Get(key); result = JsonConvert.DeserializeObject
(value); return result; } //泛型获取基本类型,像int,decimal public T GetStringValue
(string key) { return redisClient.Get
(key); } ///
/// 释放资源 /// ///
///
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
("TruckDownSchedule", "test"); Redis.SetValue
("int", 12); Redis.GetStringValue
("TruckDownSchedule"); Redis.GetStringValue
("int"); } }}

具体使用方法请参考以下路径:  

参考文档:

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

转载于:https://www.cnblogs.com/fishyues/p/9915242.html

你可能感兴趣的文章
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
LINQ to SQL vs. NHibernate
查看>>
基于Angular5和WebAPI的增删改查(一)
查看>>
windows 10 & Office 2016 安装
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
英特尔公司将停止910GL、915GL和915PL芯片组的生产
查看>>
团队编程项目作业2-团队编程项目开发环境搭建过程
查看>>
Stax解析XML示例代码
查看>>
cookie
查看>>
二级图片导航菜单
查看>>
<Using parquet with impala>
查看>>
OpenGL渲染流程
查看>>
委托异步回调
查看>>
扩展欧几里得算法
查看>>