SpringBoot入門之緩存

緩存對減輕服務器壓力至關重要,這一篇簡單記錄一下。
緩存這一塊,主要用到了這4個註解:@EnableCaching, @Cacheable,@CachePut,@CacheEvict 。
其中@EnableCaching告訴Spring框架開啓緩存能力,@Cacheable來配置訪問路徑是否緩存,@CachePut來更新緩存,@CacheEvict來清理緩存,具體用法看下面的代碼。
1.開啓Spring的緩存功能java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching//開啓緩存
public class Application {

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

}

2.對訪問路徑(URL)配置緩存信息web

@GetMapping("/{id}")
    @Cacheable("users")//設置緩存用戶信息,緩存的名稱爲users
    public User getUserInfo(@PathVariable int id) {
        User user = new User();
        user.setId(id);
        int random=new Random().nextInt(999);
        user.setName("user " + random);
        System.out.println("random is "+random);
        return user;
    }

3.更新緩存信息spring

@PostMapping("/update")
    @CachePut(value ="users",key = "#user.getId()")//更新指定的用戶緩存信息
    public boolean updateUserInfo( User user){
        System.out.println("updateUserInfo "+user.toString());
        return true;
    }

4.清除緩存信息緩存

@PostMapping("/remove")
    @CacheEvict(value = "users",key = "#user.getId()")//清除指定的用戶緩存信息
    public boolean removeUserInfoCache(@RequestBody User user){
        System.out.println("removeUserInfoCache "+user.toString());
        return true;
    }

    @PostMapping("/remove/all")
    @CacheEvict(value = "users",allEntries = true)//清除全部的用戶緩存信息
    public boolean removeAllUserInfoCache(){
        System.out.println("removeAllUserInfoCache ");
        return true;
    }

下面放一個完整的緩存類demo服務器

package com.spring.cache;

import com.spring.beans.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * Created by zhangyi on 2017/4/13.
 *
 * 緩存demo
 *
 */
@RestController
@RequestMapping("/cache/user")
public class CacheController {

    @GetMapping("/{id}")
    @Cacheable("users")//設置緩存用戶信息,緩存的名稱爲users
    public User getUserInfo(@PathVariable int id) {
        User user = new User();
        user.setId(id);
        int random=new Random().nextInt(999);
        user.setName("user " + random);
        System.out.println("random is "+random);
        return user;
    }

    @PostMapping("/update")
    @CachePut(value ="users",key = "#user.getId()")//更新指定的用戶緩存信息
    public boolean updateUserInfo( User user){
        System.out.println("updateUserInfo "+user.toString());
        return true;
    }

    @PostMapping("/remove")
    @CacheEvict(value = "users",key = "#user.getId()")//清除指定的用戶緩存信息
    public boolean removeUserInfoCache(@RequestBody User user){
        System.out.println("removeUserInfoCache "+user.toString());
        return true;
    }

    @PostMapping("/remove/all")
    @CacheEvict(value = "users",allEntries = true)//清除全部的用戶緩存信息
    public boolean removeAllUserInfoCache(){
        System.out.println("removeAllUserInfoCache ");
        return true;
    }


}
相關文章
相關標籤/搜索