Java Redis和Memcached缓存

package com.panda.core.db.impl;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.impl.MemcachedConnector;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * redis memcached 缓存
 * Created by Lovell on 9/13/16.
 */

public class RMCacheService {
    private static Logger logger = LoggerFactory.getLogger(RMCacheService.class);

    private static String REDIS_CONFIG_FILE = "/redis.properties";
    private static String MEMCACHED_CONFIG_FILE = "/memcached.properties";

    // memcached 客户端
    private MemcachedClient memcachedClient;
    // memcached 缓存过期时间
    private int mcExpiredtime = 0;
    // memcached 缓存地址
    private String mcUrl = null;
    // memcached 端口
    private short mcPort = 0;
    // memcached 锁超时最长时间
    private int mcLocktimeout = 0;
    // memcached socket读取输入InputStream的超时时间
    private int mcConnTimeout = 0;
    // memcached
    private short mcConnPoolSize = 0;

    // redis 缓存过期时间
    private int rdExpiredtime = 0;
    // redis 缓存地址
    private String rdUrl = null;
    // redis 端口
    private short rdPort = 0;
    // redis 锁超时最长时间
    private int rdLocktimeout = 0;
    // redis socket读取输入InputStream的超时时间
    private int rdConnTimeout = 0;
    // redis 缓冲池
    private JedisPool jedisPool = null;

    private static RMCacheService rmCacheService;
    public static RMCacheService getInstance() {
        if (rmCacheService == null) {
            rmCacheService = new RMCacheService();
        }
        return rmCacheService;
    }

    /**
     * redis配置文件路径
     * @param path
     */
    public void setReidsConfigFilePathName(final String path) {
        if (path.charAt(0) == '/') {
            REDIS_CONFIG_FILE = path;
        } else {
            REDIS_CONFIG_FILE = "/" + path;
        }
    }

    /**
     * memcached配置文件路径
     * @param path
     */
    public void setMemcachedConfigFilePathName(final String path) {
        if (path.charAt(0) == '/') {
            MEMCACHED_CONFIG_FILE = path;
        } else {
            MEMCACHED_CONFIG_FILE = "/" + path;
        }
    }
    /**
     * 连接redis memcached
     */
    public void start () throws IOException {
        startRedis();
        startMemcached();
    }

    /**
     * 连接redis
     * @throws IOException
     */
    public void startRedis() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = CacheService.class.getClass().getResourceAsStream(REDIS_CONFIG_FILE);
        properties.load(inputStream);

        rdExpiredtime = Integer.valueOf(properties.getProperty("redis.expiredTime"));
        rdUrl = properties.getProperty("redis.url");
        rdPort = Short.valueOf(properties.getProperty("redis.port"));
        rdLocktimeout = Integer.valueOf(properties.getProperty("redis.lockTimeout"));

        JedisPoolConfig config = new JedisPoolConfig();
        jedisPool = new JedisPool(config, rdUrl, rdPort, rdConnTimeout);
        logger.info("redis client start ok");
        getJedis();
    }

    /**
     * 连接memcached
     * @throws IOException
     */
    public void startMemcached() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = CacheService.class.getClass().getResourceAsStream(MEMCACHED_CONFIG_FILE);
        properties.load(inputStream);

        mcExpiredtime = Integer.valueOf(properties.getProperty("memcached.expiredTime"));
        mcUrl = properties.getProperty("memcached.url");
        mcPort = Short.valueOf(properties.getProperty("memcached.port"));
        mcLocktimeout = Integer.valueOf(properties.getProperty("memcached.lockTimeout"));
        mcConnPoolSize = Short.valueOf(properties.getProperty("memcached.connPoolSize"));
        System.setProperty(MemcachedConnector.XMEMCACHED_SELECTOR_POOL_SIZE, "2");
        MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder(AddrUtil.getAddresses(mcUrl + mcPort));
        memcachedClientBuilder.setFailureMode(true);
        memcachedClientBuilder.setCommandFactory(new BinaryCommandFactory());
        memcachedClientBuilder.setConnectionPoolSize(mcConnPoolSize);
        memcachedClient = memcachedClientBuilder.build();
        logger.info("memcached client start ok");
    }

    /**
     * 关闭redis memcached
     */
    public void stop() throws IOException {
        stopRedis();
        stopMemcached();
    }

    /**
     * 关闭redis
     */
    public void stopRedis() {
        if (jedisPool != null) {
            jedisPool.destroy();
        }
    }

    /**
     * 关闭memcached
     * @throws IOException
     */
    public void stopMemcached() throws IOException {
        if(memcachedClient!=null){
            memcachedClient.shutdown();
        }
    }

    public JedisPool getJedisPool() {
        return  jedisPool;
    }

    /**
     * 连接测试redis服务器是否启动
     * @return
     */
    public Jedis getJedis() {
        try {
            return jedisPool.getResource();
        } catch (Exception e) {
            logger.error("redis server is not running");
        }
        return null;
    }

    public int getRedisExpiredTime() {
        return rdExpiredtime;
    }

    public int getRedisLocktimeout() {
        return rdLocktimeout;
    }

    public int getMemcacedExpiredTime() {
        return mcExpiredtime;
    }

    public int getMemcachedLocktimeout() {
        return mcLocktimeout;
    }
}

你可能感兴趣的:(Java)