C#进行Redis操作

2022-05-07 14:36:15 浏览数 (1)

由于一些程序是用.net来写的,刚好要访问以前的redis库,于是就找一些redis的.net客户端。

在redis主页上,提供了好多个利用.net封装redis的库:

csredis

Async (and sync) client for Redis and Sentinel

Nhiredis

A lightweight wrapper around the C client hiredis.

redis-sharp

redisboost

Thread-safe async Redis client. Offers high performance and simple api

ServiceStack.Redis

This is a fork and improvement of the original C# client written by Miguel De Icaza.

Sider

Minimalistic client for C#/.NET 4.0

StackExchange.Redis

This .NET client was developed by Stack Exchange for very high performance needs (replacement to the earlier BookSleeve).

TeamDev Redis Client

Redis Client is based on redis-sharp for the basic communication functions, but it offers some differences.

看了下各个的比较,最终选择排名第一的csredis。一看例子,非常简单,就它了。

下载后编译一测试,果然代码非常简洁易读。

如进行如下代码的执行:

 private void SetRediskey()         {             using (var redis = new RedisClient("localhost"))             {                 string ping = redis.Ping();                 string echo = redis.Echo("hello world");                 DateTime time = redis.Time();                 Console.WriteLine(ping);                 Console.WriteLine(echo);                 Console.WriteLine(time);                 redis.StartPipeTransaction();                 redis.Set("key", "value");                 redis.Set("key2", "value2");                 object[] result2 = redis.EndPipe(); // transaction is EXEC'd automatically if DISCARD was not called first                 foreach (object c in result2)                     Console.WriteLine("redis result:" c.ToString());             }         }

输出结果为:

PONG hello world 04/16/2016 02:31:31 redis result:OK redis result:OK

而在redis端进行查询,效果如下:非常简单。

0 人点赞