java 操作redis 常用接口

java-redis 驱动操作

 

package com.loopxu.cache;
/**
 * redis 操作类
 * @author LX
 */

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.util.Pool;

public class RedisUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
	/*
	 * 是否为集群
	 */
	public static boolean isCluster = false;
	/*
	 * server信息
	 */
	public static String serverInfo = null;
	/*
	 * 在应用初始化的时候生成连接池
	 */
	private static Pool<Jedis> pool = null;
	/*
	 * server信息List
	 */
	private static List<String> serverList = new ArrayList<String>();

	// 在应用初始化的时候生成连接池
//		private static Pool<Jedis> pool = null;
//		private static boolean isCluster = false;
//		private static List<String> serverList = new ArrayList<String>();

	static {
		try {
			Properties prop = new Properties();
			InputStream in = RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties");
			prop.load(in);
			in.close();
			String mode = prop.getProperty("redis.mode");
			if ("cluster".equals(mode)) {
				// 集群模式
				isCluster = true;
			} else {
				// 单服务器模式
				isCluster = false;
			}
			String server = prop.getProperty("redis.servers");
			LOGGER.info("mode:" + mode + "\t servers:" + server);
			serverInfo = server;
			String[] servers = server.split(",");
			for (String str : servers) {
				serverList.add(str.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void init() {
		if (isCluster) {
			Set<String> sentinels = new HashSet<String>(16);
			if (serverList.size() > 0) {
				for (String server : serverList) {
					sentinels.add(server);
				}
			}
			GenericObjectPoolConfig config = new GenericObjectPoolConfig();
			// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
			config.setBlockWhenExhausted(true);
			// 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
			config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
			// 是否启用pool的jmx管理功能, 默认true
			config.setJmxEnabled(true);
			// 是否启用后进先出, 默认true
			config.setLifo(true);
			// 最大连接数, 默认8个
			config.setMaxTotal(1500);
			// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
			config.setMaxWaitMillis(-1);
			// 最大空闲连接数, 默认8个
			config.setMaxIdle(60);
			// 最小空闲连接数, 默认0
			config.setMinIdle(0);
			// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
			config.setMinEvictableIdleTimeMillis(1800000);
			// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
			config.setNumTestsPerEvictionRun(3);
			// 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断
			// (默认逐出策略)
			config.setSoftMinEvictableIdleTimeMillis(1800000);
			// 在获取连接的时候检查有效性, 默认false
			config.setTestOnBorrow(false);
			// 在空闲时检查有效性, 默认false
			config.setTestWhileIdle(false);
			// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
			config.setTimeBetweenEvictionRunsMillis(-1);
			pool = new JedisSentinelPool("redismaster", sentinels, config, 100000);
		} else {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxIdle(60);
			config.setMaxTotal(1500);
			config.setMaxWaitMillis(3 * 1000);
			String server = serverList.get(0);
			String[] temp = server.split(":");
			pool = new JedisPool(config, temp[0], Integer.parseInt(temp[1]));
		}
	}

	private static Pool<Jedis> getPool() {
		if (null == pool) {
			init();
		}
		return pool;
	}

	/**
	 * 
	 * @throws Exception
	 * @名称 get
	 * @描述 获取字符型key的value数据
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 String
	 * @作者
	 * @时间 2015年7月11日 下tokenToType午3:58:32
	 */
	public static String get(String key) {
		String value = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
//			throw e;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * 
	 * @名称 get
	 * @描述 获取字符型key的value数据
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 byte[]
	 * @作者
	 * @时间 2015年7月11日 下午4:00:41
	 */
	public static byte[] get(byte[] key) {
		byte[] value = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * 
	 * @名称 set
	 * @描述 设置字符型key和value
	 * @参数 @param key
	 * @参数 @param value
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:01:01
	 */
	public static void set(String key, String value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 set
	 * @描述 设置字符型key和value
	 * @参数 @param key
	 * @参数 @param value
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:01:43
	 */
	public static void set(byte[] key, byte[] value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 set
	 * @描述 设置字符型key和value
	 * @参数 @param key
	 * @参数 @param value
	 * @参数 @param time
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:01:43
	 */
	public static void set(String key, String value, int time) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
			jedis.expire(key, time);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 set
	 * @描述 设置字符型key和value 及过期时间
	 * @参数 @param key
	 * @参数 @param value
	 * @参数 @param time
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:02:22
	 */
	public static void set(byte[] key, byte[] value, int time) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
			jedis.expire(key, time);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 hset
	 * @描述 保存散列key的 field和 value
	 * @参数 @param key
	 * @参数 @param field
	 * @参数 @param value
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:02:51
	 */
	public static void hset(byte[] key, byte[] field, byte[] value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hset(key, field, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 hset
	 * @描述 保存散列key的 field和 value
	 * @参数 @param key
	 * @参数 @param field
	 * @参数 @param value
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:03:57
	 */
	public static void hset(String key, String field, String value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hset(key, field, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 mhset
	 * @描述 散列对象设置
	 * @参数 @param key
	 * @参数 @param field
	 * @参数 @param value
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 上午9:54:10
	 */
	public static void mhset(String key, Map<String, String> hash) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hmset(key, hash);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 llen
	 * @描述 查询list长度
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月11日 下午4:04:14
	 */
	public static long llen(String key) {
		Jedis jedis = null;
		long length = 0;
		try {
			pool = getPool();
			jedis = pool.getResource();
			length = jedis.llen(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return length;
	}

	/**
	 * 
	 * @名称 llen
	 * @描述 查询list长度
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月11日 下午4:51:57
	 */
	public static long llen(byte[] key) {
		long len = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.llen(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return len;
	}

	/**
	 * 
	 * @名称 hget
	 * @描述 查询散列key中field的value
	 * @参数 @param key
	 * @参数 @param field
	 * @参数 @return
	 * @返回值 String
	 * @作者
	 * @时间 2015年7月11日 下午4:04:47
	 */
	public static String hget(String key, String field) {
		String value = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.hget(key, field);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * 
	 * @名称 hdel
	 * @描述 散列删除
	 * @参数 @param key
	 * @参数 @param field
	 * @参数 @return
	 * @返回值 String
	 * @作者
	 * @时间 2015年11月10日 下午6:24:51
	 */
	public static Long hdel(String key, String field) {
		long value = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.hdel(key, field);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * 
	 * @名称 hgetAll
	 * @描述 查询散列key 所有键值对
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 Map<String,String>
	 * @作者
	 * @时间 2015年7月12日 上午11:44:01
	 */
	public static Map<String, String> hgetAll(String key) {
		Map<String, String> value = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.hgetAll(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public static byte[] hget(byte[] key, byte[] field) {
		byte[] value = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.hget(key, field);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	public static void hdel(byte[] key, byte[] field) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hdel(key, field);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void lpush(byte[] key, byte[] value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void lpush(String key, String value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.lpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void rpush(byte[] key, byte[] value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.rpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void rpush(String key, String value) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.rpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static String rpoplpush(String key, String destination) {
		Jedis jedis = null;
		String outValue = "";
		try {
			pool = getPool();
			jedis = pool.getResource();
			outValue = jedis.rpoplpush(key, destination);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return outValue;
	}

	public static void rpoplpush(byte[] key, byte[] destination) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.rpoplpush(key, destination);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static List<byte[]> lpopList(byte[] key) {
		List<byte[]> list = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			list = jedis.lrange(key, 0, -1);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return list;
	}

	public static byte[] rpop(byte[] key) {
		byte[] bytes = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			bytes = jedis.rpop(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return bytes;
	}

	public static String rpop(String key) {
		String result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.rpop(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	public static List<String> brpop(String key) {
		List<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.brpop(0, key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	public static void hmset(Object key, Map<String, String> hash) {

		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hmset(key.toString(), hash);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void hmset(Object key, Map<String, String> hash, int time) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.hmset(key.toString(), hash);
			jedis.expire(key.toString(), time);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static List<String> hmget(Object key, String... fields) {
		List<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.hmget(key.toString(), fields);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	public static Set<String> hkeys(String key) {
		Set<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.hkeys(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 keys
	 * @描述 查询匹配pattern的key 集合
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 Set
	 * @作者
	 * @时间 2015年7月11日 下午3:19:48
	 */
	public static Set<String> keys(String pattern) {
		Set<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.keys(pattern);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	public static List<byte[]> lrange(byte[] key, int from, int to) {
		List<byte[]> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.lrange(key, from, to);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	public static List<String> lrange(String key, int from, int to) {
		List<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.lrange(key, from, to);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	public static Map<byte[], byte[]> hgetAll(byte[] key) {
		Map<byte[], byte[]> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.hgetAll(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 del
	 * @描述 删除key
	 * @参数 @param key
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:55:25
	 */
	public static void del(byte[] key) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 del
	 * @描述 删除key
	 * @参数 @param key
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:55:07
	 */
	public static void del(String key) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 flushAll
	 * @描述 清空所有数据
	 * @参数
	 * @返回值 void
	 * @作者
	 * @时间 2015年7月11日 下午4:54:47
	 */
	public static void flushAll() {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.flushAll();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 
	 * @名称 ttl
	 * @描述 返回剩余时间
	 * @参数 @param key
	 * @参数 @return -2 键不存在。 -1 键存在,但没有设置过期时间或者生存时间。 >= 0 键的剩余生存时间。
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月7日 下午5:40:04
	 */
	public static long ttl(String key) {
		long time = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			time = jedis.ttl(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return time;
	}

	/**
	 * 
	 * @名称 expire
	 * @描述 设置键的生存时间
	 * @参数 @param key
	 * @参数 @param seconds 时间单位 秒
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月7日 下午5:36:54
	 */
	public static long expire(String key, int seconds) {
		long len = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			len = jedis.expire(key, seconds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return len;
	}

	/**
	 * 
	 * @名称 setnx
	 * @描述 设置不存在的key
	 * @参数 @param key
	 * @参数 @param value
	 * @参数 @return 1:设置成功。0:设置失败。
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月8日 下午3:29:01
	 */
	public static long setnx(String key, String value) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.setnx(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 sismember
	 * @描述 set集合操作:检查给定的元素是否存在于集合
	 * @参数 @param setkey
	 * @参数 @param key
	 * @参数 @return true 存在, false 不存在
	 * @返回值 boolean
	 * @作者
	 * @时间 2015年7月11日 下午4:49:54
	 */
	public static boolean sismember(String setkey, String key) {
		boolean result = false;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.sismember(setkey, key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 sadd
	 * @描述 set集合操作:集合中添加一个元素
	 * @参数 @param setkey
	 * @参数 @param memeber
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月11日 下午5:12:40
	 */
	public static long sadd(String setkey, String memeber) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.sadd(setkey, memeber);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 srem
	 * @描述 set集合操作:集合中删除一个元素
	 * @参数 @param setkey
	 * @参数 @param memeber
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年7月11日 下午5:30:30
	 */
	public static long srem(String setkey, String memeber) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.srem(setkey, memeber);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	/**
	 * @名称 smembers
	 * @描述 set集合操作:获取集合
	 * @参数 @param setKey
	 * @参数 @return
	 * @返回值 Set<String>
	 * @作者
	 * @时间 2016年1月19日 下午2:04:24
	 */
	public static Set<String> smembers(String setKey) {
		Set<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.smembers(setKey);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	/**
	 * 
	 * @名称 info
	 * @描述 查看服务器当前的状态信息和统计信息
	 * @参数 @return
	 * @返回值 String
	 * @作者
	 * @时间 2015年8月3日 下午2:33:55
	 */
	public static String info() {

		String result = "";
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.info();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 zadd
	 * @描述
	 * @参数 @return
	 * @返回值 Integer reply, specifically: 1 if the new element was added 0 if the
	 *      element was already a member of the sorted set and the score was updated
	 * @作者
	 * @时间 2015年8月12日 上午9:40:32
	 */
	public static long zadd(String key, double score, String element) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.zadd(key, score, element);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 zrem
	 * @描述 TODO(根据element删除zset)
	 * @参数 @param key
	 * @参数 @param element
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015-8-27 上午10:48:52
	 */
	public static long zrem(String key, String element) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.zrem(key, element);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 zadd
	 * @描述 TODO(批量添加 zset)
	 * @参数 @param key
	 * @参数 @param scoreMembers
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015-8-27 上午10:55:45
	 */
	public static long zadd(String key, Map<String, Double> scoreMembers) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.zadd(key, scoreMembers);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @名称 zrem
	 * @描述 TODO(批量删除ZSET list elements)
	 * @参数 @param key
	 * @参数 @param elements
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015-8-27 上午10:54:57
	 */
	public static long zrem(String key, List<String> elements) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			for (String element : elements) {
				result = jedis.zrem(key, element);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * 
	 * @throws Exception
	 * @名称 incr
	 * @描述 针对数字值递增一
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2015年8月14日 下午4:46:45
	 */
//	public static long incr(String key) throws Exception {
	public static long incr(String key) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.incr(key);
		} catch (Exception e) {
			e.printStackTrace();
//			throw e;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}

		return result;
	}

	/**
	 * @名称 decr
	 * @描述 针对数字值递减一
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2016年3月3日 下午3:49:47
	 */
	public static long decr(String key) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.decr(key);
		} catch (Exception e) {
			e.printStackTrace();
//			throw e;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 * @Description: 根据参数 count 的值,移除列表中与参数 value 相等的元素。根据参数 count 的值,移除列表中与参数 value
	 *               相等的元素。
	 * @param: count 的值可以是以下几种: count > 0 : 从表头开始向表尾搜索,移除与 value 相等的元素,数量为 count 。
	 *               count < 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。 count =
	 *               0 : 移除表中所有与 value 相等的值。
	 * @return:
	 * @auther:
	 * @date: 2018-12-3 18:58
	 */
	public static long lrem(String key, long count, String value) {
		Jedis jedis = null;
		long lrem = 0;
		try {
			pool = getPool();
			jedis = pool.getResource();
			lrem = jedis.lrem(key, count, value);
		} catch (Exception e) {
			e.printStackTrace();
//			throw e;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return lrem;
	}

	/**
	 *
	 * @名称 zremByScore
	 * @描述 根据分值删除element zremrangebyscore(key, min, max) :删除名称为key的zset中score >=
	 *     min且score <= max的所有元素
	 * @参数 @param key
	 * @参数 @param score
	 * @参数 @return
	 * @返回值 long
	 * @作者
	 * @时间 2018-9-13 下午4:16:10
	 */
	public static long zremByScore(String key, String score) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.zremrangeByScore(key, score, score);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *
	 * @名称 exists
	 * @描述 是否存在键值
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 boolean
	 * @作者
	 * @时间 2018-9-13 下午4:16:10
	 */
	public static boolean exists(String key) {
		boolean result = false;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *
	 * @名称 zrange
	 * @描述 获取zset key中start到end的元素
	 * @参数 @param key
	 * @参数 @param start
	 * @参数 @param end
	 * @参数 @return
	 * @返回值 Set<String>
	 * @作者
	 * @时间 2018-9-13 下午4:16:10
	 */
	public static Set<String> zrange(String key, long start, long end) {
		Set<String> result = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.zrange(key, start, end);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/**
	 *
	 * @名称 zcount
	 * @描述 zset大小
	 * @参数 @param key
	 * @参数 @return
	 * @返回值 boolean
	 * @作者
	 * @时间 2018-9-13 下午4:16:10
	 */
	public static long zcount(String key) {
		long result = 0;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			// zcount(final String key, final String min, final String max) 无限
			result = jedis.zcount(key, "-inf", "+inf");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
		return result;
	}

	/*
	 * public static TbSysUser tokenToUser(String token) {
	 * if(token==null||token.isEmpty()) { return null; } JSONObject jsStr =
	 * JSONObject.parseObject(get(token+"_info")); return
	 * JSONObject.toJavaObject(jsStr, TbSysUser.class); }
	 */

	/***
	 * @名称 tokenToMember
	 * @描述 通过token获取前台用户信息
	 * @参数 [token]
	 * @返回值 com.cngrain.entity.TbMemberOperator
	 * @作者
	 * @时间 2018/9/25 19:00
	 */
	/*
	 * public static TbMemberOperator tokenToMember(String token) {
	 * if(token==null||token.isEmpty()) { return null; } JSONObject jsStr =
	 * JSONObject.parseObject(get(token+"_info")); return
	 * JSONObject.toJavaObject(jsStr, TbMemberOperator.class); }
	 */

	/***
	 * @名称 tokenToType
	 * @描述 通过token获取产后服务系统当前登录用户为前台用户还是后台用户
	 * @参数 [token]
	 * @返回值 String
	 * @作者
	 * @时间 2018/9/25 19:00
	 */
	public static String tokenToType(String token) {
		if (token == null || token.isEmpty()) {
			return null;
		}
		return get(token + "_type");
	}

	public static JSONObject getJSONObject(String key) {
		Object o = get(key);
		return JSON.parseObject(o == null ? "{}" : o.toString());
	}

	public static void setJSONObject(String key, JSONObject json) {
		if (json == null) {
			return;
		}
		set(key, json.toJSONString(), 15);
	}

	/**
	 * 尝试获取分布式锁
	 * 
	 * @param jedis      Redis客户端
	 * @param lockKey    锁
	 * @param requestId  请求标识
	 * @param expireTime 超期时间
	 * @return 是否获取成功
	 */
	public static boolean tryGetDistributedLock(String lockKey) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			String result = jedis.set(lockKey, "L", "NX", "PX", 5000);
			if ("OK".equals(result)) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	/**
	 * 释放分布式锁
	 * 
	 * @param jedis     Redis客户端
	 * @param lockKey   锁
	 * @param requestId 请求标识
	 * @return 是否释放成功
	 */
	public static boolean releaseDistributedLock(String lockKey) {
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
			Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList("L"));
			if (result == null) {
				return false;
			}
			if (result.toString().equals("1")) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			// 返还到连接池
			if (jedis != null) {
				jedis.close();
			}
		}

	}

	public static void main(String[] args) throws InterruptedException {
		RedisUtil.init();
//		hset("test", "loopxu1", "1");
//		hset("test", "loopxu2", "2");
//		hset("test", "loopxu3", "3");
		
		System.out.println(JSONObject.toJSONString(hgetAll("FD_DP_KEY")));

	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java 使用 Redis 需要引入 RedisJava 客户端库,最常用的是 Jedis 和 Lettuce。 Jedis 是 RedisJava 客户端之一,由于其简单易用,广受 Java 开发者的喜爱。你可以在 Maven 引入 Jedis: ```xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.5.2</version> </dependency> ``` Lettuce 则是另一个广泛使用的 RedisJava 客户端,相比 Jedis,Lettuce 支持异步操作,性能更高。你可以在 Maven 引入 Lettuce: ```xml <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> <version>6.0.2.RELEASE</version> </dependency> ``` 无论选择哪一个客户端,使用 Redis 都需要引入相应的依赖,并按照官方文档进行配置和使用。 ### 回答2: Java使用Redis主要依赖于Jedis库。Jedis是一个Java语言编写的高性能、面向对象的Redis客户端库,它提供了一系列的API,用于与Redis服务器进行交互。 Jedis库提供了对Redis的基本操作的支持,包括连接到Redis服务器、执行命令、获取和设置值、批量处理等。通过Jedis库,我们可以通过代码与Redis进行交互,如向Redis存储数据、获取数据、删除数据等操作。 Jedis库的优点之一是其易于使用。开发人员可以很轻松地通过简单的API来连接到Redis服务器,并执行各种操作,无需过多的配置和学习成本。此外,Jedis还支持事务和管道机制,可以提高Redis操作效率。 此外,Java也可以使用Lettuce库来与Redis进行交互。Lettuce是一个基于Netty的开源Redis客户端,具有异步、反应式的特性。相比于Jedis,Lettuce在高并发场景下具有更好的性能表现。Lettuce提供了丰富的功能和API,可以充分利用Redis的特性,包括发布订阅、事务、批量处理等。 总结而言,在Java使用Redis,我们主要依赖于Jedis和Lettuce这两个库。Jedis是一个简单易用的Redis客户端库,适用于大多数场景。而Lettuce是一个高性能的Redis客户端库,适用于高并发场景和对性能要求较高的应用。开发人员可以根据自己的需求选择适合的库来与Redis进行交互。 ### 回答3: Java使用Redis可以通过引入相关的依赖库来实现。在JavaRedis常用Java客户端有Jedis和Lettuce两种选择。 1. Jedis:Jedis是一个简单且易于使用的Java Redis客户端库,它是基于Java操作Redis的API。它提供了一组易于理解和使用的接口来连接和操作Redis数据库。使用Jedis进行Redis操作非常方便,可以直接在Java代码使用Jedis提供的方法来实现Redis的交互。 2. Lettuce:Lettuce是一个高级的Redis客户端库,它提供了更多的功能和性能优化。与Jedis相比,Lettuce使用了异步、非阻塞的IO模型,可以更好地支持并发访问和高性能。Lettuce还提供了更多的Redis特性支持,例如Redis Sentinel、Redis Cluster和Redis Pub/Sub等。 无论是使用Jedis还是Lettuce,首先需要在项目添加对应的依赖库。具体的引入方式可以通过Maven或Gradle等构建工具来实现。接着,可以在Java代码创建Redis连接对象,连接到Redis数据库,并使用提供的方法来进行数据的存储、读取、删除等操作。 总的来说,Java使用Redis可以通过引入Jedis或Lettuce等相关的库,使用其提供的方法来连接和操作Redis数据库。这些库可以为Java程序员提供方便、高效和灵活的方式来使用Redis,并实现各种Redis的功能和特性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值