发表于: 2018-05-20 21:00:50
1 1290
今天完成的事情:
完成memcache代码编写和测试
applicationContext.xml中配置
<!--定义XMemcachedClientBuilder实例-->
<bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg name="addressList" value="localhost:11211">
</constructor-arg>
<!--连接超时-->
<property name="connectTimeout"><value>3000</value></property>
<!--SASL验证开启 ,如果没有相关环境,请关闭 start-->
<property name="authInfoMap"><map> <entry key-ref="server1">
<bean class="net.rubyeye.xmemcached.auth.AuthInfo" factory-method="plain">
<constructor-arg name="username"><value>xxx</value></constructor-arg>
<constructor-arg name="password"><value>123</value></constructor-arg>
</bean></entry></map></property>
<!--SASL验证开启 end--> <!--设置线程池-->
<property name="connectionPoolSize" value="2"></property>
<!--使用二进制协议-->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean></property>
<!--设置序列化方式-->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"></bean></property>
<!--设置一致性哈希--> <property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean> </property></bean>
<!--定义memcachedClient,通过memcachedClientBuilder的build方法-->
<bean name="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build" destroy-method="shutdown">
</bean>
memcache代码书写
/**
* ??????
*
* @param
* @param model
* @return
*/
@RequestMapping("/students")
public String getAllStu(Model model) throws InterruptedException, MemcachedException, TimeoutException {
if (memcachedClient.get("students") == null) {
List<Student> students = stu.getGetAll();
memcachedClient.set("students", 10 * 60 * 1000, students);
}
System.out.println("123:" + memcachedClient.get("students"));
List<Student> students1 = memcachedClient.get("students");
Iterator<Student> s = students1.iterator();
while (s.hasNext()) {
System.out.println("这些人是:" + s.next().toString());
}
model.addAttribute("studentList", students1);
return "all";
}
/**
* ??????json
*
* @param
* @param model
* @return
*/
@RequestMapping("/json/students")
public String getAllStuOne(Model model) throws InterruptedException, MemcachedException, TimeoutException {
if (memcachedClient.get("students") == null) {
List<Student> students = stu.getGetAll();
memcachedClient.set("students", 10 * 60 * 1000, students);
}
System.out.println("123:" + memcachedClient.get("students"));
List<Student> students1 = memcachedClient.get("students");
Iterator<Student> s = students1.iterator();
while (s.hasNext()) {
System.out.println("这些人是:" + s.next().toString());
}
model.addAttribute("students", students1);
return "stuJson";
}
修改的memcache.properties如下
# the pool size(the number of client)
memcached.connectionPoolSize=50
# in this mode, when a node out, it will throws MemcachedException when call this node
memcached.failureMode=true
#server1
memcached.server1.host=127.0.0.1
memcached.server1.port=11211
memcached.server1.weight=1
#server2
测试的结果
测试json数据
无缓存json测试
缓存非json测试 测到一般请求卡死了
缓存json测试
缓存json同时修改memcaceh.properties的ip地址的测试
测试结果 有缓存的json同时修改memcaceh.properties的ip地址的测试 90%line为1536ms 吞吐量throughput:59.5/sec数据最好。
明天计划的事情:负载均衡, Rigs
遇到的问题:以解决
收获:缓存memcache的使用
评论