memcache安装安装libevent库Linux:centOs5.4cd/usr/local/srcwget~provos/libevent-1.3b.tar.gztarzxvflibevent-1.3b.tar.gzcdlibevent-1.3b./configuremake&&makeinstall安装成功安装memcachecd/usr/local/srcwget启动cdusr/local/binbin]#memcached–h出错memcached:errorwhileloadingsharedlibraries:libevent-1.3b.so.1:cannotopensharedobjectfile:Nosuchfileordirectorygoogle解决方法:则运行LD_DUBUG=libs就可以知道memcached启动时库的加载路径。具体如下:[root@bogonusr/local/bin]#LD_DEBUG=libsmemcached–v5112:findlibrary=libevent-1.3b.so.1[0];searching5112:searchcache=/etc/ld.so.cache5112:searchpath=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib(systemsearchpath)5112:tryingfile=/lib/tls/i686/sse2/libevent-1.3b.so.15112:tryingfile=/lib/tls/i686/libevent-1.3b.so.15112:tryingfile=/lib/tls/sse2/libevent-1.3b.so.15112:tryingfile=/lib/tls/libevent-1.3b.so.15112:tryingfile=/lib/i686/sse2/libevent-1.3b.so.15112:tryingfile=/lib/i686/libevent-1.3b.so.15112:tryingfile=/lib/sse2/libevent-1.3b.so.15112:tryingfile=/lib/libevent-1.3b.so.15112:tryingfile=/usr/lib/tls/i686/sse2/libevent-1.3b.so.15112:tryingfile=/usr/lib/tls/i686/libevent-1.3b.so.15112:tryingfile=/usr/lib/tls/sse2/libevent-1.3b.so.15112:tryingfile=/usr/lib/tls/libevent-1.3b.so.15112:tryingfile=/usr/lib/i686/sse2/libevent-1.3b.so.15112:tryingfile=/usr/lib/i686/libevent-1.3b.so.15112:tryingfile=/usr/lib/sse2/libevent-1.3b.so.15112:tryingfile=/usr/lib/libevent-1.3b.so.1然后给libevent-1.3b.so.1建立一个链接:[root@bogon~]#sudoln-s/usr/local/lib/libevent-1.3b.so.1/lib/libevent-1.3b.so.1检查是否建立成功[root@bogon~]#ls-al/lib|greplibeventlrwxrwxrwx1rootroot33Aug2519:52libevent-1.3b.so.1-/usr/local/lib/libevent-1.3b.so.1此时在bin]#memcached–h[root@bogonbin]#memcached-hmemcached1.2.5-pnumTCPportnumbertolistenon(default:11211)-UnumUDPportnumbertolistenon(default:0,off)-sfileunixsocketpathtolistenon(disablesnetworksupport)-amaskaccessmaskforunixsocket,inoctal(default0700)-lip_addrinterfacetolistenon,defaultisINDRR_ANY-drunasadaemon-rmaximizecorefilelimit-uusernameassumeidentityofusername(onlywhenrunasroot)-mnummaxmemorytouseforitemsinmegabytes,defaultis64MB-Mreturnerroronmemoryexhausted(ratherthanremovingitems)-cnummaxsimultaneousconnections,defaultis1024-klockdownallpagedmemory.Notethatthereisalimitonhowmuchmemoryyoumaylock.Tryingtoallocatemorethanthatwouldfail,sobesureyousetthelimitcorrectlyfortheuseryoustartedthedaemonwith(notfor-uusernameuser;undershthisisdonewith'ulimit-S-lNUM_KB').-vverbose(printerrors/warningswhileineventloop)-vvveryverbose(alsoprintclientcommands/reponses)-hprintthishelpandexit-iprintmemcachedandlibeventlicense-brunamanagedinstanced(mnemonic:buckets)-PfilesavePIDinfile,onlyusedwith-doption-ffactorchunksizegrowthfactor,default1.25-nbytesminimumspaceallocatedforkey+value+flags,default48现在memcache成功搞定默认情况下memcached安装到/usr/local/bin下。启动:[root@bogonbin]#memcached-d-m128-uroot-p11211检查是否已经启动[root@bogonbin]#telnet127.0.0.111211Trying127.0.0.1...Connectedtolocalhost.localdomain(127.0.0.1).Escapecharacteris'^]'.这里使用的memcached启动选项的内容如下。选项说明p使用的TCP端口。默认为11211m最大内存大小。默认为64Mvv用veryvrebose模式启动,调试信息和错误输出到控制台d作为daemon在后台启动查看memcached的内部状态[root@bogonbin]#cd/usr/local/bin[root@bogonbin]#telnet127.0.0.111211Trying127.0.0.1...Connectedtolocalhost.localdomain(127.0.0.1).Escapecharacteris'^]'.statsSTATpid6779STATuptime14217是memcached运行的秒数STATtime1314343512STATversion1.2.5STATpointer_size32STATrusage_user0.002999STATrusage_system0.027995STATcurr_items0表示现在在缓存中的键值对个数STATtotal_items0总数STATbytes0STATcurr_connections3当前连接数STATtotal_connections4总连接数STATconnection_structures4STATcmd_get0查询缓存的次数STATcmd_set0设置key=value的次数STATget_hits0缓存命中的次数STATget_misses0get_misses的数字加上get_hits应该等于cmd_getSTATevictions0STATbytes_read56STATbytes_written482STATlimit_maxbytes134217728STATthreads1ENDQuit其中,uptime是memcached运行的秒数,cmd_get是查询缓存的次数。这两个数据相除一下就能得到平均每秒请求缓存的次数——最近niupu的流量很低,所以平均也就一秒请求一次多,这么点大的压力,用文件系统缓存一样没问题,根本不会体现出使用memcached的优越。下面的cmd_set就是设置key=value的次数。整个memcached是个大hash,用cmd_get没有找到的内容,就会调用一下cmd_set写进缓存里。紧跟着是get_hits,就是缓存命中的次数。缓存命中率=get_hits/cmd_get*100%。下面的get_misses的数字加上get_hits应该等于cmd_get。而total_itemscurr_items表示现在在缓存中的键值对个数,在图上total_items==cmd_set==get_misses,不过当可用最大内存用光时,memcached就会删掉一些内容,上面的等式就不成立了。Springaop管理memcached1,给DAO的方法上加SpringAOP的Around通知,决定数据从数据库获得还是从缓存获得。2,自定义两个Annotation,@Cache和@Flush,分别加在DAO的查询和修改的方法上。3,Around通知函数内用反射获取上述的@cache和@Flush,代表是记录缓存,还是删除缓存。4,记录缓存时用“类名+方法名+参数的hashcode”作为Key,删除缓存时,删除所有的同类名开头的key对应的缓存。5,Memcached不支持遍历key,所以采用数据库表记录key,假设:tbl_memcached(key,exp_date)。6,定义一个时钟,定期删除tbl_memcached的过期记录(删除数据库记录前,删除缓存)。7,记录缓存时,同时存入一条记录到tbl_memcached表。8,删除缓存是依据tbl_memcache表的key来进行,并且要删除tbl_memcached表的相关记录。使用memcached显著提升站点性能Memcached常被用来加速应用程序的处理,在这里,我们将着重于介绍将它部署于应用程序