RedisCache.java 2.15 KB
Newer Older
张辰's avatar
张辰 committed
1 2
package com.tanpu.community.cache;

张辰's avatar
张辰 committed
3
import com.fasterxml.jackson.core.type.TypeReference;
张辰's avatar
张辰 committed
4
import com.tanpu.common.redis.RedisHelper;
张辰's avatar
张辰 committed
5
import com.tanpu.common.util.JsonUtil;
张辰's avatar
张辰 committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import com.tanpu.community.util.SpringUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.util.function.Supplier;

public class RedisCache {

    private String cacheName;
    private RedisHelper redisHelper;

    public<T> T getObject(String key, Integer expireSeconds, Supplier<T> func, Class<T> clz) {
        String value = get(key);
        // todo 考虑缓存穿透的问题.
        if (StringUtils.isNotBlank(value)) {
张辰's avatar
张辰 committed
21
            return JsonUtil.toBean(value, clz);
张辰's avatar
张辰 committed
22 23 24
        }

        T ret = func.get();
张辰's avatar
张辰 committed
25 26 27
        if (ret != null) {
            put(key, ret, expireSeconds);
        }
张辰's avatar
张辰 committed
28 29 30
        return ret;
    }

张辰's avatar
张辰 committed
31
    public<T> T getList(String key, Integer expireSeconds, Supplier<T> func, TypeReference<T> ref) {
张辰's avatar
张辰 committed
32 33
        String value = get(key);
        if (StringUtils.isNotBlank(value)) {
张辰's avatar
张辰 committed
34
            return JsonUtil.toBean(value, ref);
张辰's avatar
张辰 committed
35 36
        }

张辰's avatar
张辰 committed
37 38 39 40
        T ret = func.get();
        if (ret != null) {
            put(key, ret, expireSeconds);
        }
张辰's avatar
张辰 committed
41 42 43
        return ret;
    }

张辰's avatar
张辰 committed
44
    public void evict(String key) {
刘基明's avatar
刘基明 committed
45
        key = cacheName + ":" + key;
张辰's avatar
张辰 committed
46 47 48
        delete(key);
    }

张辰's avatar
张辰 committed
49 50 51 52 53 54 55
    private String get(String key) {
        key = cacheName + ":" + key;
        return redisHelper.get(key);
    }

    private void put(String key, Object obj, Integer expireSeconds) {
        key = cacheName + ":" + key;
张辰's avatar
张辰 committed
56
        String value = JsonUtil.toJson(obj);
张辰's avatar
张辰 committed
57 58 59 60 61 62 63
        if (expireSeconds == 0) {
            redisHelper.set(key, value);
        } else {
            redisHelper.set(key, value, Duration.ofSeconds(expireSeconds));
        }
    }

张辰's avatar
张辰 committed
64 65 66 67
    private void delete(String key) {
        redisHelper.delete(key);
    }

张辰's avatar
张辰 committed
68 69 70 71 72 73
    public static class Builder {
        RedisCache cache = new RedisCache();

        public Builder cacheName(String cacheName) {
            cache.cacheName = cacheName;
            cache.redisHelper = SpringUtils.getBean(RedisHelper.class);
张辰's avatar
张辰 committed
74
            return this;
张辰's avatar
张辰 committed
75 76 77 78 79 80 81
        }

        public RedisCache build() {
            return cache;
        }
    }
}