J2Cache根据开发环境依赖配置

前言

J2cache是oschina站长红薯前辈写的一套牛逼框架,但里面留了很多的坑。生产环境一直用该框架做了一二级缓存,但是就在前几天生产突然爆了redis超时,无奈找不到原因。我本地debug发现找不到配置文件。说到这真的很奇怪,我按着网上说的在properties中加入了j2cache.config-location配置并制定不同开发环境对应的配置文件,可是当我debug到J2Cache.class类的时候始终找到的是j2cache.properties文件。很郁闷,为什么找不到了,而且生产都大半年过去都没有问题,为啥突然有这个问题。如果有朋友知道可以帮忙留言下。于是乎,我就打算改版了,因为我要依赖开发环境,比如要上配置中心,那肯定需要改版的。

新建一个J2CacheSelfConfig文件:为了能够读取依赖环境中的文件名称例如application-uat.properties中增加一行j2cache.config-location=/j2cache-uat.properties传入到J2CacheConfig中

import lombok.Data;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.miaodou.util.J2Cache;

@Data
@Configuration
public class J2CacheSelfConfig {
    private  CacheChannel cache;
    @Value("${j2cache.config-location}")
    private String j2CacheFileLocaltion;
    @Bean
    public CacheChannel getCache(){
       return  J2Cache.getChannel(j2CacheFileLocaltion);
    }
}

拷贝J2Cache.class重新加入项目中


import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheException;
import net.oschina.j2cache.J2CacheBuilder;
import net.oschina.j2cache.J2CacheConfig;

import java.io.IOException;

/**
 * J2Cache 的缓存入口
 * @author Winter Lau(javayou@gmail.com)
 */
@Slf4j
public class J2Cache {

	private final static String CONFIG_FILE = "/j2cache.properties";

	private static J2CacheBuilder builder;

	/*static {
		try {
			System.out.println("Has been loader j2cache configuration: " + CONFIG_FILE);
            J2CacheConfig config = J2CacheConfig.initFromConfig(CONFIG_FILE);
            builder = J2CacheBuilder.init(config);
		} catch (IOException e) {
			throw new CacheException("Failed to load j2cache configuration " + CONFIG_FILE, e);
		}
	}*/

	/**
	 * 返回缓存操作接口
	 * @return CacheChannel
	 */
	public static CacheChannel getChannel(String j2CacheFileLocaltion){

			try {
				log.info(" Has been loaded by j2cache configuration: {}", j2CacheFileLocaltion);
				J2CacheConfig config = J2CacheConfig.initFromConfig(j2CacheFileLocaltion);
				builder = J2CacheBuilder.init(config);
				 return builder.getChannel();
			} catch (IOException e) {
				throw new CacheException("Failed to load j2cache configuration " + j2CacheFileLocaltion, e);
			}
	}

    /**
     * 关闭 J2Cache
     */
	public static void close() {
	    builder.close();
    }
}

将J2CacheUtils放入Spring容器中


import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class J2CacheUtils {
    @Autowired
    private CacheChannel cache;

    /**
     * 商城业务缓存
     */
    public static String SHOP_CACHE_NAME = "shopCache";
    /**
     * 系统缓存
     */
    private static String SYS_CACHE_NAME = "sysCache";

//    private static CacheChannel cache = J2Cache.getChannel();

    /**
     * 获取SYS_CACHE_NAME缓存
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        return get(SYS_CACHE_NAME, key);
    }

    /**
     * 写入SYS_CACHE_NAME缓存
     *
     * @param key
     * @return
     */
    public void put(String key, Object value) {
        put(SYS_CACHE_NAME, key, value);
    }

    /**
     * 从SYS_CACHE_NAME缓存中移除
     *
     * @param key
     * @return
     */
    public void remove(String key) {
        remove(SYS_CACHE_NAME, key);
    }

    /**
     * 获取缓存
     *
     * @param cacheName
     * @param key
     * @return
     */
    public Object get(String cacheName, String key) {
        return cache.get(cacheName, key).getValue();
    }

    /**
     * 写入缓存
     *
     * @param cacheName
     * @param key
     * @param value
     */
    public void put(String cacheName, String key, Object value) {
        cache.set(cacheName, key, value);
    }

    /**
     * 写入缓存
     *
     * @param cacheName
     * @param key
     * @param value
     */
    public void put(String cacheName, String key, Object value, long time) {
        cache.set(cacheName, key, value, time);
    }

    /**
     * 从缓存中移除
     *
     * @param cacheName
     * @param key
     */
    public void remove(String cacheName, String key) {
        cache.evict(cacheName, key);
    }

    /**
     * 获取SYS_CACHE缓存的所有key
     *
     * @return
     */
    public Collection<String> keys() {
        return cache.keys(SYS_CACHE_NAME);
    }
    /**
     * 获取缓存的所有key
     *
     * @param cacheName
     * @return
     */
    public Collection<String> keys(String cacheName) {
        return cache.keys(cacheName);
    }

    /**
     * Clear the cache
     *
     * @param cacheName: Cache region name
     */
    public void clear(String cacheName) {
        cache.clear(cacheName);
    }

    /**
     * 判断某个缓存键是否存在
     *
     * @param region Cache region name
     * @param key    cache key
     * @return true if key exists
     */
    public boolean exists(String region, String key) {
        return check(region, key) > 0;
    }

    /**
     * 判断某个缓存键是否存在
     *
     * @param key    cache key
     * @return true if key exists
     */
    public boolean exists(String key) {
        return check(SYS_CACHE_NAME, key) > 0;
    }

    /**
     * 判断某个key存在于哪级的缓存中
     *
     * @param region cache region
     * @param key    cache key
     * @return 0(不存在), 1(一级), 2(二级)redis
     */
    public int check(String region, String key) {
        return cache.check(region, key);
    }

}

 

至此结束:

需要使用J2CacheUtils的话就直接@Autowired依赖进去就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值