skip to Main Content

Try to setup Memcache in Spring Boot project, but it does not work. I even try to setup default cache(ConcurrentMapCacheManager), but it does not work as well.

Every tutorial that I read(even Spring official) say that this configuration is enought to setup cache

cache configuration:

@Configuration
@EnableCaching
public class CacheConfiguration {

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

}

Cache using:

@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>

Is it possible that some of the dependency conflict with spring cache dependency?

2

Answers


  1. I just created a demo project to demonstrate how this simple Cache Configuration is working.

    @Configuration
    @EnableCaching
    public class CachingConfig {
    
        @Bean
        public CacheManager cacheManager() {
            return new ConcurrentMapCacheManager("test_cache");
        }
    }
    

    The interface for the Service.

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

    The corresponding implementation.

    @Service
    @CacheConfig(cacheNames={"test_cache"})
    public class TestDataServiceImpl implements TestDataService {
    
        @Cacheable
        public String getEmail(String email) {
            return email;
        }
    }
    

    And a dummy Controller.

    @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");
        }
    }
    

    Then, when you call http request localhost:8080/test and you put a breakpoint into TestDataServiceImpl.getEmail() you can see that only the first time the method is actually being executed.

    I have added code in github in order to take a look if you wish.

    Login or Signup to reply.
  2. If you are using Memcached your UserEntity should be serialized (i.e. UserEntity implements Serializable).

    You can check a small sample project of using Spring Boot with Memcached here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search