发表于: 2019-11-18 21:32:11

1 1009


今天完成的事情:

spring整合memcached

memcached.properties

#服务器地址
memcache.server=127.0.0.1
memcached.port=11211
#初始连接数目
memcache.initConn=20
#每个服务器建立最小连接数
memcache.minConn=10
#每个服务器建立最大连接数
memcache.maxConn=50
#自查线程周期工作,其每次休眠时间
memcache.maintSleep=3000
#是否使用nagle算法(Socket参数,如果是true,写数据不缓冲,直接发送)
memcache.nagle=false
#Socket阻塞读取数据的超时时间
memcache.socketTO=3000

memcached.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="locations">
           <list>
               <value>classpath:/memcached.properties</value>
           </list>
       </property>
   </bean>

   <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
         factory-method="getInstance" init-method="initialize"
         destroy-method="shutDown">

       <constructor-arg value="memCachedPool"/>

       <property name="servers">
           <list>
               <value>${memcache.server}:${memcached.port}}</value>
           </list>
       </property>

       <property name="initConn" value="${memcache.initConn}"/>

       <property name="minConn" value="${memcache.minConn}"/>

       <property name="maxConn" value="${memcache.maxConn}"/>

       <property name="maintSleep" value="${memcache.maintSleep}"/>

       <property name="nagle" value="${memcache.nagle}"/>

       <property name="socketTO" value="${memcache.socketTO}"/>
   </bean>

   <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
       <constructor-arg value="memCachedPool"/>
   </bean>

spring整合

扫描配置文件

在配置context的时候需要在文件后面加上ignore-unresolvable,这是因为在多个properties文件中项目会分辨不出该引入哪个配置文件进去

随便创建个测试单元测试一下

public class MemcacheTest {

private MemCachedClient memCachedClient;
   @Before
   public void beforeTest(){

ApplicationContext atx = new ClassPathXmlApplicationContext("/memcached.xml");
       memCachedClient = (MemCachedClient)atx.getBean("memCachedClient");
   }

@Test
   public void TestMem(){
memCachedClient.set("name","555555");

       System.out.println(memCachedClient.get("name"));
   }
}

运行结果

得先在本地运行memcache服务,不然无法连接memcache打印结果会为空


在service实现类的方法里加入缓存逻辑

不知道是不是我思路的问题。。。运行倒是没有报错。。压力测试加不加缓存数据都差不多

好像是没加进去。。。晕

一个简单的查询全部方法。。返回json数据

serviceimpl

controller


明天计划的事情:把缓存逻辑写到代码里面去,思路有点乱。。感觉自己进度有点慢了。。得加快进度


遇到的问题:在使用tomcat运行时无法解析值字符串中的占位符,是因为添加context时没有在文件后面加上ignore-unresolvable,因为在扫面多个properties文件项目会分不出应该引用哪个配置文件。


收获:

spring整合memcached


返回列表 返回列表
评论

    分享到