public IActionResult Index()
{
ConnectionMultiplexer _conn = ConnectionMultiplexer.Connect("127.0.0.1:6379");//初始化
var database = _conn.GetDatabase(7);//指定连接的库 0
RedisHelper redisHelper = new RedisHelper("4");
bool r1 = redisHelper.SetValue("key", "Hello Word!");
if (r1)
{
Console.WriteLine(redisHelper.GetValue("key"));
//redisHelper.DeleteKey("key");
}
RedisCacheHelper.Add("zhong", "zhongzhongzhong", DateTime.Now.AddDays(1));
string name = database.StringGet("name");
string age = database.StringGet("age");
var tran = database.CreateTransaction();//创建事物
tran.AddCondition(Condition.StringEqual("name", name));//乐观锁
tran.StringSetAsync("name", "海");
tran.StringSetAsync("age", 25);
database.StringSet("name", "Cang");//此时更改 name 值,提交事物的时候会失败。
bool committed = tran.Execute();//提交事物,true成功,false回滚。
string str3 = RedisCacheHelper.Get<string>("zhong");
string str = RedisCacheHelper.Get<string>("nihao");
string str1 = RedisCacheHelper.Get<string>("wei");
database.StringSet("name", "苍");//设置StringSet(key, value)
string str2 = database.StringGet("name");//结果:苍
database.StringSet("name_two", str2, TimeSpan.FromSeconds(10));//设置时间,10s后过期。
//创建对象
Demo demo = new Demo()
{
Name = "苍",
Age = 18,
Height = 1.83
};
string demojson = JsonConvert.SerializeObject(demo);//序列化
database.StringSet("model", demojson);
string model = database.StringGet("model");
demo = JsonConvert.DeserializeObject<Demo>(model);//反序列化
double increment = 0;
double decrement = 0;
for (int i = 0; i < 3; i++)
{
increment = database.StringIncrement("StringIncrement", 2);//增量,每次+2
}
for (int i = 0; i < 3; i++)
{
decrement = database.StringDecrement("StringIncrement");//减量,每次-1
}
for (int i = 0; i < 10; i++)
{
database.ListRightPush("list", i);//从底部插入数据
}
for (int i = 10; i < 20; i++)
{
database.ListLeftPush("list", i);//从顶部插入数据
}
var length = database.ListLength("list");//长度 20
var rightPop = database.ListRange("list");//从底部拿出数据var leftpop = database.ListLeftPop("list");//从顶部拿出数据var list = database.ListRange("list");
string json = JsonConvert.SerializeObject(demo);//序列化
database.HashSet("user", "cang", json);
database.HashSet("user", "shan", json);
database.HashSet("user", "yun", json);
//获取Model
string hashcang = database.HashGet("user", "cang");
demo = JsonConvert.DeserializeObject<Demo>(hashcang);//反序列化
//获取List
RedisValue[] values = database.HashValues("user");//获取所有value
IList<Demo> demolist = new List<Demo>();
foreach (var item in values)
{
Demo hashmodel = JsonConvert.DeserializeObject<Demo>(item);
demolist.Add(hashmodel);
}
ISubscriber sub = _conn.GetSubscriber();
//订阅 Channel1 频道
sub.Subscribe("Channel1", new Action<RedisChannel, RedisValue>((channel, message) =>
{
Console.WriteLine("Channel1" + " 订阅收到消息:" + message);
}));
for (int i = 0; i < 10; i++)
{
sub.Publish("Channel1", "msg" + i);//向频道 Channel1 发送信息
if (i == 2)
{
sub.Unsubscribe("Channel1");//取消订阅
}
}
var batch = database.CreateBatch();
//批量写
Task t1 = batch.StringSetAsync("name", "羽");
Task t2 = batch.StringSetAsync("age", 22);
batch.Execute();
Task.WaitAll(t1, t2);
Console.WriteLine("Age:" + database.StringGet("age"));
Console.WriteLine("Name:" + database.StringGet("name"));
//批量写
for (int i = 0; i < 100000; i++)
{
batch.StringSetAsync("age" + i, i);
}
batch.Execute();
//批量读
List<Task<RedisValue>> valueList = new List<Task<RedisValue>>();
for (int i = 0; i < 10000; i++)
{
Task<RedisValue> tres = batch.StringGetAsync("age" + i);
valueList.Add(tres);
}
batch.Execute();
foreach (var redisValue in valueList)
{
string value = redisValue.Result;//取出对应的value值
}
//ConfigurationBuilder builder = new ConfigurationBuilder();
//builder.AddJsonFile("appsettings.json");
//var configuration = builder.Build();
//var collections = configuration.AsEnumerable();
//foreach (var item in collections)
//{
// Console.WriteLine("{0}={1}", item.Key, item.Value);
//}
return View();
}