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

com.feingto.cloud.cache.provider.DefaultCacheProvider Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
package com.feingto.cloud.cache.provider;

import com.feingto.cloud.cache.ICache;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 缓存接口默认实现
 *
 * @author longfei
 */
@SuppressWarnings("unchecked")
public class DefaultCacheProvider implements ICache {
    protected Map data = new HashMap<>();
    private Map expires = new HashMap<>();
    private Map lasts = new HashMap<>();

    protected Object filterExpire(Object obj, String key) {
        return Optional.ofNullable(obj)
                .filter(v -> Objects.nonNull(expires.get(key)))
                .filter(v -> Objects.nonNull(lasts.get(key)))
                .filter(v -> expires.get(key) > 0)
                .filter(v -> System.currentTimeMillis() - lasts.get(key) > expires.get(key))
                .map(v -> {
                    v = null;
                    remove(key);
                    return v;
                })
                .orElse(obj);
    }

    @Override
    public  Map get() {
        return (Map) data;
    }

    @Override
    public  T get(String key) {
        return (T) this.filterExpire(data.get(key), key);
    }

    @Override
    public void put(String key, Object value) {
        this.put(key, value, -1);
    }

    @Override
    public void put(String key, Object value, long expireTime) {
        data.put(key, value);
        expires.put(key, expireTime);
        lasts.put(key, System.currentTimeMillis());
    }

    @Override
    public void put(String key, Object value, long expireTime, TimeUnit timeUnit) {
        switch (timeUnit) {
            case DAYS:
                expireTime *= 1000 * 1000 * 1000 * 1000L;
                break;
            case HOURS:
                expireTime *= 1000 * 1000 * 1000L;
                break;
            case MINUTES:
                expireTime *= 1000 * 1000L;
                break;
            case SECONDS:
                expireTime *= 1000L;
                break;
            case MILLISECONDS:
                break;
            case MICROSECONDS:
                expireTime *= 1 / 1000L;
                break;
            case NANOSECONDS:
                expireTime *= 1 / 1000 / 1000L;
                break;
        }
        this.put(key, value, expireTime);
    }

    @Override
    public Set keys() {
        return data.keySet();
    }

    @Override
    public boolean has(String key) {
        return data.containsKey(key);
    }

    @Override
    public void remove(String key) {
        data.remove(key);
        expires.remove(key);
        lasts.remove(key);
    }

    @Override
    public void removeByPrefix(String prefix) {
        this.keys().stream()
                .filter(key -> key.startsWith(prefix))
                .forEach(this::remove);
    }

    @Override
    public void clear() {
        data.clear();
        expires.clear();
        lasts.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy