memcache配置实践

一、下载安装memcache服务器

/Files/dlz1123/memcacheddotnet_clientlib-1.1.5.zip

解压,然后在cmd命令行安装c:

cd \解压后文件所在目录

memcached.exe -d install

memcached.exe -d start        该命令启动 Memcached,默认监听端口为 11211

 

参数解释:

-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户,如果当前为 root 的话,需要使用此参数指定用户。

 

-h 帮助 

二、安装客户端 

/Files/dlz1123/memcacheddotnet_clientlib-1.1.5.zip

解压之后将文件中的Memcached.ClientLibrary.dll 添加到项目的引用中

代码
  public   void  clear()
        {
        
string [] servers  =  {  " 10.10.80.94:11211 "  };
            SockIOPool pool 
=  SockIOPool.GetInstance();
            pool.SetServers(servers);
            pool.InitConnections 
=   3 ;
            pool.MinConnections 
=   3 ;
            pool.MaxConnections 
=   5 ;
            pool.SocketConnectTimeout 
=   1000 ;
            pool.SocketTimeout 
=   3000 ;
            pool.MaintenanceSleep 
=   30 ;
            pool.Failover 
=   true ;
            pool.Nagle 
=   false ;
            pool.Initialize();
            MemcachedClient mc 
=   new  Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression 
=   false ;
            mc.Delete(
" cache " );
            Response.Write(
" 清除缓存成功 " );
        }


        
public   void   get ()
        {
            
string [] servers  =  {  " 10.10.80.94:11211 " };
            SockIOPool pool 
=  SockIOPool.GetInstance();
            pool.SetServers(servers);
            pool.InitConnections 
=   3 ;
            pool.MinConnections 
=   3 ;
            pool.MaxConnections 
=   5 ;
            pool.SocketConnectTimeout 
=   1000 ;
            pool.SocketTimeout 
=   3000 ;
            pool.MaintenanceSleep 
=   30 ;
            pool.Failover 
=   true ;
            pool.Nagle 
=   false ;
            pool.Initialize();
            MemcachedClient mc 
=   new  Memcached.ClientLibrary.MemcachedClient();
            mc.EnableCompression 
=   false ;
            StringBuilder sb 
=   new  StringBuilder();
            
// 写入缓存
            sb.AppendLine( " 写入测试: " );
            sb.AppendLine(
" <br>_______________________________________<br> " );
            
if  (mc.KeyExists( " cache " ))
            {
                sb.AppendLine(
" 缓存cache已存在 " );
            }
            
else
            {
                mc.Set(
" cache " " 写入缓存时间: "   + DateTime.Now.ToString());
                sb.AppendLine(
" 缓存已成功写入到cache " );
            }
            sb.AppendLine(
" <br>_______________________________________<br> " );
            sb.AppendLine(
" 读取缓存内容如下:<br> " );
            sb.AppendLine(mc.Get(
" cache " ).ToString());


            
// 分析缓存状态
            Hashtable ht  =  mc.Stats();
            sb.AppendLine(
" <br>_______________________________________<br> " );
            sb.AppendLine(
" Memcached Stats: " );
            sb.AppendLine(
" <br>_______________________________________<br> " );
            
foreach  (DictionaryEntry de  in  ht)
            {
                Hashtable info 
=  (Hashtable)de.Value;
                
foreach  (DictionaryEntry de2  in  info)
                {
                    sb.AppendLine(de2.Key.ToString() 
+   " :&nbsp;&nbsp;&nbsp;&nbsp; "   +  de2.Value.ToString()  +   " <br> " );
                }
            }
            Response.Write(sb.ToString());
        }

 

 

 

 

  

你可能感兴趣的:(memcache)