发表于: 2017-10-22 23:41:29

2 744



今天完成的事情:

1. memcache整合spring 

2. memcache的测试


明天计划的事情

1.把memcache跑通后加入service层

2. 了解redies



遇到的问题: 

1.memcache无法实例化对象。

16:40:50 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@26df6e3a] to prepare test instance [memcache.MemcacheTest@4a3631f8]

java.lang.VerifyError: (class: com/danga/MemCached/SockIOPool, method: getSock signature: (Ljava/lang/String;)Lcom/danga/MemCached/SockIOPool$SockIO;) Wrong return type in function


原因:memcache版本问题,原为2.6.6

换成3.0.0即可



2. memcache通过spring的配置文件在test的测试了返回值为false, 不能存入缓存,但是放在main里时正常运行(版本问题解决起来真的是没有什么头绪)

当在main时:

public class TestMemcached {
public static void main(String[] args) {
/* 初始化SockIOPool,管理memcached的连接池 */
       String[] servers = { "localhost:11211" };
       SockIOPool pool = SockIOPool.getInstance();
       pool.setServers(servers);
       pool.setFailover(true);
       pool.setInitConn(10);
       pool.setMinConn(5);
       pool.setMaxConn(250);
       pool.setMaintSleep(30);
       pool.setNagle(false);
       pool.setSocketTO(3000);
       pool.setAliveCheck(true);
       pool.initialize();
       /* 建立MemcachedClient实例 */
       MemCachedClient memCachedClient = new MemCachedClient();
       for (int i = 0; i < 10; i++) {
/* 将对象加入到memcached缓存 */
           boolean success = memCachedClient.set("" + i, "Hello!");
           /* memcached缓存中按key值取对象 */
           String result = (String) memCachedClient.get("" + i);
           System.out.println(String.format("set( %d ): %s", i, success));
           System.out.println(String.format("get( %d ): %s", i, result));
       }
}

}

结果:




在JUNIT时:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:conf/spring-mybatis.xml","classpath:conf/spring-memcached.xml"})
public class MemcacheTest 
private static Logger loggerMencache= LoggerFactory.getLogger(MemcacheTest.class);

   private MemCachedClient cachedClient;

   @Test
   public void testMemcached(){
       loggerMencache.info("memcache");
       MemCachedClient cachedClient = new MemCachedClient();
       boolean success = cachedClient.set("hello","world");
       loggerMencache.info("success: "+ success);
       String hello = (String)memcachedUtils.get("hello");
       loggerMencache.info("hello: "+ hello);
   }

}

结果:

报错1解决:

attempting to get SockIO from uninitialized pool!

MemCachedClient cachedClient = new MemCachedClient("neeaMemcachedPool");

就可以解决返回为false,但是取不到值之还是为null


3. ${}占位符问题,在spring配置memcache时占位符无法解析,只能填上具体信息



收获:

spring配置memcache

<!-- Memcached配置 -->
<bean id="memcachedPool" class="com.whalin.MemCached.SockIOPool"
     factory-method="getInstance" init-method="initialize" destroy-method="shutDown"
     lazy-init="false" >
   <constructor-arg>
       <value>neeaMemcachedPool</value>
   </constructor-arg>
   <property name="servers">
       <list>
           <value>localhost:11211</value>
       </list>

   </property>

设置连接心跳监测开关

   <property name="aliveCheck" value="true"/>

设置初始连接数

   <property name="initConn" value="20"/>

设置最小连接数

   <property name="minConn" value="10"/>

设置最大连接数

   <property name="maxConn" value="50"/>

设置连接池维护线程的睡眠时间

   <property name="maintSleep" value="3000"/>

设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去

   <property name="nagle" value="false"/>

设置socket的读取等待超时时间
   <property name="socketTO" value="3000"/>
</bean>
<!--在使用 memcachedClient 访问 memchached , 需指明 poolname memcache,指定默认-->
<bean id="memcachedClient" class="com.whalin.MemCached.MemCachedClient">
   <constructor-arg>
       <value>neeaMemcachedPool</value>
   </constructor-arg>
</bean>

以上红字就是网上说的解决办法,没有用



进度: 

         任务开始时间:10.13

         预计完成时间:10.24

         是否有延期风险:有

         小看了任务六,全力以赴冲击任务六了

禅道:http://task.ptteng.com/zentao/project-task-264.htm






返回列表 返回列表
评论

    分享到