发表于: 2018-03-01 20:45:45
1 809
今天完成的事情:
今天继续学习缓存。
首先,搭建好服务器之后,就开始在本地连接到缓存服务器。
首先,搭建好服务器之后,就开始在本地连接到缓存服务器。
java在本地连接服务器就是用java代码创建 Memcached Client。
主要有三种方式,但是一般推荐XMemcached,比较好集成Spring:
Memcached Client for Java
SpyMemcached
XMemcached。
教程如下:
首先是jar包 xmemcached-1.4.1.jar:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.3.2</version>
</dependency>
然后关于memcached配置memcached.properties:
#server1
memcached.server1.host=127.0.0.1
memcached.server1.port=11211
memcached.server1.weight=1
第一行是服务器IP,第二行是服务器端口,因为可以设置多个服务器,第三行是该服务器在所有服务器所占的权重。
因为我们只有一个服务器,所以上面的缓存池我们就没有设置。
然后在spring的文件里,对memcached进行集成:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd
<!-- properties config -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:/com/springmvc/config/xmemcached.properties</value>
</list>
</property>
</bean>
<!-- Memcached配置 -->
<!-- p:connectionPoolSize="${memcached.connectionPoolSize}" p:failureMode="${memcached.failureMode}" -->
<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>${memcached.server1.host}</value>
</constructor-arg>
<constructor-arg>
<value>${memcached.server1.port}</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>${memcached.server1.weight}</value>
</list>
</constructor-arg>
<property name="commandFactory" >
<bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />
</property>
<property name="sessionLocator" >
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property>
<property name="transcoder" >
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<property name="connectionPoolSize" value="${memcached.connectionPoolSize}" />
<property name="failureMode" value="${memcached.failureMode}" />
</bean>
<!-- Use factory bean to build memcached client -->
<bean id="memcachedClient"
factory-bean="memcachedClientBuilder"
factory-method="build"
destroy-method="shutdown" />
</beans>
因为容器加载顺序为先加载IOC容器,然后加载MVC容器的原因,上面关于memcached的配置需要放在IOC也就是mybatis容器中。接着就是编写memcached的工具类:
public class XMemcachedUtil {
@Autowired
private XMemcachedClientBuilder memcachedClientBuilder;
@Autowired
private MemcachedClient memcachedClient;
public XMemcachedUtil() {
}
/**
* public void init() {
* xMemcachedUtil = this;
* xMemcachedUtil.memcachedClient = this.memcachedClient;
* xMemcachedUtil.memcachedClientBuilder = this.memcachedClientBuilder;
* }
*/
private MemcachedClient createClient() throws Exception {
if (memcachedClient == null) {//如果spring没有创建成功,再build一次
return memcachedClient = memcachedClientBuilder.build();
}
return null;
}
public void addCache(String name, int time, Object o) {
try {
createClient();
memcachedClient.add(name, time, SerializeUtil.serizlize(o));
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getCache(String name) {
try {
createClient();
byte[] nameValue = memcachedClient.get(name);
if (nameValue != null) {
return SerializeUtil.deserialize(nameValue);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void deleteCache(String name) {
try {
createClient();
memcachedClient.delete(name);
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
明天计划的事情:完成spring集成了Memcached的系统搭建。
遇到的问题:
暂无
收获:算是理清了关于Memcached的逻辑问题。
评论