BeetleX.Redis
BeetleX.Redis
是基于BeetleX
扩展的Redis
操作库,全异步操作模式和基于字符缓存策略优化可以提供出色的Redis访问性能;支持SSL,支持读写分离和故障切换;默认多数据格式支持,默认提供json
,protobuf
和messagepack
格式。
支持的命令
AUTH
BLPOP
BRPOP
BRPOPLPUSH
DECR
DECRBY
DEL
DUMP
EXISTS
EXPIRE
EXPIREAT
FLUSHALL
GET
GETBIT
GETRANGE
GETSET
HDEL
HEXISTS
HGET
HGETALL
HINCRBY
HINCRBYFLOAT
HKEYS
HLEN
HMGET
HMSET
HSET
HSETNX
HSTRLEN
HVALS
INCR
INCRBY
INCRBYFLOAT
KEYS
LINDEX
LINSERT
LLEN
LPOP
LPUSH
LPUSHX
LRANGE
LREM
LSET
LTRIM
MGET
MOVE
MSET
MSETNX
OBJECT
PERSIST
PEXPIRE
PEXPIREAT
PING
PSETEX
PTTL
PUBLISH
RANDOMKEY
RENAME
RENAMENX
RPOP
RPOPLPUSH
RPUSH
RPUSHX
SELECT
SET
SETBIT
SETEX
SETNX
SETRANGE
STRLEN
SUBSCRIBE
TOUCH
TTL
TYPE
UNLINK
UNSUBSCRIBE
WAIT
ZADD
ZCARD
ZCOUNT
ZINCRBY
ZINTERSTORE
ZLEXCOUNT
ZRANGE
ZRANGEBYLEX
ZRANGEBYSCORE
ZRANK
ZREM
ZREMRANGEBYLEX
ZREMRANGEBYRANK
ZREMRANGEBYSCORE
ZREVRANGE
ZREVRANGEBYSCORE
ZREVRANK
ZSCORE
ZUNIONSTORE
GitHub
https://github.com/IKende/BeetleX.Redis
Nuget
https://www.nuget.org/packages/BeetleX.Redis/
设置
组件默认提供一个0
库的实例,只需要添加相应的Redis
服务地址即可
Redis.Default.DataFormater = new JsonFormater();Redis.Default.Host.AddWriteHost("localhost");//开启SSLRedis.Default.Host.AddWriteHost("localhost",6378,true);//设置密码Redis.Default.Host.AddWriteHost("localhost").Password="123456"
创建一个数据实例
RedisDB DB = new RedisDB(1);
DB.DataFormater = new JsonFormater();
DB.Host.AddWriteHost("localhost");//开启SSL
DB.Host.AddWriteHost("localhost",6378,true);//设置密码
DB.Host.AddWriteHost("localhost").Password="123456"
每个库只创建一个实例即可,相关所有操作都是线程安全。
Json db
RedisDB DB = new RedisDB(0, new JsonFormater());
Protobuf db
RedisDB DB = new RedisDB(0, new ProtobufFormater());
MessagePack db
RedisDB DB = new RedisDB(0, new MessagePackFormater());
SET/SET操作
await Redis.Get<Employee>("nonexisting");
await Redis.Set("emp3", GetEmployee(3));
await Redis.Get<Employee>("emp3");
MSET/MGET操作
await Redis.Set(("field1", GetEmployee(1)), ("field2", GetEmployee(2)));
await Redis.Get<Employee, Order, Customer>("emp1", "order1", "customer1");
列表
var list = Redis.CreateList<Employee>("employees");
await list.Push(GetEmployee(1));
await list.Insert(true, GetEmployee(2), GetEmployee(3));
await list.Range(0, -1);
有序集合
var sequeue = DB.CreateSequence("seq2");
await sequeue.ZAdd((100, "A1"), (200, "A2"), (300, "A3"), (400, "A4"));
var items = await sequeue.ZRange(0, -1, true);
键值表
var table = Redis.CreateHashTable("myhash");
await table.MSet(("field1", GetEmployee(1)), ("field2", GetEmployee(2)));
await table.Get<Employee, Employee>("field1", "field2");
await table.Del("emp2");
await table.Keys();
订阅操作
var subscribe = Redis.Subscribe();
subscribe.Register<Employee>("employees");
subscribe.Receive = (o, e) =>
{Console.WriteLine($"{e.Type} {e.Channel} {e.Data}");
};
subscribe.Listen();
发布
await Redis.Publish("employees", GetEmployee(1));