1. 程式人生 > >spring boot guava cache 緩存學習

spring boot guava cache 緩存學習

concat app tails ger art num article ole con

http://blog.csdn.net/hy245120020/article/details/78065676

************************************************************

spring boot guava cache 緩存學習

  1. 自定義key
  2. 自定義全局key過期時間,緩存個數
  3. 針對單個key自定義過期時間,緩存個數

引入依賴

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>
spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>

自定義key

 @Override
    @Cacheable(value 
= "user", key = "‘user‘.concat(#id.toString())") public User findUserById(Long id) { log.info("findUserById query from db, id: {}", id); return userMap.get(id); } @Override @CachePut(value = "user", key = "‘user‘.concat(#user.id.toString())") public void update(User user) { log.info(
"update db, user: {}", user.toString()); userMap.put(user.getId(), user); } @Override @CacheEvict(value = "user", key = "‘user‘.concat(#id.toString())") public void remove(Long id) { log.info("remove from db, id: {}", id); userMap.remove(id); }

自定義全局key過期時間,緩存個數

package com.km.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * <p>guava緩存配置</p>
 * Created by [email protected] on 2017/9/22.
 */
@Configuration
@EnableCaching
public class GuavaConfig {


    /**
     * 配置全局緩存參數,3600秒過期,最大個數1000
     */
    @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));
        return cacheManager;
    }

}

針對單個key自定義過期時間,緩存個數

package com.km.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * <p>guava緩存配置</p>
 * Created by [email protected] on 2017/9/22.
 */
@Configuration
@EnableCaching
public class GuavaConfig {

    private static final int DEFAULT_MAXSIZE = 1000;
    private static final int DEFAULT_TTL = 3600;

    /**
     * 定義cache名稱、超時時長秒、最大個數
     * 每個cache缺省3600秒過期,最大個數1000
     */
    public enum Caches {
        user(60, 2),
        info(5),
        role;

        Caches() {
        }

        Caches(int ttl) {
            this.ttl = ttl;
        }

        Caches(int ttl, int maxSize) {
            this.ttl = ttl;
            this.maxSize = maxSize;
        }

        private int maxSize = DEFAULT_MAXSIZE;    //最大數量
        private int ttl = DEFAULT_TTL;        //過期時間(秒)

        public int getMaxSize() {
            return maxSize;
        }

        public void setMaxSize(int maxSize) {
            this.maxSize = maxSize;
        }

        public int getTtl() {
            return ttl;
        }

        public void setTtl(int ttl) {
            this.ttl = ttl;
        }
    }

    /**
     * 個性化配置緩存
     */
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager manager = new SimpleCacheManager();
        //把各個cache註冊到cacheManager中,GuavaCache實現了org.springframework.cache.Cache接口
        ArrayList<GuavaCache> caches = new ArrayList<>();
        for (Caches c : Caches.values()) {
            caches.add(new GuavaCache(c.name(), CacheBuilder.newBuilder().recordStats().expireAfterWrite(c.getTtl(), TimeUnit.SECONDS).maximumSize(c.getMaxSize()).build()));
        }
        manager.setCaches(caches);
        return manager;
    }
}

配置yml

server:
  port: 8080
spring:
  cache:
    type: guava

配置啟動

package com.km;

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

@SpringBootApplication
@EnableCaching
public class SpringBootGuavaCacheApplication {

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

spring boot guava cache 緩存學習