See the RedisStringService class for encapsulation in Article 1

The use of redis data structure String

using (RedisStringService service = new RedisStringService())
                {
                    service.Set<string> ("student1"."Wings of dreams.");
                    Console.WriteLine(service.Get("student1"));

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

                    Console.WriteLine(service.GetAndSetValue("student1"."Program error"));
                    Console.WriteLine(service.Get("student1"));

                    service.Set<string> ("student2"."The king", 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));
                }
Copy the code

case

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);
}
Copy the code

Oversold seckill activities

public class Oversell
    {
        private static bool IsGoOn = true;// Whether the seckill activity ends
        public static void Show()
        {
            using (RedisStringService service = new RedisStringService())
            {
                service.Set<int> ("Stock".10);/ / is inventory
            }

            for (int i = 0; i < 5000; i++)
            {
                int k = i;
                Task.Run(() =>// Each thread is a user request
                {
                    using (RedisStringService service = new RedisStringService())
                    {
                        if (IsGoOn)
                        {
                            long index = service.Decr("Stock");//-1 and return
                            if (index >= 0)
                            {
                                Console.WriteLine($"{k.ToString("000")}The second kill succeeded. The second kill index is{index}");
                                // Select * from database
                            }
                            else
                            {
                                if (IsGoOn)
                                {
                                    IsGoOn = false;
                                }
                                Console.WriteLine($"{k.ToString("000")}Seconds kill failed, seconds kill index is{index}"); }}else
                        {
                            Console.WriteLine($"{k.ToString("000")}Seckill stop......"); }}}); } Console.Read(); }}Copy the code

Use of extended Version

// Key value is a string. Serialize
                // If you want to change the value of an attribute read -- deserialize -- modify -- serialize 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"));
                }
Copy the code