RedisAPI
博客专区 > Curry_M 的博客 > 博客详情
RedisAPI
Curry_M 发表于6个月前
RedisAPI
  • 发表于 6个月前
  • 阅读 3
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

 

package com.pingan.common.util;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * Redis操作接口
 */
public class RedisAPI {

    public static final Logger logger = Logger.getLogger(RedisAPI.class);

    private static ShardedJedisPool shardedJedisPool;

    public ShardedJedisPool getShardedJedisPool() {
        return shardedJedisPool;
    }

    @Autowired
    public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
        RedisAPI.shardedJedisPool = shardedJedisPool;
    }

    public static ShardedJedis getRedisClient() {
    	ShardedJedis shardJedis =null;
        try {
        	shardJedis= shardedJedisPool.getResource();
            return shardJedis;
        } catch (Exception e) {
            logger.error("getRedisClent error", e);
        }
        return null;
    }

    public static void returnResource(ShardedJedis shardedJedis) {
        shardedJedisPool.returnResource(shardedJedis);
    }

    public static void returnBrokenResource(ShardedJedis shardedJedis) {
        shardedJedisPool.returnBrokenResource(shardedJedis);
    }

    public void returnResource(ShardedJedis shardedJedis, boolean broken) {
        if (broken) {
            shardedJedisPool.returnBrokenResource(shardedJedis);
        } else {
            shardedJedisPool.returnResource(shardedJedis);
        }
    }

    /**
     * 获取数据
     * 
     * @param key
     * @return
     */
    public static String get(String key) {
        String value = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            value = jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return value;
    }

    /**
     * 赋值数据
     * 
     * @param key
     * @return
     */
    public static String set(String key, String value) {
        String result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }

        return result;
    }

    /**
     * 删除数据
     * 
     * @param key
     * @return
     */
    public static Long del(String key) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 步增
     * 
     * @param key
     * @return
     */
    public static Long incrBy(String key, Long value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.incrBy(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }

        return result;
    }

    /**
     * 步减指定指
     * 
     * @param key
     * @return
     */
    public static Long decrBy(String key, Long value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.decrBy(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }

        return result;
    }

    /**
     * 步减1
     * 
     * @param key
     * @return
     */
    public static Long decr(String key) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.decr(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }

        return result;
    }
    
    /**
     * 判断set中是否有值value
     * 
     * @param set,value
     * @return
     */
    public static Boolean sismember(String set, String value) {
        Boolean result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.sismember(set, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 自增记数
     * 
     * @param key
     * @return
     */
    public static Long incr(String key) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            Long times = jedis.incr(key);
            //            if (times.longValue() == 1l)
            //                jedis.expire(key, seconds);
            result = times;
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * set赋值数据
     * 
     * @param key,value
     * @return
     */
    public static Long sadd(String key, String value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.sadd(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * 查询set集中的数量
     * 
     * @param key,value
     * @return
     */
    public static Long scard(String key) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.scard(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * set移除数据
     * 
     * @param key,value
     * @return
     */
    public static Long srem(String key, String value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.srem(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取Set中所有数据
     * 
     * @param key
     * @return
     */
    public static Set<String> smembers(String key) {
        Set<String> result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.smembers(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 随机返回set中的一个元素
     * 
     * @param set
     * @return
     */
    public static String spop(String set) {
        String result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.spop(set);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 设置过期时间
     * 
     * @param set
     * @return
     */
    public static Long expire(String key, int seconds) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.expire(key, seconds);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * list赋值数据
     * 
     * @param key,value
     * @return
     */
    public static Long lpush(String key, String value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.lpush(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * list赋值数据
     * 
     * @param key,value
     * @return
     */
    public static Long llen(String key) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.llen(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * list取值列表数据
     * 
     * @param key,value
     * @return
     */
    public static List<String> lrange(String key, long start, long end) {
        List<String> result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.lrange(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * list取值数据
     * 
     * @param key,value
     * @return
     */
    public static String lpop(String key) {
        String result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.lpop(key);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    
    /**
     * hash设值数据
     * 
     * @param key,value
     * @return
     */
    public static Long hset(String key, String field, String value) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.hset(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * hash取值数据
     * 
     * @param key,value
     * @return
     */
    public static String hget(String key, String field) {
        String result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.hget(key, field);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
    
    /**
     * hash删值数据
     * 
     * @param key,value
     * @return
     */
    public static Long hdel(String key, String field) {
        Long result = null;
        ShardedJedis jedis = null;
        try {
            jedis = getRedisClient();
            result = jedis.hdel(key, field, field);
        } catch (Exception e) {
            e.printStackTrace();
            returnBrokenResource(jedis);
        } finally {
            returnResource(jedis);
        }
        return result;
    }
}

 

标签: Redis
共有 人打赏支持
粉丝 2
博文 38
码字总数 38900
×
Curry_M
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: