Spring Boot基本缓存对我不起作用

时间:2015-11-11 19:13:44

标签: java spring-mvc caching spring-boot

我正在尝试对数据执行简单的缓存,但每次我都会测试它。它似乎永远不会缓存数据。

包app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;

@EnableAutoConfiguration
@SpringBootApplication
@EnableCaching
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

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

}

package app.cache;

import app.repo.User.User;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


@Service
public class UserService {

    private User userInfo;

    @CacheEvict("getUserInfo")
    public void setUserInfo(User userInfo) {
        this.userInfo = userInfo;
    }

    @Cacheable("getUserInfo")
    public User getUserInfo() {
        return userInfo;
    }
}

package app.controllers;

import app.cache.UserService;
import app.repo.User.Player;
import app.repo.User.User;
import app.repo.User.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
public class UserController {

    @Autowired
    private UserService userCache;


    @RequestMapping("/test-user-cache")
    @ResponseBody
    public User testUserCache() {
        if(userCache.getUserInfo() != null) {
            return userCache.getUserInfo();
        }
        User user = new User("joejoe","wordpass");
        user.setId(44444444);
        userCache.setUserInfo(user);
        return userCache.getUserInfo();
    }

}

依赖关系

...
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-server-jndi</artifactId>
            <version>1.5.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
    </dependencies>
...

当我尝试保存数据时似乎无法保存数据。当我再次请求时,不会缓存任何数据。

1 个答案:

答案 0 :(得分:0)

你得到的行为因为缓存有效。数据已保存,但null已缓存:if(userCache.getUserInfo()

为了在设置值时清除缓存,您可以使用setUserInfo注释@CacheEvict(cacheNames="books", allEntries=true)方法。