美文网首页
Caffeine实现类似redis的动态过期时间

Caffeine实现类似redis的动态过期时间

作者: TinyThing | 来源:发表于2023-08-27 13:58 被阅读0次

1. 使用Caffeine实现缓存功能时,为了替换原有的redis接口,发现存在一个问题:

原本的redis支持set(key, value, expire)这样的功能,而原始的caffeine只能全局设置过期策略,不支持类似的功能,因此需要对Caffeine包装一下,做了一个工具类实现类似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实现类似redis的动态过期时间

      本文链接:https://www.haomeiwen.com/subject/tilwmdtx.html