首页 > 数据库 >各种redis的介绍:ServiceStack.Redis,StackExchange.Redis,CSRedis

各种redis的介绍:ServiceStack.Redis,StackExchange.Redis,CSRedis

时间:2023-01-13 09:57:24浏览次数:39  
标签:Console string Redis ServiceStack redis list CsRedisHelper WriteLine public

1.ServiceStack.Redis 是商业版,免费版有限制;ServiceStack.Redis每小时6000次限制,ServiceStack 4.0 开始已经成为商业产品,不再完全免费,好在是开源的.

2.StackExchange.Redis 是免费版,但是内核在 .NETCore 运行有问题经常 Timeout,暂无法解决;

3.CSRedis于2016年开始支持.NETCore一直迭代至今,实现了低门槛、高性能,和分区高级玩法的.NETCore redis-cli SDK;

 

1. 通过包管理器安装CSRedis
2. 实例化csredis,并继承icasheservice
3. DBContent 里实例化csredisClient
4. TestRedisCache


public class TestRedisCache: ICacheService
{
/// <summary>
/// redis
/// </summary>
public CSRedisClient rds;

//redis cach second,default 120,read appsetting.json
int _expirySeconds = 120;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="connectionStrings"></param>
public TestRedisCache(string[] connectionStrings, int expirySeconds)
{
this.rds = new CSRedis.CSRedisClient((p => { return null; }), connectionStrings);
_expirySeconds = expirySeconds;
}

public void Add<V>(string key, V value)
{
this.rds.Set(key, value);

}

public void Add<V>(string key, V value, int cacheDurationInSeconds)
{
if (cacheDurationInSeconds >= 2147483647)
{
cacheDurationInSeconds = _expirySeconds;
}
this.rds.Set(key, value, cacheDurationInSeconds);
}

public bool ContainsKey<V>(string key)
{
return this.rds.Exists(key);
}

public V Get<V>(string key)
{
return this.rds.Get<V>(key);
}

public IEnumerable<string> GetAllKey<V>()
{
return this.rds.Keys("*");
}

public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
{
if (this.ContainsKey<V>(cacheKey))
{
return this.Get<V>(cacheKey);
}
else
{
var result = create();
this.Add(cacheKey, result, cacheDurationInSeconds);
return result;
}
}

public void Remove<V>(string key)
{
this.rds.Del(key);
}
public void DisposeRedis()
{
this.rds.Dispose();
}
}
}


5. DBContext

public DBContext()
{
//get mysql connnection str
var salveCount = configuration.GetSection("ConnectionStrings:SlaveCount").Value.ObjToInt();
List<SlaveConnectionConfig> slaves = new List<SlaveConnectionConfig>();
for (int i = 0; i < salveCount; i++)
{
slaves.Add(new SlaveConnectionConfig
{
ConnectionString = configuration[$"ConnectionStrings:Slave:{i}:ConnectionString"], HitRate = configuration[$"ConnectionStrings:Slave:{i}:HitRate"].ObjToInt()
});
}

//redis db default expiration Second
if (!string.IsNullOrEmpty(configuration["RedisString:DurationSecond"]))
{ CachExpirationSecond = int.Parse(configuration["RedisString:DurationSecond"]); }

string redisconn = configuration["RedisString:ConnectionHost"];

if (RedisDB == null)
{
RedisDB = new ExamRedisCache(new string[] { redisconn }, CachExpirationSecond);
}


//mysql db object
if (DB == null)
{
DB = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = configuration["ConnectionStrings:Master"],
DbType = DbType.MySql,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
SlaveConnectionConfigs = slaves,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = RedisDB
}
});
}

//调式代码 用来打印SQL
DB.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
DB.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
}

 

-----------------csRedis-------------

