2017-11-30 6 views
0

스프링 부트 프로젝트에서 Memcache를 설정하려고 시도하지만 작동하지 않습니다. 나도 기본 캐시 (ConcurrentMapCacheManager)를 설정하려고하지만 잘 작동하지 않습니다.스프링 부트 캐시를 설정할 수 없습니다.

나는 (심지어 공식 봄)를 읽어이 구성 설정 캐시에 한 정도로이라고 말할 때마다 튜토리얼

캐시 구성 :

@Configuration 
@EnableCaching 
public class CacheConfiguration { 

    @Bean 
    public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager("test_cache"); 
    } 

} 

캐시 사용 :

@Service 
@CacheConfig(cacheNames = "test_cache") 
public class UserServiceImpl extends IUserService { 
    ... 
    @Cacheable 
    public UserEntity getByEmail(String email) { 
     UserEntity entity = repository.findByEmail(email); 

     return entity; 
    } 
... 
} 

의 pom.xml을

<dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-aop</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-devtools</artifactId> 
      <version>1.5.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-validation</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>5.2.12.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>5.2.12.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hsqldb</groupId> 
      <artifactId>hsqldb</artifactId> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>commons-pool</groupId> 
      <artifactId>commons-pool</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>com.vladmihalcea</groupId> 
      <artifactId>hibernate-types-52</artifactId> 
      <version>1.1.1</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-collections</groupId> 
      <artifactId>commons-collections</artifactId> 
      <version>3.2.2</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>2.0.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>io.reactivex.rxjava2</groupId> 
      <artifactId>rxjava</artifactId> 
      <version>2.1.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-cache</artifactId> 
     </dependency> 
    </dependencies> 

종속성 중 일부가 스프링 캐시 종속성과 충돌 할 수 있습니까?

+0

'작동하지 않습니다'에 대해 자세히 설명해 줄 수 있습니까? 예외가 있습니까? –

+0

@RobertFarley, 같은 이메일 param에 대한 메소드 getByEmail이 매번 실행되지만 처음 후에 결과를 캐시해야합니다. –

+0

메소드'getByEmail'이 다른 bean에서 호출되었습니다. 컨트롤러에서? – lzagkaretos

답변

1

이 간단한 캐시 구성이 어떻게 작동하는지 보여주기 위해 데모 프로젝트를 만들었습니다.

@Configuration 
@EnableCaching 
public class CachingConfig { 

    @Bean 
    public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager("test_cache"); 
    } 
} 

서비스 인터페이스.

public interface TestDataService { 
    String getEmail(String email); 
} 

해당 구현.

@Service 
@CacheConfig(cacheNames={"test_cache"}) 
public class TestDataServiceImpl implements TestDataService { 

    @Cacheable 
    public String getEmail(String email) { 
     return email; 
    } 
} 

그리고 더미 컨트롤러. 당신이 HTTP 요청 localhost:8080/test를 호출하고 TestDataServiceImpl.getEmail()에 중단 점을 넣을 때

@RestController 
public class TestDataController { 

    private TestDataService testDataService; 

    @Autowired 
    public TestDataController(TestDataService testDataService) { 
     this.testDataService = testDataService; 
    } 

    @RequestMapping(value = "test") 
    String getEmail() { 
     return testDataService.getEmail("test.me"); 
    } 
} 

그렇다면, 당신은이 방법이 실제로 실행되고 처음에만 볼 수 있습니다.

원한다면 코드를 github에 추가했습니다. 당신이 직렬화해야 당신의 UserEntity을 memcached를 사용하는 경우에는

2

(즉 UserEntity는 Serializable를 구현).

Memcached here으로 스프링 부트를 사용하는 작은 샘플 프로젝트를 확인할 수 있습니다.