(精华)2020年8月15日 redis数据库 StackExchange.Redis中String类型(C#版)

RedisStringService 类请参考第一篇的封装

redis 数据结构string的使用

using (RedisStringService service = new RedisStringService())
                {
                    service.Set<string>("student1", "梦的翅膀");
                    Console.WriteLine(service.Get("student1"));

                    service.Append("student1", "20180802");
                    Console.WriteLine(service.Get("student1"));

                    Console.WriteLine(service.GetAndSetValue("student1", "程序错误"));
                    Console.WriteLine(service.Get("student1"));

                    service.Set<string>("student2", "王", DateTime.Now.AddSeconds(5));
                    Thread.Sleep(5100);
                    Console.WriteLine(service.Get("student2"));

                    service.Set<int>("Age", 32);
                    Console.WriteLine(service.Incr("Age"));
                    Console.WriteLine(service.IncrBy("Age", 3));
                    Console.WriteLine(service.Decr("Age"));
                    Console.WriteLine(service.DecrBy("Age", 3));
                }

案例

using (RedisStringService service = new RedisStringService())
{
//service.Set<string>($"userinfo_{user.Id}", Newtonsoft.Json.JsonConvert.SerializeObject(user));
service.Set<UserInfo>($"userinfo_{user.Id}", user);
var userCacheList = service.Get<UserInfo>(new List<string>() { $"userinfo_{user.Id}" });
var userCache = userCacheList.FirstOrDefault();
//string sResult = service.Get($"userinfo_{user.Id}");
//var userCache = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(sResult);
userCache.Account = "Admin";
service.Set<UserInfo>($"userinfo_{user.Id}", userCache);
}

超卖秒杀活动

public class Oversell
    {
        private static bool IsGoOn = true;//秒杀活动是否结束
        public static void Show()
        {
            using (RedisStringService service = new RedisStringService())
            {
                service.Set<int>("Stock", 10);//是库存
            }

            for (int i = 0; i < 5000; i++)
            {
                int k = i;
                Task.Run(() =>//每个线程就是一个用户请求
                {
                    using (RedisStringService service = new RedisStringService())
                    {
                        if (IsGoOn)
                        {
                            long index = service.Decr("Stock");//-1并且返回  
                            if (index >= 0)
                            {
                                Console.WriteLine($"{k.ToString("000")}秒杀成功,秒杀商品索引为{index}");
                                //可以分队列,去数据库操作
                            }
                            else
                            {
                                if (IsGoOn)
                                {
                                    IsGoOn = false;
                                }
                                Console.WriteLine($"{k.ToString("000")}秒杀失败,秒杀商品索引为{index}");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{k.ToString("000")}秒杀停止......");
                        }
                    }
                });
            }
            Console.Read();
        }
    }

扩展版使用

//key  value 都是string   假如是个对象呢?序列化一下
                //假如要修改某一个属性的值   读--反序列化--修改--序列化 memcached
                using (RedisStringService service = new RedisStringService())
                {
                    service.KeyFulsh();
                    service.StringSet("RedisStringService_key1", "RedisStringService_value1");
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    Console.WriteLine(service.StringGetSet("RedisStringService_key1", "RedisStringService_value2"));
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));

                    service.StringAppend("RedisStringService_key1", "Append");
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    service.StringSet("RedisStringService_key1", "RedisStringService_value", new TimeSpan(0, 0, 0, 5));
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                    Thread.Sleep(5000);
                    Console.WriteLine(service.StringGet("RedisStringService_key1"));
                }
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:上身试试 返回首页