• 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 }
  • 相关阅读:
    Debian/Kali下Subversion的配置
    Linux下解压Windows中的压缩包乱码解决办法
    JavaSocket全双工通信 问题 待解决
    MYSQL学习笔记
    Java Socket 全双工通信
    Kali2017安装后的那些事
    nginx的一次安装与配置
    解决1130 Host 'localhost' is not allowed to connect to this MySQL server
    SimpleDateFormat 和 LocalDate、LocalTime 以及时间大小比较简单示例
    java 线程池 ExeutorService
  • 原文地址:https://www.cnblogs.com/liangwenchao-912/p/5529000.html
Copyright © 2020-2023  润新知