简体   繁体   English

Spring Boot无法设置缓存

[英]Spring Boot can not setup cache

Try to setup Memcache in Spring Boot project, but it does not work. 尝试在Spring Boot项目中设置Memcache,但是它不起作用。 I even try to setup default cache(ConcurrentMapCacheManager), but it does not work as well. 我什至尝试设置默认缓存(ConcurrentMapCacheManager),但效果不佳。

Every tutorial that I read(even Spring official) say that this configuration is enought to setup cache 我读过的每个教程(甚至是Spring官方)都说此配置足以设置缓存

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 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? 某些依赖关系可能与Spring缓存依赖关系冲突吗?

If you are using Memcached your UserEntity should be serialized (ie UserEntity implements Serializable ). 如果您使用的是Memcached,则应将UserEntity 序列化 (即UserEntity实现Serializable )。

You can check a small sample project of using Spring Boot with Memcached here . 您可以在此处检查将Spring Boot与Memcached结合使用的小样本项目。

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. 然后,当您调用http请求localhost:8080/test并将断点放入TestDataServiceImpl.getEmail()您可以看到只有在第一次实际执行该方法时。

I have added code in github in order to take a look if you wish. 我在github中添加了代码,以便您看一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM