StackExchange是一个优秀的c# redis客户端,但是存在操作略为繁琐的弊端,为了简化操作,使用 StackExchange.Redis.Extensions成为了一个非常值得推荐的选择。它能让使用StackExchange变得相当简单。

StackExchange.Redis.Extensions github地址:https://github.com/imperugo/StackExchange.Redis.Extensions

第一步用nuget安装相关包:

PM> Install-Package StackExchange.Redis.Extensions.Newtonsoft

完成后即可使用。

以下将用一个多线程修改某一个值的控制台案例来作为演示。

using System;
using System.Threading;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Core.Configuration;
using StackExchange.Redis.Extensions.Newtonsoft; namespace Redis
{
class Program
{
public static void Ins()
{
Thread thread = new Thread(() =>
{
for (int i = ; i < ; i++)
{
client.Database.StringIncrement("key");
}
});
thread.Start();
} private static StackExchangeRedisCacheClient client; static void Main(string[] args)
{
var redisConfiguration = new RedisConfiguration() //配置
{
Hosts = new RedisHost[]
{
new RedisHost(){Host = "127.0.0.1",Port = }
}
}; client = new StackExchangeRedisCacheClient(new NewtonsoftSerializer(),redisConfiguration );
client.Add("key", ); for (int j = ; j < ; j++)
{
Ins();
} Thread.Sleep(); int i = client.Get<int>("key");
Console.WriteLine(i);
Console.ReadKey();
}
} }

值得注意的是,如果要把相关配置写进app.config或者web.config,需要另外再安装一个包:StackExchange.Redis.Extensions.LegacyConfiguration

Install-Package StackExchange.Redis.Extensions.LegacyConfiguration

然后配置文件就可以写相关配置了:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- 注意,configSections必须是configuration节点的第一个子元素-->
<configSections>
<section name="redisCacheClient" type="StackExchange.Redis.Extensions.LegacyConfiguration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.LegacyConfiguration" />
</configSections> <redisCacheClient allowAdmin="true" ssl="false" connectTimeout="3000" database="24">
<serverEnumerationStrategy mode="Single" targetRole="PreferSlave" unreachableServerAction="IgnoreIfOtherAvailable" />
<hosts>
<add host="127.0.0.1" cachePort="6379" />
</hosts>
</redisCacheClient> </configuration>

然后在代码里用RedisCachingSectionHandler.GetConfig()获得配置:

var config = RedisCachingSectionHandler.GetConfig();

client = new StackExchangeRedisCacheClient(new NewtonsoftSerializer(),config );

c# redis 操作类库推荐:StackExchange.Redis.Extensions的更多相关文章

  1. c# redis 利用锁(StackExchange.Redis LockTake)来保证数据在高并发情况下的正确性

    之前有写过一篇介绍c#操作redis的文章 http://www.cnblogs.com/axel10/p/8459434.html ,这篇文章中的案例使用了StringIncrement来实现了高并 ...

  2. RedisRepository封装—Redis发布订阅以及StackExchange.Redis中的使用

    本文版权归博客园和作者本人吴双共同所有,转载请注明本Redis系列分享地址.http://www.cnblogs.com/tdws/tag/NoSql/ Redis Pub/Sub模式 基本介绍 Re ...

  3. Redis 详解 (一) StackExchange.Redis Client

    这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...

  4. Redis项目实战 .net StackExchange.Redis

    StackExchange.Redis 免费.支持异步.用的最多 常用对象 源码地址:https://github.com/StackExchange/StackExchange.Redis    用 ...

  5. .NET平台下Redis使用(二)【StackExchange.Redis学习】

    Program.cs内容: using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Data; usi ...

  6. Redis报错: StackExchange.Redis.RedisServerException: Endpoint 39.105.22.111:7200 serving hashslot 12448 is not reachable at this point of time.

    emmmm……要下班了,简单记录一下. 如果是127.0.0.1:7200报这个错,请移步 https://blog.csdn.net/foreverhot1019/article/details/7 ...

  7. 负载均衡@StackExchange.Redis实现Session外置--纯干货喂饱你

    Redis和StackExchange.Redis redis有多个数据库1.redis 中的每一个数据库,都由一个 redisDb 的结构存储.其中,redisDb.id 存储着 redis 数据库 ...

  8. c#使用 StackExchange.Redis 封装 RedisHelper

    公司一直在用.net自带的缓存,大家都知道.net自带缓存的缺点,就不多说了,不知道的可以查一查,领导最近在说分布式缓存,我们选的是redis,领导让我不忙的时候封装一下,搜索了两天,选了选第三方的插 ...

  9. 使用StackExchange.Redis客户端进行Redis访问出现的Timeout异常排查

    问题产生 这两天业务系统在redis的使用过程中,当并行客户端数量达到200+之后,产生了大量timeout异常,典型的异常信息如下: Timeout performing HVALS Parser2 ...

随机推荐

  1. 表的操作(Oracle和DB2)

    asc和desc 分别表示升序和降序 select * from tablename order by id desc :根据id字段按照降序排列,从大到小 select * from tablena ...

  2. CentOS下安装配置cmake

    安装环境:CentOS-6.4   安装方式:源码编译安装  软件:cmake-2.8.5.tar.gz 下载地址暂时不提供,去百度搜一下准有 安装前提 系统中已经安装了gcc. ncurses-de ...

  3. C. Vasya and String

    原题链接 C. Vasya and String High school student Vasya got a string of length n as a birthday present. T ...

  4. Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等

    文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常

  5. 吾八哥学Selenium(四):操作下拉框select标签的方法

    我们在做web页面自动化测试的时候会经常遇到<select></select>标签的下拉框,那么在Python里如何实现去操作这种控件呢?今天就给大家分享一下这个玩法.为了让大 ...

  6. R语言数据框中,用0替代NA缺失值

    1.用0替代数据框中的缺失值NA 生成数据框: > m <- matrix(sample(c(NA, :), , replace = TRUE), ) > d <- as.da ...

  7. SELinux一键开启与禁用脚本

    SELinux是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统.但是SELinux的并不能与众多服务很好的兼容,有些人会关闭SELinux一了百了.在日常的运 ...

  8. Mybatis认识

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...

  9. jstree树形菜单

    final 用于声明属性.方法和类,分别表示属性不可变,方法不可重写,类不可继承.其实可以参考用easyui的tree 和 ztree参考: https://www.jstree.com/demo/ ...

  10. Centos定时启动和清除任务

    因为需要定时并发执行任务,所以查到了crontab这个工具,介绍一下其用法: SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=roo ...