2014-05-09 7 views
0

예상대로 작동하지 내가 코드를 아래에 실행할 때이 출력 나타납니다 테스트memcached를 테스트 내가 <a href="https://code.google.com/p/simple-spring-memcached/" rel="nofollow">https://code.google.com/p/simple-spring-memcached/</a>을 구현하고있어

Adding to cache 
Adding to cache 

코드를 memcached를 :

import java.util.ArrayList; 
import java.util.List; 

import junit.framework.Assert; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import com.google.code.ssm.CacheFactory; 
import com.google.code.ssm.api.ParameterValueKeyProvider; 
import com.google.code.ssm.api.ReadThroughSingleCache; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:memcached-context.xml" }) 
public class MemcachedTest { 

    @Test 
    public void testMemcached() { 
     Assert.assertTrue(getComplexObjectFromDB("test").size() == 1); 
     Assert.assertTrue(getComplexObjectFromDB("test").size() == 1); 
    } 

    private List<String> getSA(){ 

     List<String> l = new ArrayList<String>(); 
     System.out.println("Adding to cache"); 
     l.add("test"); 

     return l; 
    } 

    @ReadThroughSingleCache(namespace = "CplxObj", expiration = 3600) 
    public List<String> getComplexObjectFromDB(@ParameterValueKeyProvider String id) { 

     return getSA(); 
    } 

    @Autowired 
    private CacheFactory defaultMemcachedClient; 

} 

memcached를-context.xml에 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <mvc:annotation-driven /> 

    <context:annotation-config /> 

<import resource="simplesm-context.xml" /> 
    <aop:aspectj-autoproxy /> 

    <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"> 
      <property name="cacheClientFactory"> 
       <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /> 
      </property> 
      <property name="addressProvider"> 
       <bean class="com.google.code.ssm.config.DefaultAddressProvider"> 
        <property name="address" value="xx.xx.xx.xx:11211" /> 
       </bean> 
      </property> 
      <property name="configuration"> 
       <bean class="com.google.code.ssm.providers.CacheConfiguration"> 
         <property name="consistentHashing" value="true" /> 
       </bean> 
      </property> 
    </bean> 

</beans> 

캐시가 목록에 추가되면 "캐시에 추가 중"이라는 메시지가 한 번만 나타납니다. 반환해야하며 목록을 다시 인스턴스화해서는 안됩니다? 내 구현이 맞습니까?

업데이트 : @raznor 제안에 따라

여기이 예상대로 작동하는 것 같다

:

memcached를-context.xml에 :

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <mvc:annotation-driven /> 

    <context:annotation-config /> 

<import resource="simplesm-context.xml" /> 
    <aop:aspectj-autoproxy /> 

    <bean name="memcachedClient" class="com.memcached.MemcachedClient"> 
    </bean> 

    <bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"> 
      <property name="cacheClientFactory"> 
       <bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /> 
      </property> 
      <property name="addressProvider"> 
       <bean class="com.google.code.ssm.config.DefaultAddressProvider"> 
        <property name="address" value="1.1.1.1:11211" /> 
       </bean> 
      </property> 
      <property name="configuration"> 
       <bean class="com.google.code.ssm.providers.CacheConfiguration"> 
         <property name="consistentHashing" value="true" /> 
       </bean> 
      </property> 
    </bean> 

</beans> 

MemcachedTest.java :

import java.util.ArrayList; 
import java.util.List; 

import junit.framework.Assert; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

import com.google.code.ssm.CacheFactory; 
import com.google.code.ssm.api.ParameterValueKeyProvider; 
import com.google.code.ssm.api.ReadThroughSingleCache; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:memcached-context.xml" }) 
public class MemcachedTest { 

    @Test 
    public void testMemcached() { 

     Assert.assertTrue(mClient.getComplexObjectFromDB("test1").size() == 1); 
     Assert.assertTrue(mClient.getComplexObjectFromDB("test1").size() == 1); 
    } 

    @Autowired 
    private MemcachedClient mClient; 

} 

MemcachedClient.java :

package com.memcached; 

import java.util.ArrayList; 
import java.util.List; 

import com.google.code.ssm.api.ParameterValueKeyProvider; 
import com.google.code.ssm.api.ReadThroughSingleCache; 

public class MemcachedClient { 

    @ReadThroughSingleCache(namespace = "CplxObj", expiration = 3600) 
    public List<String> getComplexObjectFromDB(
      @ParameterValueKeyProvider String id) { 

     return getSA(); 
    } 

    private List<String> getSA() { 

     List<String> l = new ArrayList<String>(); 
     System.out.println("Adding to cache"); 
     l.add("test"); 

     return l; 
    } 

} 

답변

1

자체 호출이 도청되지 않기 때문에 작동하지 않습니다. 이 작업을 수행하려면 getComplexObjectFromDB 메소드를 Spring Bean으로 분리하여이 bean을 사용하여 테스트에서 호출하십시오. myBean.getComplexObjectFromDB.

+0

감사합니다. 지금은 저에게 도움이되는 것 같습니다. 질문이 업데이트되었습니다. 내 임 플레 멘 테이션이 맞는지 확인할 수 있니? Spring bean defaultMemcachedClient는 "Simple Spring Memcached"자체를 구성하는 데 필요하지만 사용자 정의 클라이언트 코드에서 직접 참조하면 안됩니다. –

+0

예, 사용 가능한 공급자 중 일부를 사용하여 memcached 인스턴스에 물리적 연결 및 설정을 제공하려면 defaultMemcachedClient와 같은 bean이 필요합니다. 코드에서 참조 할 필요는 없지만 get, set, add ..., memcached 명령에 직접 액세스하려면 ... – ragnor

+0

구현이 올바른 것입니다. Spring 캐시와 SSM을 통합하고 @Cacheable 주석을 사용할 수도 있습니다. – ragnor

관련 문제