繁体   English   中英

Spring Boot无法设置缓存

[英]Spring Boot can not setup cache

尝试在Spring Boot项目中设置Memcache,但是它不起作用。 我什至尝试设置默认缓存(ConcurrentMapCacheManager),但效果不佳。

我读过的每个教程(甚至是Spring官方)都说此配置足以设置缓存

缓存配置:

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

某些依赖关系可能与Spring缓存依赖关系冲突吗?

如果您使用的是Memcached,则应将UserEntity 序列化 (即UserEntity实现Serializable )。

您可以在此处检查将Spring Boot与Memcached结合使用的小样本项目。

我刚刚创建了一个演示项目,以演示此简单的缓存配置的工作方式。

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

和一个虚拟控制器。

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

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

我在github中添加了代码,以便您看一下。

暂无
暂无

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

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