C# redis操作

来源:互联网 发布:coc狂暴法术数据 编辑:程序博客网 时间:2024/05/20 10:10

1、概况

  Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
  Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。

 

2、安装依赖库

 

3、键值的存取

using StackExchange.Redis;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace RedisTest1{    class Program    {        static  void Main(string[] args)        {            var redis = ConnectionMultiplexer.Connect("192.168.1.132");            var rel = "";                        IDatabase db = redis.GetDatabase(1);//redis.GetDatabase(1)指定数据库1;如果不填写则默认为-1            db.StringSet("test", "你好");//把值存入test中            rel = db.StringGet("test");//读取test中的值            var aPending = db.StringGetAsync("test");//异步读取test key            rel = db.Wait(aPending); //等待给定的异步操作完成(或超时)            db.KeyDelete("test");//删除指定的key            Console.WriteLine(rel);//打印test值            HashEntry[] HaskValue = new HashEntry[2];            HaskValue[0] = new HashEntry("A","3");            HaskValue[1] = new HashEntry("B", "4");            db.HashSet("test", HaskValue);//存储哈希值            //获取全部哈希值            HashEntry[] relHash = db.HashGetAll("test");            //打印            for (int i=0;i< relHash.Length;i++) {                Console.WriteLine(string.Format("Name:{0}--Value:{1}", relHash[i].Name, relHash[i].Value));            }                        db.KeyExpire("test", TimeSpan.FromSeconds(2), flags: CommandFlags.FireAndForget);//设置test失效时间            redis.Close();//关闭连接            redis.Dispose();//释放资源            Console.Read();        }    }}

4、事件的订阅

  • 发布端
using StackExchange.Redis;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace RedisPublish{    class Program    {        static  void Main(string[] args)        {            string host = "192.168.1.132";            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);            ISubscriber db = redis.GetSubscriber();//redis.GetDatabase(1)指定数据库1;如果不填写则默认为-1            db.Publish("test", "123");            string reader = "start end";            while (reader != "exit")            {                reader = Console.ReadLine();                db.Publish("test", reader);            }            Console.Read();        }    }}
  • 订阅端
using StackExchange.Redis;using System;namespace RedisSubscribe{    class Program    {        static void Main(string[] args)        {            string host = "192.168.1.132";            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(host);            ISubscriber db = redis.GetSubscriber();            db.Subscribe("test", new Action<RedisChannel, RedisValue>((chan, msage) => {                Console.WriteLine("通道:" + chan);                Console.WriteLine("消息内容:" + msage);            }));            Console.ReadLine();        }    }}

 更多使用请参考官网API:https://stackexchange.github.io/StackExchange.Redis/