简体   繁体   中英

Spring Boot basic Cacheing is Not working for me

I am trying to do a simple cache of data but everytime I test it. It never seems to cache the data.

package 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

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

It doesnt seem to save the data when I try to save it. And when I do a request again, no data is never cached.

You get that behavior because the cache works. The data is saved, but null is already cached: if(userCache.getUserInfo()

In order to clear the cache when you set the value you can annotate the setUserInfo Method with @CacheEvict(cacheNames="books", allEntries=true) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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