All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qingzhuge.framework.cache.caffeine.CaffeineCache Maven / Gradle / Ivy

There is a newer version: 1.0.1.5
Show newest version
package com.qingzhuge.framework.cache.caffeine;

import com.qingzhuge.framework.cache.Level1Cache;
import com.github.benmanes.caffeine.cache.Cache;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author :zero.xiao
 * datetime :2018/1/14 16:01
 * 一级缓存实现
 **/
public class CaffeineCache implements Level1Cache {

    private Cache cache;
    private long size ;
    private long expire ;
    /**开启缓存*/
    private boolean open;

    CaffeineCache(Cache cache, long size, long expire,boolean open) {
        this.cache = cache;
        this.size = size;
        this.expire = expire;
        this.open = open;
    }

    @Override
    public long ttl() {
        return expire;
    }

    @Override
    public long size() { return size; }

    @Override
    public Object get(String key) {
        if (!open){
            return null;
        }
        return cache.getIfPresent(key);
    }

    @Override
    public void put(String key, Object value) {
        if (open) {
            cache.put(key, value);
        }
    }

    @Override
    public Collection keys() {
        if (!open){
            return null;
        }
        return cache.asMap().keySet();
    }

    @Override
    public void evict(String...keys) {
        if (!open){
            return;
        }
        cache.invalidateAll(Arrays.asList(keys));
    }

    @Override
    public void clear() {
        cache.invalidateAll();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy