SpringBoot Series: Spring Boot integration Spring Cache

I, on the Spring Cache

Cache increasingly important in the present application,
the Spring version 3.1 defines org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces to unify the different cache technology, and support the use of JCache (JSR-107) annotations to simplify we have developed.

By SpringCache, you can quickly embed your Cache implementation, mainly @ Cacheable, @ CachePut, @ CacheEvict, @ CacheConfig, @ Caching and other annotations to achieve.

  • @Cacheable: acting on the method, the method returns to cache results, if the cache already exists, it is available directly from the cache, the cache can be specified from the key parameter, the value is cached method returns a value.
  • @CachePut: acting on the method, regardless of the presence of the cache, each time re-add the cache, the cache can be specified from the key parameters, the method returns the cached value is the value used for the updates.
  • @CacheEvict: acting on the method for clearing the cache.
  • @CacheConfig: acting on the class, class unified cache attribute this annotation configuration.
  • @Caching: acting on the method for setting multiple cache one time.
  • @EnableCaching: at class, it is used to enable annotations.

Second, demo

To use Spring Cache, you need to be introduced Spring Cache dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Cache依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

Then on startup class, we need to use @EnableCaching to declare open the cache.

@EnableCaching //开启缓存
@SpringBootApplication
public class SpringbootApplication {

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

}

This annotation can be used to operate the cache, create CacheService class, Map dataMap which store data, eliminating the operation of the database.

@Slf4j
@Service
public class CacheService {

    private Map<Integer, User> dataMap = new HashMap <Integer, User>(){
        {
            for (int i = 1; i < 100 ; i++) {
                User u = new User("code" + i, "name" + i);
                put(i, u);
            }
        }
     };

    // 获取数据
    @Cacheable(value = "cache", key = "'user:' + #id")
    public User get(int id){
        log.info("通过id{}查询获取", id);
        return dataMap.get(id);
    }

    // 更新数据
    @CachePut(value = "cache", key = "'user:' + #id")
    public User set(int id, User u){
        log.info("更新id{}数据", id);
        dataMap.put(id, u);
        return u;
     }
     
    //删除数据
    @CacheEvict(value = "cache", key = "'user:' + #id")
    public User del(int id){
        log.info("删除id{}数据", id);
        dataMap.remove(id);
        return u;
    }

}

get method for simulating a query, @ Cacheable for adding caching, set method for modifying, @ CachePut update the cache, del method for deleting data, @CacheEvict delete the cache. It should be noted that the annotated value represents cache classification, does not mean that the value of the object cache.

Then create CacheApi, used to call CacheService testing.

@RestController
@RequestMapping("cache")
public class CacheApi {

    @Autowired
    private CacheService cacheService;

    @GetMapping("get")
    public User  get(@RequestParam int id){
        return cacheService.get(id);
    }

    @PostMapping("set")
    public User  set(@RequestParam int id, @RequestParam String code, @RequestParam String name){
        User u = new User(code, name);
        return cacheService.set(id, u);
    }

    @DeleteMapping("del")
    public void  del(@RequestParam int id){
        cacheService.del(id);
    }

}

Then we open the swagger-ui interface (http: // localhost: 10900 / swagger-ui.html) test call query multiple times, you can see, CacheService get method, performed only once for the same id. Then call updates, get again to find the data has been updated, and calls del, you can clear the cache, the query will again call the method.

Source Address: https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

Guess you like

Origin www.cnblogs.com/imyanger/p/11723560.html