文档章节

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
博文 85
码字总数 83163
作品 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

没有更多内容

加载失败,请刷新页面

加载更多

Linux虚拟机安装VmwareTool

1.点击虚拟机: 2.点击安装Vmware-Tools,出现如下界面,将圈起来的拖动到桌面 3.接着打开终端,执行解压缩命令 tar -xzv -f VMwareTools-10.2.5-8068393.tar.gz 这个后面的VMwareTools-10.2....

陈刚生
24分钟前
0
0
Java学习笔记(1) --JSE 、JEE、 JME三者的区别

java SE : java standard edition , JSE。它允许允许开发和部署在桌面、服务器、嵌入式环境和实时环境中使用的java应用程序。 java SE包含了java Web 服务开发的类,并为java platform ,e...

hellation_
27分钟前
1
0
kafka 环境配置

https://www.cnblogs.com/fly-piglet/p/7762556.html

steel7c4
30分钟前
0
0
关于人工智能的思考

像人类一样可以思考,可以创造的人工智能会出现吗? 个人认为:会出现。但是出现的时间,取决了于硬件架构,和运算架构实现的时候。原因在于,人类智能可以出现,机器智能为什么不可以呢,不...

janl
32分钟前
0
0
破界!Omi生态omi-mp发布,用小程序开发生成Web

omi-mp 是什么 Omi 框架是微信支付线研发部和 AlloyTeam 开源的通用 Web 组件化框架,基于 Web Components,用来开发 PC、手机浏览器或者微信、手Q webview 的 Web 页面。自今年5月开源以来,...

腾讯开源
35分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部