MemcacheHelper.cs

Posted BarryLiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MemcacheHelper.cs相关的知识,希望对你有一定的参考价值。

 1 using Memcached.ClientLibrary;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Common
 9 {
10    public class MemcacheHelper
11     {
12        private static readonly MemcachedClient mc = null;
13 
14        static MemcacheHelper()
15        {
16            //最好放在配置文件中
17            string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };
18 
19            //初始化池
20            SockIOPool pool = SockIOPool.GetInstance();
21            pool.SetServers(serverlist);
22 
23            pool.InitConnections = 3;
24            pool.MinConnections = 3;
25            pool.MaxConnections = 5;
26 
27            pool.SocketConnectTimeout = 1000;
28            pool.SocketTimeout = 3000;
29 
30            pool.MaintenanceSleep = 30;
31            pool.Failover = true;
32 
33            pool.Nagle = false;
34            pool.Initialize();
35 
36            // 获得客户端实例
37             mc = new MemcachedClient();
38            mc.EnableCompression = false;
39        }
40        /// <summary>
41        /// 存储数据
42        /// </summary>
43        /// <param name="key"></param>
44        /// <param name="value"></param>
45        /// <returns></returns>
46        public static bool Set(string key,object value)
47        {
48           return mc.Set(key, value);
49        }
50        public static bool Set(string key, object value,DateTime time)
51        {
52            return mc.Set(key, value,time);
53        }
54        /// <summary>
55        /// 获取数据
56        /// </summary>
57        /// <param name="key"></param>
58        /// <returns></returns>
59        public static object Get(string key)
60        {
61            return mc.Get(key);
62        }
63        /// <summary>
64        /// 删除
65        /// </summary>
66        /// <param name="key"></param>
67        /// <returns></returns>
68        public static bool Delete(string key)
69        {
70            if (mc.KeyExists(key))
71            {
72                return mc.Delete(key);
73 
74            }
75            return false;
76 
77        }
78     }
79 }

 

以上是关于MemcacheHelper.cs的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数