文档章节

RedisAPI

Curry_M
 Curry_M
发布于 2017/04/10 14:17
字数 952
阅读 13
收藏 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;
    }
}

 

© 著作权归作者所有

共有 人打赏支持
Curry_M
粉丝 1
博文 88
码字总数 84728
作品 0
浦东
程序员
关于结构化多条件的方式一直

package com.fid.util; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; imp......

503
2017/09/21
14
0
JedisConnectionException: java.net.SocketTimeoutException: Read timed out

Exception in thread "Thread-2" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out at redis.clients.util.RedisInputStream.en......

wangxi得
2016/01/25
3.2K
3
关于从redis条件查询,表现形式

package com.ytwl.cms.basic.impl; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;......

503
2017/09/21
84
1
Redis笔记整理(二):Java API使用与Redis分布式集群环境搭建

[TOC] Redis Java API使用(一):单机版本Redis API使用 Redis的Java API通过Jedis来进行操作,因此首先需要Jedis的第三方库,因为使用的是Maven工程,所以先给出Jedis的依赖: 基本代码示例...

xpleaf
06/26
0
0
Jedis API 详细示例

Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库。 Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的数据保存在...

已月
01/16
0
0

没有更多内容

加载失败,请刷新页面

加载更多

20180920 rzsz传输文件、用户和用户组相关配置文件与管理

利用rz、sz实现Linux与Windows互传文件 [root@centos01 ~]# yum install -y lrzsz # 安装工具sz test.txt # 弹出对话框,传递到选择的路径下rz # 回车后,会从对话框中选择对应的文件传递...

野雪球
今天
1
0
OSChina 周四乱弹 —— 毒蛇当辣条

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @ 达尔文:分享花澤香菜/前野智昭/小野大輔/井上喜久子的单曲《ミッション! 健?康?第?イチ》 《ミッション! 健?康?第?イチ》- 花澤香菜/前野智...

小小编辑
今天
7
3
java -jar运行内存设置

java -Xms64m #JVM启动时的初始堆大小 -Xmx128m #最大堆大小 -Xmn64m #年轻代的大小,其余的空间是老年代 -XX:MaxMetaspaceSize=128m # -XX:CompressedClassSpaceSize=6...

李玉长
今天
3
0
Spring | 手把手教你SSM最优雅的整合方式

HEY 本节主要内容为:基于Spring从0到1搭建一个web工程,适合初学者,Java初级开发者。欢迎与我交流。 MODULE 新建一个Maven工程。 不论你是什么工具,选这个就可以了,然后next,直至finis...

冯文议
今天
2
0
RxJS的另外四种实现方式(四)——性能最高的库(续)

接上一篇RxJS的另外四种实现方式(三)——性能最高的库 上一篇文章我展示了这个最高性能库的实现方法。下面我介绍一下这个性能提升的秘密。 首先,为了弄清楚Most库究竟为何如此快,我必须借...

一个灰
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部