java中Redis工具类



import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis工具类
 */
@Component("redisManager")
public class RedisManager implements InitializingBean, DisposableBean{
 
 private static final Logger log = LoggerFactory.getLogger(RedisManager.class);

 @Value("${redis.host}")
    private String host;

 @Value("${redis.port}")
    private int port;
 
 @Value("${redis.maxTotal}")
    private int maxTotal;

    @Value("${redis.maxIdle}")
    private int maxIdle;
   
    @Value("${redis.auth}")
    private String auth;
 
 private JedisPool jedisPool ;

    // 0 - never expire
    private int expire = 0;

    public RedisManager() {
    }
   
    /**
     * 初始化方法
     */
    public void init() {
     
    }

    /**
     * 初始化JedisPool
     */
    @Override
 public void afterPropertiesSet() throws Exception {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        jedisPool = new JedisPool(poolConfig, host, port);
 }

    /**
     * get String type value from redis
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        byte[] value = null;
        Jedis jedis = getInstance();
        try {
            value = jedis.get(key);
        }
        finally {
         jedis.close();
        }
        return value;
    }

    /**
     * set String type value to redis
     * @param key
     * @param value
     * @return
     */
    public byte[] set(byte[] key, byte[] value) {
        Jedis jedis = getInstance();
        try {
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        }
        finally {
         jedis.close();
        }
        return value;
    }

    /**
     * set String type value to redis with expire time
     * @param key
     * @param value
     * @param expire 过期时间
     * @return
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        Jedis jedis = getInstance();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        }
        finally {
         jedis.close();
        }
        return value;
    }

    /**
     * del
     * @param key
     */
    public void del(byte[] key) {
        Jedis jedis = getInstance();
        try {
            jedis.del(key);
        }
        finally {
         jedis.close();
        }
    }

    /**
     * flush 慎用
     */
    public void flushDB() {
        Jedis jedis = getInstance();
        try {
            jedis.flushDB();
        }
        finally {
         jedis.close();
        }
    }

    /**
     * size
     */
    public Long dbSize() {
        Long dbSize = 0L;
        Jedis jedis = getInstance();
        try {
            dbSize = jedis.dbSize();
        }
        finally {
         jedis.close();
        }
        return dbSize;
    }

    /**
     * keys
     * @param regex
     * @return
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        Jedis jedis = getInstance();
        try {
            keys = jedis.keys(pattern.getBytes());
        }
        finally {
         jedis.close();
        }
        return keys;
    }
   
    public void expire(byte[] key, int expire) {
        Jedis jedis = getInstance();
        try {
         jedis.expire(key, expire);
        }
        finally {
         jedis.close();
        }
    }

 @Override
 public void destroy() throws Exception {
  jedisPool.destroy();
        log.info("redis pool closed.");
 }
 
 private Jedis getInstance() {
        Jedis jedis = jedisPool.getResource();
        jedis.auth(auth);
        return jedis;
    }
 
 public String getHost() {
  return host;
 }

 public void setHost(String host) {
  this.host = host;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }

 public int getMaxTotal() {
  return maxTotal;
 }

 public void setMaxTotal(int maxTotal) {
  this.maxTotal = maxTotal;
 }

 public int getMaxIdle() {
  return maxIdle;
 }

 public void setMaxIdle(int maxIdle) {
  this.maxIdle = maxIdle;
 }

 public String getAuth() {
  return auth;
 }

 public void setAuth(String auth) {
  this.auth = auth;
 }

 public JedisPool getJedisPool() {
  return jedisPool;
 }

 public void setJedisPool(JedisPool jedisPool) {
  this.jedisPool = jedisPool;
 }

 public int getExpire() {
  return expire;
 }

 public void setExpire(int expire) {
  this.expire = expire;
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值