Redis使用实例(RedisHelper)

public class RedisHelper {
    private static JedisPool pool = null;
    static {
        if (pool == null) {
        JedisPoolConfig config = new JedisPoolConfig();
        // 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
            // 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
            config.setMaxTotal(500);
            // 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
            config.setMaxIdle(10);
            // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
            config.setTestOnBorrow(true);
            config.setTestOnReturn(true);
// 获取连接池 100000毫秒延时
            // pool = new JedisPool(config, "10.98.103.6", 6379, 100000);
            if (FrontedConst.Redis_IP != null
                    && FrontedConst.Redis_Port != null) {
                String Redis_IP = FrontedConst.Redis_IP;
                int Redis_Port = Integer.parseInt(FrontedConst.Redis_Port);
                pool = new JedisPool(config, Redis_IP, Redis_Port, 100000);
            } else {FEPViewUtils.getAwtUtil().writeLogger(
                        "Please check configuration file.");
            }
        }


/**
     * 构建redis连接池
     * 
     * @param ip
     * @param port
     * @return JedisPool
     */
    public static JedisPool getPool() {
        return pool;
    }

 /**
     * 返还到连接池
     * 
     * @param pool
     * @param redis
     */
 public static void returnResource(JedisPool pool, Jedis redis)
  {
    if (redis != null)
      pool.returnResource(redis);
  }


/**
     * 获取数据
     * 
     * @param key
     * @return
     */
public static String get(String key)
  {
    String value = null;

    JedisPool pool = null;
    Jedis jedis = null;
    try {
      pool = getPool();
      jedis = pool.getResource();
      value = jedis.get(key);
    }
    catch (Exception e) {
    // 释放redis对象
      pool.returnBrokenResource(jedis);
      e.printStackTrace();
    }
    finally {
    // 返还到连接池
      returnResource(pool, jedis);
    }

    return value;
  }

/**
     * 设置数据
     * 
     * @param key
     * @param value
     * @return
     */
  public static boolean set(String key, String value)
  {
    JedisPool pool = null;
    Jedis jedis = null;
    boolean bool = false;
    try {
      pool = getPool();
      jedis = pool.getResource();
      jedis.set(key, value);
      bool = true;
    }
    catch (Exception e) {
    // 释放redis对象
      pool.returnBrokenResource(jedis);
      e.printStackTrace();
    }
    finally {
    // 返还到连接池
      returnResource(pool, jedis);
    }
    return bool;
  }

  /**
     * 订阅频道
     * 
     * @param channelName
     * @throws Exception
     */
  public static void subscribe(BinaryJedisPubSub binaryJedisPubSub, byte[] channelName)
  {
    JedisPool pool = null;
    Jedis jedis = null;
    try {
      pool = getPool();
      jedis = pool.getResource();
      jedis.subscribe(binaryJedisPubSub, new byte[][] { channelName });
    }
    catch (Exception e) {
    // 释放redis对象
      pool.returnBrokenResource(jedis);
      e.printStackTrace();
    }
    finally {
    // 返还到连接池
      returnResource(pool, jedis);
    }
  }

/**
     * 取消订阅
     * 
     * @param binaryJedisPubSub
     * @return
     */
  public static boolean unsubscribe(BinaryJedisPubSub binaryJedisPubSub)
  {
    boolean bool = false;
    try
    {
      binaryJedisPubSub.unsubscribe();
      bool = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bool;
  }

/**
     * 取消订阅
     * 
     * @param binaryJedisPubSub
     * @param name
     * @return
     */
  public static boolean unsubscribeWithChannelName(BinaryJedisPubSub binaryJedisPubSub, String[] params)
  {
    boolean bool = false;
    byte[][] bytes = new byte[params.length][];
    int i = 0;
    try {
      String[] arr$ = params; int len$ = arr$.length; for (int i$ = 0; i$ < len$; ++i$) { String param = arr$[i$];
        bytes[i] = param.getBytes();
        ++i;
      }
      binaryJedisPubSub.punsubscribe(bytes);
      bool = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bool;
  }

/**
     * 推送信息
     * 
     * @param channelName
     * @param msg
     * @return
     */
  public static boolean publishMsgs(byte[] channelName, byte[] msg)
  {
    JedisPool pool = null;
    Jedis jedis = null;
    try {
      pool = getPool();
      jedis = pool.getResource();
      jedis.publish(channelName, msg);

      return true;
    }
    catch (Exception e)
    {
    // 释放redis对象
      pool.returnBrokenResource(jedis);
      e.printStackTrace();
      return false;

    }
    finally 
       { 
       // 返还到连接池
       returnResource(pool, jedis);
        }
  }

/**
     * 清空redis
     * @return
     */
public static boolean flushDB() {
        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = getPool();
            jedis = pool.getResource();
            jedis.flushDB();
            return true;
} catch (Exception e) {
            // 释放redis对象
            pool.returnBrokenResource(jedis);
            e.printStackTrace();
            return false;
        }finally {
            // 返还到连接池
            returnResource(pool, jedis);
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值