void Test()
{

CsRedisHelper.Set("name", "祝雷");//设置值。默认永不过期
//aaaaa.SetAsync("name", "祝雷");//异步操作
Console.WriteLine(CsRedisHelper.Get<String>("name"));

CsRedisHelper.Set("time", DateTime.Now, 1);
Console.WriteLine(CsRedisHelper.Get<DateTime>("time"));

Console.WriteLine(CsRedisHelper.Get<DateTime>("time"));

// 列表
CsRedisHelper.RPush("list", "第一个元素");
CsRedisHelper.RPush("list", "第二个元素");
CsRedisHelper.LInsertBefore("list", "第二个元素", "我是新插入的第二个元素!");
Console.WriteLine($"list的长度为{CsRedisHelper.LLen("list")}");
//Console.WriteLine($"list的长度为{aaaaa.LLenAsync("list")}");//异步
Console.WriteLine($"list的第二个元素为{CsRedisHelper.LIndex("list", 1)}");
//Console.WriteLine($"list的第二个元素为{aaaaa.LIndexAsync("list",1)}");//异步
// 哈希
CsRedisHelper.HSet("person", "name", "zhulei");
CsRedisHelper.HSet("person", "sex", "男");
CsRedisHelper.HSet("person", "age", "28");
CsRedisHelper.HSet("person", "adress", "hefei");
Console.WriteLine($"person这个哈希中的age为{CsRedisHelper.HGet<int>("person", "age")}");
//Console.WriteLine($"person这个哈希中的age为{aaaaa.HGetAsync<int>("person", "age")}");//异步


// 集合
CsRedisHelper.SAdd("students", "zhangsan", "lisi");
CsRedisHelper.SAdd("students", "wangwu");
CsRedisHelper.SAdd("students", "zhaoliu");
Console.WriteLine($"students这个集合的大小为{CsRedisHelper.SCard("students")}");
Console.WriteLine($"students这个集合是否包含wagnwu:{CsRedisHelper.SIsMember("students", "wangwu")}");

//普通订阅
RedisHelper.Subscribe(
("chan1", msg => Console.WriteLine(msg.Body)),
("chan2", msg => Console.WriteLine(msg.Body)));

//模式订阅(通配符)
RedisHelper.PSubscribe(new[] { "test*", "*test001", "test*002" }, msg => {
Console.WriteLine($"PSUB {msg.MessageId}:{msg.Body} {msg.Pattern}: chan:{msg.Channel}");
});
//模式订阅已经解决的难题:
//1、分区的节点匹配规则,导致通配符最大可能匹配全部节点,所以全部节点都要订阅
//2、本组 "test*", "*test001", "test*002" 订阅全部节点时,需要解决同一条消息不可执行多次

//发布
RedisHelper.Publish("chan1", "123123123");
//无论是分区或普通模式,RedisHelper.Publish 都可以正常通信

//不加缓存的时候,要从数据库查询
//var t1 = Test.Select.WhereId(1).ToOne();

//一般的缓存代码,如不封装还挺繁琐的
var cacheValue = RedisHelper.Get("test1");
if (!string.IsNullOrEmpty(cacheValue))
{
//try
//{
// return JsonConvert.DeserializeObject(cacheValue);
//}
//catch
//{
// //出错时删除key
// RedisHelper.Del("test1");
// throw;
//}
}
//var t1 = Test.Select.WhereId(1).ToOne();
// RedisHelper.Set("test1", JsonConvert.SerializeObject(t1), 10); //缓存10秒

//使用缓存壳效果同上,以下示例使用 string 和 hash 缓存数据
//var t1 = RedisHelper.CacheShell("test1", 10, () => Test.Select.WhereId(1).ToOne());
//var t2 = RedisHelper.CacheShell("test", "1", 10, () => Test.Select.WhereId(1).ToOne());
//var t3 = RedisHelper.CacheShell("test", new[] { "1", "2" }, 10, notCacheFields => new[] {
// ("1", Test.Select.WhereId(1).ToOne()),
// ("2", Test.Select.WhereId(2).ToOne())
// });
}

 

原文出处 https://www.cnblogs.com/csj007523/p/13751390.html

标签:Console,string,Redis,ServiceStack,redis,list,CsRedisHelper,WriteLine,public
From: https://www.cnblogs.com/nnnnnn/p/17048622.html

相关文章

  • 华为云数据库GaussDB(for Redis),如何为人们日常生活保驾护航​
    互联网世界就是数据世界,数据的来源有很多,比如出行记录、消费记录、浏览的网页、发送的消息等等。除了文本类型的数据,图像、音乐、声音都是数据。​随着新时代下数字化转型加......
  • Redis 6 学习笔记1 ——NoSQL数据库介绍,Redis常用数据类型
    NoSQL数据库介绍(了解)技术的分类1、解决功能性的问题:Java、Jsp、RDBMS、Tomcat、HTML、Linux、JDBC、SVN,2、进一步地,解决系统功能扩展性的问题:Struts、Spring、SpringMVC......
  • Redis-独立功能-排序
    排序Redis的SORT命令可以对列表键、集合键或者有序集合键的值进行排序。如果按照选项来划分的话,一个SORT命令的执行过程可以分为以下几步:1)排序:在这一步,命令会使用ALPHA......
  • Redis-独立功能-事务
    事务事务的实现Redis通过MULTI、EXEC、WATCH等命令来实现事务(transaction)功能。事务提供了一种将多个命令请求打包,然后一次性、按顺序地执行多个命令的机制,并且在事务执......
  • 使用redis实现自增序列
    来源:https://www.cnblogs.com/shigj/p/11215262.htmlpublicclassTestService{ @Resource RedisTemplate<String,Object>redisTemplate; publicStringgetI......
  • Redis-独立功能-发布与订阅
    发布与订阅Redis的发布与订阅功能由PUBLISH、SUBSCRIBE、PSUBSCRIBE等命令组成。通过执行SUBSCRIBE命令,客户端可以订阅一个或多个频道,从而成为这些频道的订阅者(subscribe......
  • 【一句话】Redis的3中缓存策略
    首先一句话:旁路缓存模式策略:写->写DB,删缓存,读->读cache,没有则读DB,然后更新到缓存读写穿透策略:写->写缓存,然后由缓存系统写DB,读->读cache,没有则将DB写入cache,再读cache异......
  • Redis-多机数据库-集群
    集群Redis集群是Redis提供的分布式数据库方案,集群通过分片(sharding)来进行数据共享,并提供复制和故障转移功能。Redis服务器在启动时会根据cluster-enabled配置选项是否为y......
  • redis命令清理key
    ###手动清除单个key:redis-cli-h'ipaddr'-p6379-a'passwd'-n0keys'keyName'|xargsredis-cli-h'ipaddr'-p6379-a'passwd'-n0del;或者手动清除单个k......
  • RedisUtil以及可放入Map和List格式的配置
    RedisUtilpackagecom.diit.onemap.common.utils;importorg.springframework.data.redis.core.*;importorg.springframework.stereotype.Service;importjavax.ann......