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

org.yes.tools.cache.MybatisRedisCache Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.yes.tools.cache;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.springframework.beans.factory.annotation.Value;
import org.yes.tools.utils.RedisUtil;
import org.yes.tools.utils.SpringUtil;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Slf4j
public class MybatisRedisCache implements Cache {
    final static String NAME_SPACE = "mybatis-cache:";

    @Value("${spring.data.redis.mybatis-cache-sec:3600}")
    private int mybatisCacheSec;

    private final String id;
    /**
     * The {@code ReadWriteLock}.
     */
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public MybatisRedisCache(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        log.info("Mybatis-Redis-Cache:id=" + id);
        this.id = id;
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return this.readWriteLock;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public void putObject(Object key, Object value) {
        try {
            RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
            log.debug("mybatis cache putObject: key=" + key + ",value=" + value);
            if (null != value) {
                if (mybatisCacheSec > 0) {
                    // 这里简单起见, 先固定好了缓存的时长
                    // 也可以尝试 结合 在不同的mapper中指定特殊的缓存时长
                    // 也可以根据实际业务情况,制定缓存策略
                    redisUtil.set(NAME_SPACE + key.toString(), value, (long) mybatisCacheSec, TimeUnit.SECONDS);
                } else {
                    redisUtil.set(NAME_SPACE + key.toString(), value);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("redis保存数据异常!");
        }
    }

    @Override
    public Object getObject(Object key) {
        try {
            RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
            log.debug("mybatis cache etObject: key=" + key);
            int size = this.getSize();

            if (null != key) {
                // 这里很坑, 如果选用的redis序列化反序列化的方式不合适,在返回结果后可能会报类转换异常
                return redisUtil.get(NAME_SPACE + key.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("redis获取数据异常!");
        }
        return null;
    }

    @Override
    public Object removeObject(Object key) {
        try {
            RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
            if (null != key) {
                redisUtil.delete(NAME_SPACE + key.toString());
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("redis获取数据异常!");
        }
        return null;
    }

    @Override
    public void clear() {
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        Set keys = redisUtil.getKeys(NAME_SPACE + "*");
        if (CollectionUtil.isNotEmpty(keys)) {
            for (String key : keys) {
                redisUtil.delete(key);
            }
        }
        log.debug("mybatis cache clear");
    }

    @Override
    public int getSize() {
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        Set keys = redisUtil.getKeys(NAME_SPACE + "*");
        if (CollectionUtil.isNotEmpty(keys)) {
            return keys.size();
        }
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy