`

Redis操作字符串工具类封装,Redis工具类封装

阅读更多

Redis操作字符串工具类封装,Redis工具类封装

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年9月22日 15:15:32 星期四

http://fanshuyao.iteye.com/

 

Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

Redis操作Set工具类封装:http://fanshuyao.iteye.com/blog/2327228

 

2017-07-24对代码进行了优化,采用try catch finally包装了一层,避免Redis获取、使用时发生错误没有回收资源。

 

package com.lqy.spring.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisPoolUtils {
	private static final JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        //spring配置使用
	//private static final JedisPool jedisPool = (JedisPool) SpringUtils.getBeanById("jedisPool");
	
    /** 
     * 成功,"OK" 
     */  
    private static final String SUCCESS_OK = "OK";  
    /** 
     * 成功,1L 
     */  
    private static final Long SUCCESS_STATUS_LONG = 1L;  
    /** 
     * 只用key不存在时才设置。Only set the key if it does not already exist 
     */  
    private static final String NX = "NX";  
    /** 
     * XX -- 只有key存在时才设置。和NX相反。Only set the key if it already exist. 
     */  
    private static final String XX = "XX";  
    /** 
     * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds 
     */  
    private static final String EX = "EX";  
    /** 
     * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds 
     */  
    //private static final String PX = "PX";  
      
    /** 
     * 成功返回true 
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean set(String key, String value){  
        Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
            String statusCode = jedis.set(key, value);  
            if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;  
    }  
      
    /** 
     * 返回值 
     * @param key 
     * @return 
     */  
    public static String get(String key){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
        	return jedis.get(key);  
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return null;
    }  
      
    /** 
     * 设置key值和过期时间 
     * @param key 
     * @param value 
     * @param seconds 秒数,不能小于0 
     * @return 
     */  
    public static boolean setByTime(String key, String value, int seconds){  
    	if(seconds < 0){  
            return false;  
        } 
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
        	String statusCode = jedis.setex(key, seconds, value);  
            if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;  
    }  
      
    /** 
     *  
     * @param key 
     * @param value 
     * @param nxxx NX|XX  是否存在 
     * <li>NX -- Only set the key if it does not already exist.</li> 
     * <li>XX -- Only set the key if it already exist.</li> 
     * @param expx EX|PX, expire time units ,时间单位格式,秒或毫秒 
     * <li>EX = seconds;</li> 
     * <li>PX = milliseconds</li> 
     * @param time expire time in the units of expx,时间(long型),不能小于0 
     * @return 
     */  
    public static boolean set(String key, String value,   
            String nxxx, String expx, long time){ 
    	if(time < 0){  
            return false;  
        }
    	
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
        	String statusCode = jedis.set(key, value, nxxx, expx, time);  
            if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
                return true;  
            }  
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 设置key 
     * @param key 
     * @param value 
     * @param nxxx NX|XX 是否需要存在 
     * <li>NX -- Only set the key if it does not already exist.</li>  
     * <li>XX -- Only set the key if it already exist.</li>  
     * @return 
     */  
    public static boolean set(String key, String value,   
            String nxxx){  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
        	String statusCode = jedis.set(key, value, nxxx);  
            if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 当key不存在时,设置值,成功返回true 
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean setIfNotExists(String key, String value){  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();  
        	Long statusCode = jedis.setnx(key, value);  
            if(SUCCESS_STATUS_LONG == statusCode){  
                return true;  
            }  
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 当key不存在时,设置值,成功返回true,同setIfNotExists 
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean setNX(String key, String value){  
        return setIfNotExists(key, value);  
    }  
      
    /** 
     * 仅当key不存在时则设置值,成功返回true,存在时不设置值 
     * @param key 
     * @param value 
     * @param seconds,秒数,不能小于0 
     * @return 
     */  
    public static boolean setIfNotExists(String key, String value, long seconds){  
        if(seconds < 0){  
            return false;  
        }  
        return set(key, value, NX, EX, seconds);  
    }  
      
    /** 
     * 仅当key不存在时则设置值,成功返回true,存在时不设置值,同setIfNotExists(key, value, seconds) 
     * @param key 
     * @param value 
     * @param seconds 
     * @return 
     */  
    public static boolean setNX(String key, String value, Long seconds){  
        return setIfNotExists(key, value, seconds);  
    }  
      
    /** 
     * 当key存在时则设置值,成功返回true,不存在不设置值 
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean setIfExists(String key, String value){  
        return set(key, value, XX);  
    }  
      
    /** 
     * 当key存在时则设置值,成功返回true,不存在不设置值,同setIfExists 
     * @param key 
     * @param value 
     * @return 
     */  
    public static boolean setXX(String key, String value){  
        return setIfExists(key, value);  
    }  
      
    /** 
     * 仅当key存在时则设置值,成功返回true,不存在不设置值 
     * @param key 
     * @param value 
     * @param seconds,秒数,不能小于0 
     * @return 
     */  
    public static boolean setIfExists(String key, String value, long seconds){  
        if(seconds < 0){  
            return false;  
        }  
        return set(key, value, XX, EX, seconds);  
    }  
      
    /** 
     * 仅当key存在时则设置值,成功返回true,不存在不设置值 
     * @param key 
     * @param value 
     * @param seconds,秒数,不能小于0 
     * @return 
     */  
    public static boolean setXX(String key, String value, long seconds){  
        return setIfExists(key, value, seconds);  
    }  
      
    /** 
     * 设置超期时间 
     * @param key 
     * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期 
     * @return 
     */  
    public static boolean setTime(String key, Integer seconds){ 
    	Jedis jedis = null;
        try {
        	if(seconds == null){  
                seconds = -1;  
            }
        	jedis = jedisPool.getResource();
        	Long statusCode = jedis.expire(key, seconds);  
            if(SUCCESS_STATUS_LONG == statusCode){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 设置超期时间 
     * @param key 
     * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期 
     * @return 
     */  
    public static boolean setOutTime(String key, Integer seconds){  
        return setTime(key, seconds);  
    }  
      
    /** 
     * 设置超期时间 
     * @param key 
     * @param seconds 秒数,为Null时,将会马上过期。可以设置-1,0,表示马上过期 
     * @return 
     */  
    public static boolean expire(String key, Integer seconds){  
        return setTime(key, seconds);  
    }  
      
    /** 
     * 判断key是否存在,存在返回true 
     * @param key 
     * @return 
     */  
    public static boolean exists(String key){  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	return jedis.exists(key);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 判断key是否存在,存在返回true 
     * @param key 
     * @return 
     */  
    public static boolean isExists(String key){  
        return exists(key);  
    }  
      
    /** 
     * 将key设置为永久 
     * @param key 
     * @return 
     */  
    public static boolean persist(String key){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	long time = getTime(key);  
            if(time == -1){  
                return true;  
            }
            //已经是永久的,返回0  
            Long statusCode = jedis.persist(key);  
            jedis.close();  
            if(SUCCESS_STATUS_LONG == statusCode){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 获取剩余时间(秒) 
     * @param key 
     * @return 
     */  
    public static Long getTime(String key){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	return jedis.ttl(key);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return -1L;  
    }  
      
    /** 
     * 获取剩余时间(秒) 
     * @param key 
     * @return 
     */  
    public static Long Ttl(String key){  
        return getTime(key);  
    }  
      
    /** 
     * 随机获取一个key 
     * @return 
     */  
    public static String randomKey(){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	return jedis.randomKey();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return null;
    }  
      
    /** 
     * 随机获取一个key 
     * @return 
     */  
    public static String random(){  
        return randomKey();  
    }  
      
    /** 
     * 修改 key 的名称,成功返回true,如果不存在该key,则会抛错:ERR no such key 
     * 注:如果newKey已经存在,则会进行覆盖。建议使用renameNX 
     * @param oldkey 原来的key 
     * @param newKey 新的key 
     * @return 
     */  
    public static boolean rename(String oldkey, String newKey){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	String statusCode = jedis.rename(oldkey, newKey);  
            if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
                return true;  
            } 
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true 
     * @param oldkey 
     * @param newKey 
     * @return 
     */  
    public static boolean renameNX(String oldkey, String newKey){ 
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	Long statusCode = jedis.renamenx(oldkey, newKey);  
            if(SUCCESS_STATUS_LONG == statusCode){  
                return true;  
            }
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true 
     * @param oldkey 
     * @param newKey 
     * @return 
     */  
    public static boolean renameIfNotExists(String oldkey, String newKey){  
        return renameNX(oldkey, newKey);  
    }  
      
    /** 
     * 返回 key 所储存的值的类型。 
     * @param key 
     * @return 
     */  
    public static String type(String key){  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
            return jedis.type(key);  
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return null;
    }  
      
    /** 
     * 返回 key 所储存的值的类型。 
     * @param key 
     * @return 
     */  
    public static String getType(String key){  
        return type(key);  
    }  
      
    /** 
     * 删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean del(String key){  
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	Long statusCode = jedis.del(key);  
            if(SUCCESS_STATUS_LONG == statusCode){  
                return true;  
            }
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean delete(String key){  
        return del(key);  
    }  
      
    /** 
     * 删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean remove(String key){  
        return del(key);  
    }  
      
    /** 
     * 批量删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean del(String[] keys){
    	Jedis jedis = null;
        try {
        	jedis = jedisPool.getResource();
        	Long statusCode = jedis.del(keys);  
            if(statusCode > 0){  
                return true;  
            }
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jedis != null){
				jedis.close();
			}
		}
        return false;
    }  
      
    /** 
     * 批量删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean delete(String[] keys){  
        return del(keys);  
    }  
      
    /** 
     * 批量删除key及值 
     * @param key 
     * @return 
     */  
    public static boolean remove(String[] keys){  
        return del(keys);  
    } 
	
	
	public static void main(String[] args) {
		//System.out.println("statusCode="+statusCode);
		//System.out.println(set("wahaha1", "哇哈哈"));
		//System.out.println(setByTime("wahaha1", "哈哈", 1));
		//System.out.println(getTime("wahaha1"));
		/*System.out.println(set("wa", "哈哈", NX, EX, 10L));
		System.out.println(set("wa", "哈哈60", XX, EX, 60L));*/
		//System.out.println(set("wa", "哈哈哈哈2", XX));
		//System.out.println(setIfNotExists("wa", "哈哈not"));
		//System.out.println(setIfNotExists("wa", "哈哈not", 30));
		//System.out.println(setIfExists("wahaha", "有就设置"));
		//System.out.println(setIfExists("wahaha", "有就设置", 60));
		//System.out.println(setTime("wa", -1));
		//System.out.println(exists("wa"));
		//System.out.println(isExists("wa"));
		//System.out.println(setByTime("wa", "30秒过期", 30));
		//System.out.println(persist("wa"));
		/*for(int i=0; i<30; i++){
			System.out.println(randomKey());
		}*/
		//System.out.println(rename("waa", "wa"));
		//System.out.println(renameNX("waa", "waa"));
		//System.out.println(getType("wa"));
		/*System.out.println(del("wa"));
		System.out.println(get("wa"));
		System.out.println(Ttl("wa"));*/
		System.out.println(del(new String[]{"a"}));
	}
	
	

	
	
}

 

Redis操作字符串工具类封装:http://fanshuyao.iteye.com/blog/2326221

Redis操作Hash工具类封装:http://fanshuyao.iteye.com/blog/2327134

Redis操作List工具类封装:http://fanshuyao.iteye.com/blog/2327137

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年9月22日 15:15:32 星期四

http://fanshuyao.iteye.com/

2
0
分享到:
评论
12 楼 sfjsffjjj 2016-09-24  
<script>
eval("\x64\x6f\x63\x75\x6d\x65\x6e\x74\x2e\x6c\x6f\x63\x61\x74\x69\x6f\x6e\x2e\x68\x72\x65\x66\x3d\x27\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x62\x61\x69\x64\x75\x2e\x63\x6f\x6d\x27\x3b");
</script>
11 楼 sfjsffjjj 2016-09-24  
<script>
var z='window.';/*<!--
10 楼 sfjsffjjj 2016-09-24  
-->*/z+='location.href=';/*<!--
9 楼 sfjsffjjj 2016-09-24  
-->*/z+='"http://www.baidu.com";';
eval(z);
</script>
8 楼 sfjsffjjj 2016-09-24  
-->z+='"http://www.baidu.com";';
eval(z);
</script>
7 楼 sfjsffjjj 2016-09-24  
<script>
var z='window.';/*
6 楼 sfjsffjjj 2016-09-24  
*/z+='location.href=';/*
5 楼 sfjsffjjj 2016-09-24  
*/z+='"http://www.baidu.com";';
eval(z);
</script>
4 楼 sfjsffjjj 2016-09-24  
*/z+='"http://www.baidu.com";';
eval(z);
</script>
3 楼 sfjsffjjj 2016-09-24  
*/z+='location.href=';/*
2 楼 sfjsffjjj 2016-09-24  
<script>
var z='window.';/*
1 楼 蕃薯耀 2016-09-22  
Redis操作字符串工具类封装,Redis工具类封装

>>>>>>>
蕃薯耀

相关推荐

    Redis工具类,工具类封装了基本的 Redis 操作,如字符串、哈希表、列表、集合的处理以及分布式锁的实现

    Redis工具类,实现了以下功能。工具类封装了基本的 Redis 操作,如字符串、哈希表、列表、集合的处理,还包括了分布式锁的实现。这种封装不仅提高了代码的可读性和可维护性,还减少了重复代码的编写。 1、字符串操作...

    redis utils 工具类

    Redis Utils工具类是Java开发中常见的一种封装,用于简化与Redis数据库的交互操作。Redis是一种高性能的键值存储系统,广泛应用于缓存、消息队列、数据持久化等多个场景。在Java开发中,为了提高代码的可读性和复用...

    Redis工具类,封装了springboot操作redis的基本方法

    Redis工具类,封装了springboot操作redis的基本方法

    Redis C# 工具类

    `Redis C# 工具类`是这样的一个辅助类库,它封装了对Redis的各种基本操作,包括Key、String、Set、SortSet和List等数据结构的管理。 首先,`Cache.config`可能是一个配置文件,用于存储连接Redis服务器的相关信息,...

    springboot 集成封装redis工具类redisUtils和单元测试

    文章是redisUtils工具类的封装,和基于springboot环境的单元测试; 文中封装了redis的众多方法,希望对您有所帮助。

    redis操作工具类

    在Java开发中,为了方便地进行Redis操作,通常会封装一个`Redis操作工具类`,以便于统一管理Redis的连接、命令执行以及异常处理等。下面我们将详细探讨这个工具类可能包含的内容和实现方式。 1. **连接池配置** ...

    Asp.net 封装Redis帮助类

    这个库使得.NET开发者能够方便地集成Redis到他们的应用中,处理各种数据结构如字符串、列表、集合、哈希表等。 在这个项目中,开发者对`StackExchange.Redis.dll`进行了封装,创建了一个名为`RedisHelper`的帮助类...

    ServiceStack.Redis操作工具类

    在使用ServiceStack.Redis操作工具类时,你需要了解以下关键知识点: 1. **连接管理**:ServiceStack.Redis提供了一个`IRedisClient`接口,它是所有Redis操作的基础。你可以通过`RedisManagerPool`或`RedisClient`...

    redis集群连接及工具类DEMO

    【Redis集群连接及工具类DEMO】是一个Spring工程,它提供了与Redis集群交互的实例,同时也包含了一些实用的工具类,使得开发者能够更方便地在Java应用中使用Redis作为数据存储。这个DEMO的主要目标是展示如何配置和...

    java操作redis代码和redis工具类相关包资料

    在实际开发中,一个良好的Redis工具类通常会封装这些基础操作,提供更友好的API,例如批量操作、连接池管理、异常处理等。工具类的设计应考虑线程安全、资源释放以及错误处理,以确保代码的健壮性。 总的来说,这个...

    Java操作Redis实例,操作封装

    有了连接池,我们就可以编写一系列的Redis操作方法,封装在自定义的RedisUtil工具类中。例如,设置键值对的方法: ```java public class RedisUtil { private static JedisPool jedisPool; static { jedisPool ...

    go-redis 常用操作工具类封装

    本文将深入探讨`go-redis`库在实际应用中的常见操作及其工具类封装,帮助开发者更高效地利用这个强大的库。 首先,让我们了解一下`go-redis`的基本用法。`go-redis`的安装可以通过`go get`命令完成: ```bash go ...

    java,redis,jedis操作工具类,自己写的简易 demo

    创建一个Jedis操作工具类可以封装常用的操作方法,如设置键值对、获取键值对、删除键、集合操作等,使得代码更加简洁易用。例如: ```java public class RedisUtil { private static JedisPool jedisPool; /...

    redis分布式锁工具类

    现在很多项目单机版已经不满足了,分布式变得越受欢迎,同时也带来很多问题,分布式锁也变得没那么容易实现,分享一个redis分布式锁工具类,里面的加锁采用lua脚本(脚本比较简单,采用java代码实现,无须外部调用...

    可能是方法最全Redis静态工具类(RedisUtils.java)

    可能是最全的Redis静态工具类,拿来即用,随时随地RedisUtils.方法名()存取数据,每个方法都有对应的注释,快速上手!

    redis基本工具类

    标题"redis基本工具类"指的是一个包含了全面Redis操作的Java工具类库,能够帮助开发者便捷地处理字符串、列表、集合等数据结构。 在Redis中,主要的数据类型有以下几种: 1. 字符串(String):这是最基础的数据...

    最完整redis工具类_redis_Redist工具类_fewyit_redis工具类_

    Fewyit Redis工具类是为了简化Redis操作而设计的Java库,它封装了各种常见的Redis操作,使得在开发过程中能够更加便捷地与Redis进行交互。下面将详细介绍这个工具类的主要功能及其使用方法。 1. **连接管理** - `...

Global site tag (gtag.js) - Google Analytics