Caffeine實現(xiàn)類似redis的動態(tài)過期時間設置示例
使用Caffeine實現(xiàn)緩存功能
為了替換原有的redis接口,發(fā)現(xiàn)存在一個問題:
原本的redis支持set(key, value, expire)這樣的功能,而原始的caffeine只能全局設置過期策略,不支持類似的功能,因此需要對Caffeine包裝一下,做了一個工具類實現(xiàn)類似redis的動態(tài)過期時間設置。
工具類實現(xiàn)類似redis
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Expiry; import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import org.springframework.stereotype.Component; import java.util.Collection; import java.util.concurrent.TimeUnit; import static java.util.Objects.isNull; /** * 工具類 * * @author guo */ public class CacheUtils { /** * 不設置過期時長 */ public static final long NOT_EXPIRE = Long.MAX_VALUE; public static final Cache<String, CacheObject<?>> CAFFEINE = Caffeine.newBuilder() .expireAfter(new Expiry<String, CacheObject<?>>() { @Override public long expireAfterCreate(@NonNull String key, @NonNull CacheObject<?> value, long currentTime) { return value.expire; } @Override public long expireAfterUpdate(@NonNull String key, @NonNull CacheObject<?> value, long currentTime, @NonNegative long currentDuration) { return value.expire; } @Override public long expireAfterRead(@NonNull String key, @NonNull CacheObject<?> value, long currentTime, @NonNegative long currentDuration) { return value.expire; } }) .initialCapacity(100) .maximumSize(1024) .build(); private static class CacheObject<T> { T data; long expire; public CacheObject(T data, long second) { this.data = data; this.expire = TimeUnit.SECONDS.toNanos(second); } } public static <T> void set(String key, T value, long expire) { CacheObject<T> cacheObject = new CacheObject<>(value, expire); CAFFEINE.put(key, cacheObject); } public static void set(String key, Object value) { set(key, value, NOT_EXPIRE); } @SuppressWarnings("unchecked") public static <T> T get(String key) { CacheObject<?> cacheObject = CAFFEINE.getIfPresent(key); if (isNull(cacheObject)) { return null; } return (T) cacheObject.data; } public static void delete(String key) { CAFFEINE.invalidate(key); } public static void delete(Collection<String> keys) { CAFFEINE.invalidateAll(keys); } public static void main(String[] args) throws InterruptedException { CacheUtils.set("A", 1, 2); CacheUtils.set("B", 2, 5); CacheUtils.set("C", 3, 8); System.out.println(CAFFEINE.asMap()); Object a = CacheUtils.get("A"); Object b = CacheUtils.get("B"); Object c = CacheUtils.get("C"); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println("-----------------"); Thread.sleep(2000); a = CacheUtils.get("A"); b = CacheUtils.get("B"); c = CacheUtils.get("C"); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println("-----------------"); Thread.sleep(5000); a = CacheUtils.get("A"); b = CacheUtils.get("B"); c = CacheUtils.get("C"); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println("-----------------"); Thread.sleep(8000); a = CacheUtils.get("A"); b = CacheUtils.get("B"); c = CacheUtils.get("C"); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println("-----------------"); } }
以上就是Caffeine實現(xiàn)類似redis的動態(tài)過期時間設置示例的詳細內(nèi)容,更多關(guān)于Caffeine動態(tài)過期時間設置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Redis中ServiceStack.Redis和StackExchange.Redis區(qū)別詳解
本文主要介紹了Redis中ServiceStack.Redis和StackExchange.Redis區(qū)別詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Redis數(shù)據(jù)庫的使用場景介紹(避免誤用Redis)
這篇文章主要介紹了Redis數(shù)據(jù)庫的使用場景介紹(避免誤用Redis),本文用簡要的語言總結(jié)了Redis數(shù)據(jù)庫的適應場合,人而避免錯誤的使用它而產(chǎn)生昂貴的維護代價,需要的朋友可以參考下2015-03-03關(guān)于使用IDEA的springboot框架往Redis里寫入數(shù)據(jù)亂碼問題
這篇文章主要介紹了用IDEA的springboot框架往Redis里寫入數(shù)據(jù)亂碼問題,本文給大家分享解決方法通過圖文示例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10Redis不是一直號稱單線程效率也很高嗎,為什么又采用多線程了?
這篇文章主要介紹了Redis不是一直號稱單線程效率也很高嗎,為什么又采用多線程了的相關(guān)資料,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03