一、安装
下载memcached-win32-1.4.4-14.exe
1 解压到指定目录,如D:\mem1.44\memcached-win32-1.4.4-14\
2 用cmd打开命令窗口,转到解压的目录,输入 memcached.exe -d install
3 打开控制面板,打开服务,可以看到memcached已经在上面可,如果没有启动,则手动启动一下。
二、使用
安装memcached后,我们怎么用?
Memcached,我们可以看成一种缓存服务器,我们将某些数据入库的时候,要先将这些数据以key-value的形式存到memcached中,这样我们需要数据的时候,先到memcached中查找,如果有,则取出来用,如果没有,再去数据库中查找,查找出来后,将查找出来的数据存到memcached中,供下次用。
既然如此,我们在后台处理数据的时候,对数据存取,成了使用memcached最大的问题。
还好,memcached对于数据存取有很多解决方案,比如memcached本身提供的java memcached clent,比如xmemcached等,对比一下,xmemcached有很多优点,所以我们采用xmemcached方式存取数据。
需要的jar包
导入工程后,新建一个测试类:
MemcachedClientBuilder builder= new XMemcachedClientBuilder(AddrUtil .getAddresses("localhost:11211")); MemcachedClient memcachedClient = null; try { memcachedClient = builder.build(); } catch (IOException e1) { e1.printStackTrace(); } try { //第一个参数:key。第二个参数:单位是秒,意思是存储时间,0为永久 //第三个参数:value memcachedClient.set("hello", 0, "Hello,xmemcached"); String value= memcachedClient.get("hello"); System.out.println("hello=" + value); memcachedClient.delete("hello"); value= memcachedClient.get("hello"); System.out.println("hello=" + value); } catch (MemcachedException e) { System.err.println("MemcachedClientoperation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClientoperation timeout"); e.printStackTrace(); } catch (InterruptedException e) { // ignore } try { memcachedClient.shutdown(); } catch (IOException e) { System.err.println("ShutdownMemcachedClient fail"); e.printStackTrace(); }
三、spring+xmemcached
1 建立web工程,搭建spring环境
导入包
在spring的xml文件中添加如下代码:
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean"> <property name="servers"> <value>localhost:11211</value> </property> <!—由于只有一台memcached缓存器,所以不配如下权重了 <propertyname="weights"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> --> <property name="sessionLocator"> <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean> </property> <property name="transcoder"> <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/> </property> <property name="bufferAllocator"> <bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean> </property> </bean>
2 简单解释参数
servers
服务器列表,格式:ip:port
weights 权重 越大,存储量越大
主机映射:host1对应1号、host2对应2号..
sessionLocator
Session 分配器,有自带的,影响分布式
transcoder
通信编码方式
bufferAllocator
缓冲区分配器
这些参数都用xmemcached自带的解释方式就行。
测试类代码:
ApplicationContext context = newClassPathXmlApplicationContext("beans.xml"); XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient"); try { xmc.set("hello", 0, "pms"); } catch (TimeoutExceptione1) { e1.printStackTrace(); } catch (InterruptedException e1) { e1.printStackTrace(); } catch (MemcachedException e1) { e1.printStackTrace(); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } try { String str=xmc.get("hello"); System.out.println(str); xmc.shutdown(); } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generatedcatch block e.printStackTrace(); }
运行没出现错误
备注:如果采用spring容器,在web.xml中配置加载文件的话,就不用
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
XMemcachedClient xmc=(XMemcachedClient) context.getBean("memcachedClient");
这两句了,而直接采用注入的方式就可以了,得到的xmc对象即可以操纵数据了。