文档章节

RedisAPI

Curry_M
 Curry_M
发布于 2017/04/10 14:17
字数 952
阅读 15
收藏 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
浦东
程序员
私信 提问
LedisDB 0.4 版本发布,Go 实现的高性能 NoSQL

LedisDB 0.4 版本发布,该版本主要有如下改动: 解决了ttl key可能导致cpu 100%的bug,需要手动运行ledis-ttl-upgrade进行原有数据升级 重构replication,具体可参考基于rocksdb的性能报告,...

siddontang
2014/11/30
2.9K
10
Redis笔记整理(二):Java API使用与Redis分布式集群环境搭建

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

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

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

已月
2018/01/16
0
0

没有更多内容

加载失败,请刷新页面

加载更多

没有更多内容

a标签点击浏览器下载图片,不打开

网上好多给a标签加download属性的,但都不好使。 <!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Auth......

S三少S
16分钟前
26
0
搭建私有maven仓库并在项目中使用

这是一篇写给女朋友看的教程...前方高能,注意避让~ 1.私有maven仓库的搭建 搭建环境为阿里云ESC服务器,CentOS.确保服务器已经安装Jdk。然后我要手把手教你安装和启动nexus。 Step1:新建一个...

liululee
19分钟前
1
0
js 写文件

//写文件function doSave(value, type, name) {    var blob;    if (typeof window.Blob == "function") {        blob = new Blob([value], {type: type});    }......

重城重楼
28分钟前
2
0
Ubuntu and Apache 搭建 Let's Encrypt SSL Https

本文参考来着来自https://www.howtoing.com/install-free-lets-encrypt-ssl-certificate-for-apache-on-debian-and-ubuntu 感谢作者的分享 本事例环境介绍 ubuntu (Version 16.04.1 LTS) ubu......

罗培海
32分钟前
22
0
如何优雅的使用RabbitMQ

目录 RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具。消息队列的使用场景大概有3种: 1、系统集成,分布式系统的...

编程SHA
35分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部