广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解java操作Redis数据库的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)
  • 944
分享到

详解java操作Redis数据库的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)

2024-04-02 19:04:59 944人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

该工具包含是封装了jedis,包含Redis.properties和jedisPool,序列化使用的是protostuff,map类型操作使用的是fastJSON 自己抽空写的,基本

工具包含是封装了jedis,包含Redis.properties和jedisPool,序列化使用的是protostuff,map类型操作使用的是fastJSON

自己抽空写的,基本只要理解什么是get,什么是set就可以使用redis数据库

下载地址:点击打开链接

JedisPoolUtil的源码:


package com.bsy.common;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
 
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
public class JedisPoolUtil {
 
	private static final String PROPERTIES_PATH = "redis.properties";
	private static JedisPool jedisPool;
 
	
	static {
		if (jedisPool == null) {
			try {
				init();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	
	private static void init() throws IOException {
		URL resource = JedisPoolUtil.class.getClassLoader().getResource(PROPERTIES_PATH);
		if (resource == null) {
			throw new FileNotFoundException("没有找到指定redis的配置文件:" + PROPERTIES_PATH);
		}
		//加载配置文件
		InputStream input = new FileInputStream(resource.getFile());
		Properties p = new Properties();
		p.load(input);
		//开始配置JedisPool
		String host = p.getProperty("redis.host") == null ? "localhost" : p.getProperty("redis.host");
		int port = p.getProperty("redis.port") == null ? 6379 : Integer.parseInt(p.getProperty("redis.port"));
		String auth = p.getProperty("redis.auth");
		int poolTimeOut = p.getProperty("connectionTimeOut") == null ? 2000
				: Integer.parseInt(p.getProperty("connectionTimeOut"));
		//判断使用默认的配置方式还是采用自定义配置方式,
		boolean isSetDefault = p.getProperty("defaultSetting") == null ? true
				: Boolean.parseBoolean(p.getProperty("defaultSetting"));
		if (isSetDefault) {
			jedisPool = new JedisPool(new GenericObjectPoolConfig(), host, port, poolTimeOut, auth);
		} else {
			JedisPoolConfig config = new JedisPoolConfig();
			String blockWhenExhausted = p.getProperty("redis.blockWhenExhausted");
			if (blockWhenExhausted != null) {
				config.setBlockWhenExhausted(Boolean.parseBoolean(blockWhenExhausted));
			}
			String evictionPolicyClassName = p.getProperty("redis.evictionPolicyClassName");
			if (evictionPolicyClassName != null) {
				config.setEvictionPolicyClassName(evictionPolicyClassName);
			}
			String jmxEnabled = p.getProperty("redis.jmxEnabled");
			if (jmxEnabled != null) {
				config.setJmxEnabled(Boolean.parseBoolean(jmxEnabled));
			}
			String lifo = p.getProperty("redis.lifo");
			if (lifo != null) {
				config.setLifo(Boolean.parseBoolean(lifo));
			}
			String maxIdle = p.getProperty("redis.maxIdle");
			if (maxIdle != null) {
				config.setMaxIdle(Integer.parseInt(maxIdle));
			}
			String maxTotal = p.getProperty("redis.maxTotal");
			if (maxTotal != null) {
				config.setMaxTotal(Integer.parseInt(maxTotal));
			}
			String maxWaitMillis = p.getProperty("redis.maxWaitMillis");
			if (maxWaitMillis != null) {
				config.setMaxWaitMillis(Long.parseLong(maxWaitMillis));
			}
			String minEvictableIdleTimeMillis = p.getProperty("redis.minEvictableIdleTimeMillis");
			if (minEvictableIdleTimeMillis != null) {
				config.setMinEvictableIdleTimeMillis(Long.parseLong(minEvictableIdleTimeMillis));
			}
			String minIdle = p.getProperty("redis.minIdle");
			if (minIdle != null) {
				config.setMinIdle(Integer.parseInt(minIdle));
			}
			String numTestsPerEvictionRun = p.getProperty("redis.numTestsPerEvictionRun");
			if (numTestsPerEvictionRun != null) {
				config.setNumTestsPerEvictionRun(Integer.parseInt(numTestsPerEvictionRun));
			}
			String softMinEvictableIdleTimeMillis = p.getProperty("redis.softMinEvictableIdleTimeMillis");
			if (softMinEvictableIdleTimeMillis != null) {
				config.setSoftMinEvictableIdleTimeMillis(Long.parseLong(softMinEvictableIdleTimeMillis));
			}
			String testOnBorrow = p.getProperty("redis.testOnBorrow");
			if (testOnBorrow != null) {
				config.setTestOnBorrow(Boolean.parseBoolean(testOnBorrow));
			}
			String testWhileIdle = p.getProperty("redis.testWhileIdle");
			if (testWhileIdle != null) {
				config.setTestWhileIdle(Boolean.parseBoolean(testWhileIdle));
			}
			String timeBetweenEvictionRunsMillis = p.getProperty("redus.timeBetweenEvictionRunsMillis");
			if (timeBetweenEvictionRunsMillis != null) {
				config.setTimeBetweenEvictionRunsMillis(Long.parseLong(timeBetweenEvictionRunsMillis));
			}
			jedisPool = new JedisPool(config, host, port, poolTimeOut, auth);
		}
 
	}
 
	public static Jedis getJedis() {
		return jedisPool.getResource();
	}
 
	public static void closeJedis(Jedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}
 
}

redis.properties源码:


#redis地址
redis.host=localhost
#redis端口号
redis.port=6379
#redis的密码
#redis.auth=

#是否使用JedisPool默认的配置,确定true,默认true
#defaultSetting=false;
#jedisPool的timeout时间,默认2000
#connectionTimeOut=
#连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
#redis.blockWhenExhausted=true
#设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
#redis.evictionPolicyClassName=org.apache.commons.pool2.impl.DefaultEvictionPolicy
#是否启用pool的jmx管理功能, 默认true
#redis.jmxEnabled=true
#是否启用后进先出, 默认true
#redis.lifo=true
#最大空闲连接数, 默认8个
#redis.maxIdle=8	
#最大连接数, 默认8个
#redis.maxTotal=8
#获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
#redis.maxWaitMillis=-1
#逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
#redis.minEvictableIdleTimeMillis=1800000
#最小空闲连接数, 默认0
#redis.minIdle=0
#每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
#redis.numTestsPerEvictionRun=3
#对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断  (默认逐出策略)   
#redis.softMinEvictableIdleTimeMillis=1800000
#在获取连接的时候检查有效性, 默认false
#redis.testOnBorrow=false
#在空闲时检查有效性, 默认false
#redis.testWhileIdle=false
#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
#redus.timeBetweenEvictionRunsMillis=-1

JedisUtil源码:


package com.bsy.common;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import com.alibaba.fastjson.JSON;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;
 

public class JedisUtil {
	private static final int DEFAULT_SETEX_TIMEOUT = 60 * 60;// setex的默认时间
 
	
	public static int set(String key, String value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			if (jedis.set(key, value).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
 
	}
 
	
	public static int setEx(String key, String value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			if (jedis.setex(key, DEFAULT_SETEX_TIMEOUT, value).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setEx(String key, String value, int timeout) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			if (jedis.setex(key, timeout, value).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> int set(String key, T value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeri(value);
			if (jedis.set(key.getBytes(), data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> int setEx(String key, T value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeri(value);
			if (jedis.setex(key.getBytes(), DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> int setEx(String key, T value, int timeout) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeri(value);
			if (jedis.setex(key.getBytes(), timeout, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static Long incr(String key) throws JedisDataException {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			return jedis.incr(key);
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static Long decr(String key) throws JedisDataException {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			return jedis.decr(key);
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setList(String key, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.rpush(key, value);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setExList(String key, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.rpush(key, value);
			jedis.expire(key, DEFAULT_SETEX_TIMEOUT);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
 
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setExList(String key, int timeOut, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.rpush(key, value);
			jedis.expire(key, timeOut);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
 
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setList(String key, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.rpush(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setExList(String key, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.rpush(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			jedis.expire(key, DEFAULT_SETEX_TIMEOUT);
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setExList(String key, int timeOut, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.rpush(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			jedis.expire(key, timeOut);
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> int setList(String key, List<T> value) throws RuntimeException, IOException {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeriList(value);
			if (jedis.set(key.getBytes(), data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
 
	public static <T> int setExList(String key, List<T> value) throws RuntimeException, IOException {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeriList(value);
			if (jedis.setex(key.getBytes(), DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> int setExList(String key, List<T> value, int timeout) throws RuntimeException, IOException {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = enSeriList(value);
			if (jedis.setex(key.getBytes(), timeout, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setSet(String key, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.sadd(key, value);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setExSet(String key, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.sadd(key, value);
			jedis.expire(key, DEFAULT_SETEX_TIMEOUT);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static int setExSet(String key, int timeOut, String... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Long result = jedis.sadd(key, value);
			jedis.expire(key, timeOut);
			if (result != null && result != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setSet(String key, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.sadd(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setExSet(String key, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.sadd(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			jedis.expire(key, DEFAULT_SETEX_TIMEOUT);
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	@SafeVarargs
	public static <T> int setExSet(String key, int timeOut, T... value) {
		if (isValueNull(key, value)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			int res = 0;
			for (T t : value) {
				byte[] data = enSeri(t);
				Long result = jedis.sadd(key.getBytes(), data);
				if (result != null && result != 0) {
					res++;
				}
			}
			jedis.expire(key, timeOut);
			if (res != 0) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <K, V> int setMap(String key, Map<K, V> value) {
		if (value == null || key == null || "".equals(key)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			String data = JSON.toJSONString(value);
			if (jedis.set(key, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <K, V> int setExMap(String key, Map<K, V> value) {
		if (value == null || key == null || "".equals(key)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			String data = JSON.toJSONString(value);
			if (jedis.setex(key, DEFAULT_SETEX_TIMEOUT, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <K, V> int setExMap(String key, Map<K, V> value, int timeout) {
		if (value == null || key == null || "".equals(key)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			String data = JSON.toJSONString(value);
			if (jedis.setex(key, timeout, data).equalsIgnoreCase("ok")) {
				return 1;
			} else {
				return 0;
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static String get(String key) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			return jedis.get(key);
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> T get(String key, Class<T> clazz) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
 
			byte[] data = jedis.get(key.getBytes());
			T result = deSeri(data, clazz);
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static List<String> getList(String key, long start, long end) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			List<String> result = jedis.lrange(key, start, end);
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> List<T> getList(String key, long start, long end, Class<T> clazz) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			List<byte[]> lrange = jedis.lrange(key.getBytes(), start, end);
			List<T> result = null;
			if (lrange != null) {
				for (byte[] data : lrange) {
					if (result == null) {
						result = new ArrayList<>();
					}
					result.add(deSeri(data, clazz));
				}
			}
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static long getListCount(String key) {
		if (isValueNull(key)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			return jedis.llen(key);
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> List<T> getList(String key, Class<T> clazz) throws IOException {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			byte[] data = jedis.get(key.getBytes());
			List<T> result = deSeriList(data, clazz);
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static Set<String> getSet(String key) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Set<String> result = jedis.smembers(key);
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <T> Set<T> getSet(String key, Class<T> clazz) {
		if (isValueNull(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			Set<byte[]> smembers = jedis.smembers(key.getBytes());
			Set<T> result = null;
			if (smembers != null) {
				for (byte[] data : smembers) {
					if (result == null) {
						result = new HashSet<>();
					}
					result.add(deSeri(data, clazz));
				}
			}
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static long getSetCount(String key) {
		if (isValueNull(key)) {
			return 0;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			return jedis.scard(key);
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static <K, V> Map<K, V> getMap(String key, Class<K> k, Class<V> v) {
		if (key == null || "".equals(key)) {
			return null;
		}
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			String data = jedis.get(key);
			@SuppressWarnings("unchecked")
			Map<K, V> result = (Map<K, V>) JSON.parseObject(data);
			return result;
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	
	public static void del(String... key) {
		Jedis jedis = null;
		try {
			jedis = JedisPoolUtil.getJedis();
			for (int i = 0; i < key.length; i++) {
				jedis.del(key);
			}
		} finally {
			JedisPoolUtil.closeJedis(jedis);
		}
	}
 
	// --------------------------公用方法区------------------------------------
	
	private static boolean isValueNull(Object... obj) {
		for (int i = 0; i < obj.length; i++) {
			if (obj[i] == null || "".equals(obj[i])) {
				return true;
			}
		}
		return false;
	}
 
	
	private static <T> byte[] enSeri(T value) {
		@SuppressWarnings("unchecked")
		RuntimeSchema<T> schema = (RuntimeSchema<T>) RuntimeSchema.createFrom(value.getClass());
		byte[] data = ProtostuffIOUtil.toByteArray(value, schema,
				LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
		return data;
	}
 
	
	private static <T> T deSeri(byte[] data, Class<T> clazz) {
		if (data == null || data.length == 0) {
			return null;
		}
		RuntimeSchema<T> schema = RuntimeSchema.createFrom(clazz);
		T result = schema.newMessage();
		ProtostuffIOUtil.mergeFrom(data, result, schema);
		return result;
	}
 
	
	private static <T> byte[] enSeriList(List<T> list) throws RuntimeException, IOException {
		if (list == null || list.size() == 0) {
			throw new RuntimeException("集合不能为空!");
		}
		@SuppressWarnings("unchecked")
		RuntimeSchema<T> schema = (RuntimeSchema<T>) RuntimeSchema.getSchema(list.get(0).getClass());
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ProtostuffIOUtil.writeListTo(out, list, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
		byte[] byteArray = out.toByteArray();
		return byteArray;
	}
 
	
	private static <T> List<T> deSeriList(byte[] data, Class<T> clazz) throws IOException {
		if (data == null || data.length == 0) {
			return null;
		}
		RuntimeSchema<T> schema = RuntimeSchema.createFrom(clazz);
		List<T> result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(data), schema);
		return result;
	}
 
}

到此这篇关于java操作Redis数据库的redis工具的文章就介绍到这了,更多相关java操作Redis数据库的redis工具内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解java操作Redis数据库的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)

本文链接: https://www.lsjlt.com/news/133281.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作