【8】memcached实例

一.memcached环境搭建

1、下载后解压到D:\memcached(下载地址:memcached-win64下载地址

      

2、安装到windows服务,打开cmd命令行,进入memcached目录,执行memcached -d install命令,安装服务。

     如果在没有安装过的情况下,出现"failed to install service or service already installed"错误,可能是cmd.exe需要用管理员身份运行。

     

3、启动服务,执行memcached -d start

4、参数介绍

     -p 监听的端口 
    -l 连接的IP地址, 默认是本机 
   -d start 启动memcached服务 
   -d restart 重起memcached服务 
   -d stop|shutdown 关闭正在运行的memcached服务 
   -d install 安装memcached服务 
   -d uninstall 卸载memcached服务 
   -u 以的身份运行 (仅在以root运行的时候有效) 
   -m 最大内存使用,单位MB。默认64MB 
   -M 内存耗尽时返回错误,而不是删除项 
   -c 最大同时连接数,默认是1024 
   -f 块大小增长因子,默认是1.25 
   -n 最小分配空间,key+value+flags默认是48 
   -h 显示帮助

 

二. .net缓存实例

1.添加缓存项目
2.nuget中下载enyimmemcached包
 
3.web.config添加配置文件
<configuration>
  <configSections>
    <sectionGroup name="enyim.com">
      <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
  </configSections>
  <enyim.com protocol="Binary">
    <memcached>
      <servers>
        <add address="127.0.0.1" port="11211"  />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

 


4.添加MemcachedHelper类
    public class MemcachedHelper
    {
        MemcachedClient memClient = new MemcachedClient();

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Set(string key, object value)
        {
            object memValue;
            if (memClient.TryGet(key, out memValue))
            {
                memClient.Remove(key);
            }
            return memClient.Store(Enyim.Caching.Memcached.StoreMode.Add, key, value);
        }

        /// <summary>
        /// 获取缓存 泛型方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Get<T>(string key) where T : class
        {
            object result;
            if (memClient.TryGet(key, out result))
                return result as T;
            return null;
        }

        /// <summary>
        /// 获取缓存 通用方法
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            object result;
            if (memClient.TryGet(key, out result))
                return result;
            return null;
        }
    }

 


5.Main方法中代码
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MemcachedHelper memHelper = new MemcachedHelper();
                //缓存字符串
                memHelper.Set("memStr", "Hello World!");
                var a = memHelper.Get("memStr");
                Console.WriteLine(a);
                //缓存类型
                memHelper.Set("memClass", new MemClass() { Id = 1, Name = "class" });
                var b = memHelper.Get<MemClass>("memClass");
                Console.WriteLine(string.Format("Id:{0} Name:{1}", b.Id, b.Name));
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }


    [Serializable]//注:缓存类型一定要加可序列化的特性,否则无法缓存成功
    public class MemClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

 

 
6.运行项目


 
 
至此一个简单的memcached项目顺利

 

posted @ 2017-04-10 19:14  清幽火焰  阅读(316)  评论(0编辑  收藏  举报