Guava cache
- github地址:点我直达
- 全内存的本地缓存实现
- 高性能且功能丰富
- 线程安全,操作简单
添加依赖
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
封装API
BaseCache.java
package net.ybclass.online_ybclass.utils; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component public class BaseCache { private Cache<String, Object> tenMinteCache= CacheBuilder.newBuilder() .initialCapacity(10) //设置缓存初始大小,应该合理设置,后期会扩容 .maximumSize(100) //最大值 .concurrencyLevel(5) //并发数 .expireAfterWrite(600, TimeUnit.SECONDS) //缓存过期时间,写入后10分钟过期 .recordStats() //统计缓存命中率 .build(); public Cache<String, Object> getTenMinteCache() { return tenMinteCache; } public void setTenMinteCache(Cache<String, Object> tenMinteCache) { this.tenMinteCache = tenMinteCache; } }