1. 程式人生 > 實用技巧 >專案總結71:Caffeine的基本概念和使用

專案總結71:Caffeine的基本概念和使用

專案總結71:Caffeine的基本概念和使用

start

pom引用

        <!-- Spring boot Cache-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>1.5.12.RELEASE</version>
        </dependency>
        <!--for
caffeine cache--> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.7.0</version> </dependency>

  application.properties配置

self.caffeine.maxsize=1000
self.caffeine.expire.time
=100

  CaffeineConfig配置類

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; //https://www.cnblogs.com/fnlingnzb-learner/p/11025565.html @EnableCaching @Configuration public class CaffeineCacheConfig { @Value("${self.caffeine.maxsize}") public Long maximumSize; @Value("${self.caffeine.expire.time}") public Long expireTime; @Bean("caffeineCacheManager") public CacheManager caffeineCacheManager(){ CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine( Caffeine.newBuilder()//建立Caffeine例項(並使用預設配置) .recordStats()//開啟預設打點監控策略 .expireAfterWrite(maximumSize, TimeUnit.SECONDS)//每次寫入10秒後過期 .maximumSize(expireTime));//快取最大條數 return cacheManager; } }

  service類

public interface CaffeineService {

    void putValue(String cacheName,Object key, Object value);

    <T>T getValue(String cacheName, String key);
}

@Service
public class CaffeineServiceImpl implements CaffeineService {

    @Autowired
    private CacheManager caffeineCacheManager;

    private final static String DEFAULT_CACHE = "default";

    @Override
    public void putValue(String cacheName,Object key, Object value) {
        if(key == null || value == null){
            return;
        }
        if(cacheName == null){
            cacheName = DEFAULT_CACHE;
        }
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null){
            cache.put(key,value);
        }
    }

    @Override
    public <T> T getValue(String cacheName, String key) {
        if(cacheName == null || key == null){
            return null;
        }
        Cache cache = caffeineCacheManager.getCache(cacheName);
        if(cache != null){
            Cache.ValueWrapper valueWrapper = cache.get(key);
            if(valueWrapper != null){
                return (T)valueWrapper.get();
            }
        }
        return null;
    }
}

  controller類

@RestController
@RequestMapping(value="/caffeine")
public class CaffeineController {

    @Autowired
    private CaffeineService caffeineService;

    //http://localhost:8080/caffeine/put?key=k1&value=v1
    @GetMapping("/put")
    public String put(
            @RequestParam(value="cacheName",required = false)String cacheName,
            @RequestParam String key,
            @RequestParam String value
    ){
        caffeineService.putValue(cacheName,key,value);
        return "success";
    }


    //http://localhost:8080/caffeine/get?key=k1&cacheName=default
    @GetMapping("/get")
    public String get(
            @RequestParam String cacheName,
            @RequestParam String key
    ){
        String value = caffeineService.getValue(cacheName, key);
        return "success: " + value;
    }

}

end