项目创建
为了测试方便,使用控制台应用程序即可。
这里创建.NET Core 3.1(长期支持)
可以看到,非常干净
StackExchange.Redis操作示例
打开NuGet包管理器,搜索【StackExchange.Redis】,本示例使用的是2.6.66版本
引包【using StackExchange.Redis;】
ConnectionMultiplexer
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:6379");
IDatabase db = conn.GetDatabase();
db.StringSet("userName","admin");
RedisValue redisValue = db.StringGet("userName");
Console.WriteLine(redisValue);
}
输出结果,可以看到admin是咱们拿到的值。
RedisDBHelper
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
namespace demo_20221010_redis
class RedisDBHelper
private ConnectionMultiplexer redis { get; set; }
public IDatabase db { get; set; }
/// <summary>
/// 其实有个链接就行,一般都是默认本地。如果不是就再传
/// </summary>
/// <param name="connection"></param>
public RedisDBHelper(string connection)
redis = ConnectionMultiplexer.Connect(connection);
db = redis.GetDatabase();
}
使用RedisDBHelper测试
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
bool isf = redis.db.StringSet("userName","admin");
if (isf)
RedisValue redisValue = redis.db.StringGet("userName");
Console.WriteLine(redisValue.ToString());
else {
Console.WriteLine("写入失败");
}
有存储时间的String
这里输入的时间必须是TimeSpan类型。故而使用TimeSpan.FromSeconds(double);来输入时间。
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
bool isf = redis.db.StringSet("haveTime","我是存储时间",TimeSpan.FromSeconds(120));
if (isf) {
Console.WriteLine("存储值:"+redis.db.StringGet("haveTime").ToString()) ;
Console.WriteLine("剩余时间:"+redis.db.KeyTimeToLive("haveTime"));
}
Redis删除Key:KeyDelete
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
bool isf = redis.db.StringSet("haveTime","我是存储时间",TimeSpan.FromSeconds(120));
if (isf) {
//删除后再查询,肯定是空
redis.db.KeyDelete("havaTime");
Console.WriteLine("是否有值:"+redis.db.StringGet("havaTime"));
}
没有值就是正确的,这个删除可以删除任何的可以,都是通用的。
Redis的List操作
向左添加,其中ListRange是读取。
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mylist");
RedisValue[] arr = new RedisValue[10];
for (int i = 0; i < arr.Length; i++)
arr[i]= new RedisValue("正确"+i);
long isf = redis.db.ListLeftPush("mylist",arr);
RedisValue[] list = redis.db.ListRange("mylist");
foreach (var item in list)
Console.WriteLine(item);
}
向右添加
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mylist");
RedisValue[] arr = new RedisValue[10];
for (int i = 0; i < arr.Length; i++)
arr[i]= new RedisValue("正确"+i);
long isf = redis.db.ListRightPush("mylist",arr);
RedisValue[] list = redis.db.ListRange("mylist");
foreach (var item in list)
Console.WriteLine(item);
}
redis list范围查询
一般的时候list存储的值都是成千上万的,甚至几十上百万,所以我们需要好好处理一下。一般的情况下也就获取最后一千条左右的数据,如果都返回去了不就能让别人推算出后面的大致数据走向了吗?
这里我专门匹配的写了RedisValue,其实写字符串数组也行的。
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mylist");
RedisValue[] arr = new RedisValue[10];
for (int i = 0; i < arr.Length; i++)
arr[i] = new RedisValue("正确" + i);
long isf = redis.db.ListRightPush("mylist", arr);
RedisValue[] list = redis.db.ListRange("mylist",0,5);
foreach (var item in list)
Console.WriteLine(item);
}
redis HashSet操作
如果替换file的话直接写名字替换值即可。
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("myHashSet");
redis.db.HashSet("myHashSet","id","9527");
redis.db.HashSet("myHashSet","userName","admin");
redis.db.HashSet("myHashSet","sex","男");
redis.db.HashSet("myHashSet","age","22");
redis.db.HashDelete("myHashSet","sex");
HashEntry[] hashEntries = redis.db.HashGetAll("myHashSet");
foreach (var item in hashEntries)
Console.WriteLine(item);
}
redis Set操作
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mySet");
redis.db.SetAdd("mySet","a1");
redis.db.SetAdd("mySet","a2");
redis.db.SetAdd("mySet","a3");
redis.db.SetAdd("mySet","a4");
redis.db.SetAdd("mySet","a5");
RedisValue[] redisValues = redis.db.SetMembers("mySet");
foreach (var item in redisValues)
Console.WriteLine(item);
}
获取Set长度
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mySet");
redis.db.KeyDelete("mySet1");
redis.db.SetAdd("mySet","a1");
redis.db.SetAdd("mySet","a2");
redis.db.SetAdd("mySet","a3");
redis.db.SetAdd("mySet","a4");
redis.db.SetAdd("mySet","a5");
redis.db.SetAdd("mySet1","a1");
redis.db.SetAdd("mySet1","a2");
redis.db.SetAdd("mySet1","a6");
redis.db.SetAdd("mySet1","a7");
long v1 = redis.db.SetLength("mySet");
long v2 = redis.db.SetLength("mySet1");
Console.WriteLine("v1:"+v1);
Console.WriteLine("v2:"+v2);
}
Set查询交集
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mySet");
redis.db.KeyDelete("mySet1");
redis.db.SetAdd("mySet","a1");
redis.db.SetAdd("mySet","a2");
redis.db.SetAdd("mySet","a3");
redis.db.SetAdd("mySet","a4");
redis.db.SetAdd("mySet","a5");
redis.db.SetAdd("mySet1","a1");
redis.db.SetAdd("mySet1","a2");
redis.db.SetAdd("mySet1","a6");
redis.db.SetAdd("mySet1","a7");
RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Intersect, "mySet", "mySet1");
foreach (var item in redisValues)
Console.WriteLine(item);
}
Set查询差集
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mySet");
redis.db.KeyDelete("mySet1");
redis.db.SetAdd("mySet","a1");
redis.db.SetAdd("mySet","a2");
redis.db.SetAdd("mySet","a3");
redis.db.SetAdd("mySet","a4");
redis.db.SetAdd("mySet","a5");
redis.db.SetAdd("mySet1","a1");
redis.db.SetAdd("mySet1","a2");
redis.db.SetAdd("mySet1","a6");
redis.db.SetAdd("mySet1","a7");
RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Difference, "mySet", "mySet1");
foreach (var item in redisValues)
Console.WriteLine(item);
}
Set查询并集
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("mySet");
redis.db.KeyDelete("mySet1");
redis.db.SetAdd("mySet","a1");
redis.db.SetAdd("mySet","a2");
redis.db.SetAdd("mySet","a3");
redis.db.SetAdd("mySet","a4");
redis.db.SetAdd("mySet","a5");
redis.db.SetAdd("mySet1","a1");
redis.db.SetAdd("mySet1","a2");
redis.db.SetAdd("mySet1","a6");
redis.db.SetAdd("mySet1","a7");
RedisValue[] redisValues = redis.db.SetCombine(SetOperation.Union, "mySet", "mySet1");
foreach (var item in redisValues)
Console.WriteLine(item);
}
Redis ZSET
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("myZset");
redis.db.SortedSetAdd("myZset","a1",69);
redis.db.SortedSetAdd("myZset","a2",79);
redis.db.SortedSetAdd("myZset","a3",85);
redis.db.SortedSetAdd("myZset","a4",82);
redis.db.SortedSetAdd("myZset","a5",92);
//分数范围
RedisValue[] redisValues = redis.db.SortedSetRangeByScore("myZset", 70, 90);
foreach (var item in redisValues)
Console.WriteLine(item);
}
正序
Plain Text
自动换行
xxxxxxxxxx
1
using StackExchange.Redis;
2
using System;
3
namespace demo_20221010_redis
4
{
5
class Program
6
{
7
static void Main(string[] args)
8
{
9
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
10
redis.db.KeyDelete("myZset");
11
redis.db.SortedSetAdd("myZset","a1",69);
12
redis.db.SortedSetAdd("myZset","a2",79);
13
redis.db.SortedSetAdd("myZset","a3",85);
14
redis.db.SortedSetAdd("myZset","a4",82);
15
redis.db.SortedSetAdd("myZset","a5",92);
16
//正序
17
SortedSetEntry[] sortedSetEntries = redis.db.SortedSetRangeByRankWithScores("myZset",0,-1,Order.Ascending);
18
foreach (var item in sortedSetEntries)
19
{
20
Console.WriteLine(item);
21
}
22
}
23
}
24
}
倒序
using StackExchange.Redis;
using System;
namespace demo_20221010_redis
class Program
static void Main(string[] args)
RedisDBHelper redis = new RedisDBHelper("127.0.0.1:6379");
redis.db.KeyDelete("myZset");
redis.db.SortedSetAdd("myZset","a1",69);
redis.db.SortedSetAdd("myZset","a2",79);
redis.db.SortedSetAdd("myZset","a3",85);
redis.db.SortedSetAdd("myZset","a4",82);
redis.db.SortedSetAdd("myZset","a5",92);
SortedSetEntry[] sortedSetEntries = redis.db.SortedSetRangeByRankWithScores("myZset",0,-1,Order.Descending);
foreach (var item in sortedSetEntries)
Console.WriteLine(item);
.net core工具组件系列之Redis—— 第一篇:Windows环境配置Redis(5.x以上版本)以及部署为Windows服务
Cygwin工具编译Redis Redis6.x版本是未编译版本(官方很调皮,所以没办法,咱只好帮他们编译一下了),所以咱们先下载一个Cygwin,用它来对Redis进行编译。
十六、.net core(.NET 6)搭建基于Redis的Hangfire定时器
搭建基于Redis的Hangfire定时器Hangfire的定时配置信息会自动生成在指定到数据库内,包括关系型数据库或非关系型数据库内。目前为止,它在Redis、Oracle上面,可以支持最短15秒的定时周期,其他数据库是分钟级别到。以下使用Hangfire+Redis来实现定时任务。首先创建新的类库项目 Wsk.Core.Hangfire,然后在Wsk.Core.Package包项目下引用hangfire有关的组件,包括 Hangfire、Hangfire.Core、Hangfire.Redis、Hangfire.Redis.StaskExchange。
九、.net core(.NET 6)添加通用的Redis功能
.net core 编写通用的Redis功能在 Package项目里面,添加包:StackExchange.Redis:
一套.Net6可落地的微服务、分布式开源项目
一个轻量级的微服务架构,支持经典三层架构和DDD架构模式开发,技师选型都是目前主流的技术。是一个前后端分离架构,前端采用Vue开发,后端使用.Net6架构。Api接口遵循RESTful规范,接口安全验证采用JWT机制。
ASP.NET Core 使用Redis实现分布式缓存:Docker、 IDistributedCache、StackExchan geRedis
ASP.NET Core 使用Redis实现分布式缓存:Docker、 IDistributedCache、StackExchan geRedis