欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解java操作Redis數(shù)據(jù)庫(kù)的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoolUtil)

 更新時(shí)間:2021年08月23日 09:53:18   作者:miniduhua  
這篇文章主要介紹了java操作Redis數(shù)據(jù)庫(kù)的redis工具,包括RedisUtil,jedis工具JedisUtil,JedisPoolUtil工具,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

該工具包含是封裝了jedis,包含redis.properties和jedisPool,序列化使用的是protostuff,map類型操作使用的是fastjson

自己抽空寫(xiě)的,基本只要理解什么是get,什么是set就可以使用redis數(shù)據(jù)庫(kù)了

下載地址:點(diǎn)擊打開(kāi)鏈接

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();
			}
		}
	}
 
	/**
	 * 初始化Jedis連接池
	 * 
	 * @throws IOException
	 */
	private static void init() throws IOException {
		URL resource = JedisPoolUtil.class.getClassLoader().getResource(PROPERTIES_PATH);
		if (resource == null) {
			throw new FileNotFoundException("沒(méi)有找到指定redis的配置文件:" + PROPERTIES_PATH);
		}
		//加載配置文件
		InputStream input = new FileInputStream(resource.getFile());
		Properties p = new Properties();
		p.load(input);
		//開(kāi)始配置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"));
		//判斷使用默認(rèn)的配置方式還是采用自定義配置方式,
		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端口號(hào)
redis.port=6379
#redis的密碼
#redis.auth=

#是否使用JedisPool默認(rèn)的配置,確定true,默認(rèn)true
#defaultSetting=false;
#jedisPool的timeout時(shí)間,默認(rèn)2000
#connectionTimeOut=
#連接耗盡時(shí)是否阻塞, false報(bào)異常,ture阻塞直到超時(shí), 默認(rèn)true
#redis.blockWhenExhausted=true
#設(shè)置的逐出策略類名, 默認(rèn)DefaultEvictionPolicy(當(dāng)連接超過(guò)最大空閑時(shí)間,或連接數(shù)超過(guò)最大空閑連接數(shù))
#redis.evictionPolicyClassName=org.apache.commons.pool2.impl.DefaultEvictionPolicy
#是否啟用pool的jmx管理功能, 默認(rèn)true
#redis.jmxEnabled=true
#是否啟用后進(jìn)先出, 默認(rèn)true
#redis.lifo=true
#最大空閑連接數(shù), 默認(rèn)8個(gè)
#redis.maxIdle=8	
#最大連接數(shù), 默認(rèn)8個(gè)
#redis.maxTotal=8
#獲取連接時(shí)的最大等待毫秒數(shù)(如果設(shè)置為阻塞時(shí)BlockWhenExhausted),如果超時(shí)就拋異常, 小于零:阻塞不確定的時(shí)間,  默認(rèn)-1
#redis.maxWaitMillis=-1
#逐出連接的最小空閑時(shí)間 默認(rèn)1800000毫秒(30分鐘)
#redis.minEvictableIdleTimeMillis=1800000
#最小空閑連接數(shù), 默認(rèn)0
#redis.minIdle=0
#每次逐出檢查時(shí) 逐出的最大數(shù)目 如果為負(fù)數(shù)就是 : 1/abs(n), 默認(rèn)3
#redis.numTestsPerEvictionRun=3
#對(duì)象空閑多久后逐出, 當(dāng)空閑時(shí)間>該值 且 空閑連接>最大空閑數(shù) 時(shí)直接逐出,不再根據(jù)MinEvictableIdleTimeMillis判斷  (默認(rèn)逐出策略)   
#redis.softMinEvictableIdleTimeMillis=1800000
#在獲取連接的時(shí)候檢查有效性, 默認(rèn)false
#redis.testOnBorrow=false
#在空閑時(shí)檢查有效性, 默認(rèn)false
#redis.testWhileIdle=false
#逐出掃描的時(shí)間間隔(毫秒) 如果為負(fù)數(shù),則不運(yùn)行逐出線程, 默認(rèn)-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;
 
/**
 * jedis的幫助工具 本類map依賴fastjson,對(duì)象與集合使用protostuff序列化框架 jedis版本2.9.0
 * protostuff相關(guān)版本1.0.8 fastjson版本1.2.32 使用maven自動(dòng)添加依賴
 * 
 * @author Mirren
 *
 */
public class JedisUtil {
	private static final int DEFAULT_SETEX_TIMEOUT = 60 * 60;// setex的默認(rèn)時(shí)間
 
	/**
	 * 添加一個(gè)字符串值,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
 
	}
 
	/**
	 * 緩存一個(gè)字符串值,成功返回1,失敗返回0,默認(rèn)緩存時(shí)間為1小時(shí),以本類的常量DEFAULT_SETEX_TIMEOUT為準(zhǔn)
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)字符串值,成功返回1,失敗返回0,緩存時(shí)間以timeout為準(zhǔn),單位秒
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)指定類型的對(duì)象,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)指定類型的對(duì)象,成功返回1,失敗返回0,默認(rèn)緩存時(shí)間為1小時(shí),以本類的常量DEFAULT_SETEX_TIMEOUT為準(zhǔn)
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)指定類型的對(duì)象,成功返回1,失敗返回0,緩存時(shí)間以timeout為準(zhǔn),單位秒
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 將一個(gè)數(shù)值+1,成功返回+后的結(jié)果,失敗返回null
	 * 
	 * @param key
	 * @return
	 * @throws JedisDataException
	 */
	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);
		}
	}
 
	/**
	 * 將一個(gè)數(shù)值-1,成功返回-后的結(jié)果,失敗返回null
	 * 
	 * @param key
	 * @return
	 * @throws JedisDataException
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)字符串值到list中,,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)字符串值到list中,全部list的key默認(rèn)緩存時(shí)間為1小時(shí),成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)字符串值到list中,全部list的key緩存時(shí)間為timeOut,單位為秒,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)<T>類型對(duì)象值到list中,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 緩存一個(gè)<T>類型對(duì)象值到list中,全部list的key默認(rèn)緩存時(shí)間為1小時(shí),成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 緩存一個(gè)<T>類型對(duì)象值到list中,全部list的key緩存時(shí)間為timeOut,單位秒,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 添加一個(gè)List集合成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 * @throws IOException
	 * @throws RuntimeException
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)List<T>集合,成功返回1,失敗返回0,默認(rèn)緩存時(shí)間為1小時(shí),以本類的常量DEFAULT_SETEX_TIMEOUT為準(zhǔn)
	 * 
	 * @param key
	 * @param value
	 * @return
	 * @throws IOException
	 * @throws RuntimeException
	 */
 
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)List<T>集合,成功返回1,失敗返回0,緩存時(shí)間以timeout為準(zhǔn),單位秒
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 * @throws IOException
	 * @throws RuntimeException
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)字符串到set,如果key存在就在就最追加,如果key不存在就創(chuàng)建,成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)字符串set,如果key存在就在就最追加,整個(gè)set的key默認(rèn)一小時(shí)后過(guò)期,如果key存在就在可以種繼續(xù)添加,如果key不存在就創(chuàng)建,成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)字符串set,如果key存在就在就最追加,整個(gè)set的key有效時(shí)間為timeOut時(shí)間,單位秒,如果key存在就在可以種繼續(xù)添加,如果key不存在就創(chuàng)建,,成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 添加一個(gè)<T>類型到set集合,如果key存在就在就最追加,成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 緩存一個(gè)<T>類型到set集合,如果key存在就在就最追加,整個(gè)set的key默認(rèn)有效時(shí)間為1小時(shí),成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 緩存一個(gè)<T>類型到set集合,如果key存在就在就最追加,整個(gè)set的key有效時(shí)間為timeOut,單位秒,成功返回1,失敗或者沒(méi)有受影響返回0
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	@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);
		}
	}
 
	/**
	 * 添加一個(gè)Map<K, V>集合,成功返回1,失敗返回0
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)Map<K, V>集合,成功返回1,失敗返回0,默認(rèn)緩存時(shí)間為1小時(shí),以本類的常量DEFAULT_SETEX_TIMEOUT為準(zhǔn)
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 緩存一個(gè)Map<K, V>集合,成功返回1,失敗返回0,緩存時(shí)間以timeout為準(zhǔn),單位秒
	 * 
	 * @param key
	 * @param value
	 * @param timeout
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲取一個(gè)字符串值
	 * 
	 * @param key
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)指定類型的對(duì)象
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)字符串集合,區(qū)間以偏移量 START 和 END 指定。 其中 0 表示列表的第一個(gè)元素, 1
	 * 表示列表的第二個(gè)元素,以此類推。 你也可以使用負(fù)數(shù)下標(biāo),以 -1 表示列表的最后一個(gè)元素, -2 表示列表的倒數(shù)第二個(gè)元素,以此類推。
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)<T>類型的對(duì)象集合,區(qū)間以偏移量 START 和 END 指定。 其中 0 表示列表的第一個(gè)元素, 1 表示列表的第二個(gè)元素,以此類推。
	 * 你也可以使用負(fù)數(shù)下標(biāo),以 -1 表示列表的最后一個(gè)元素, -2 表示列表的倒數(shù)第二個(gè)元素,以此類推。
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得list中存了多少個(gè)值
	 * 
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)List<T>的集合,
	 * 
	 * @param key
	 *            鍵
	 * @param clazz
	 *            返回集合的類型
	 * @return
	 * @throws IOException
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)字符串set集合
	 * 
	 * @param key
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)字符串set集合
	 * 
	 * @param key
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得集合中存在多少個(gè)值
	 * 
	 * @param key
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 獲得一個(gè)Map<v,k>的集合
	 * 
	 * @param key
	 * @param v
	 * @param k
	 * @return
	 */
	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);
		}
	}
 
	/**
	 * 刪除一個(gè)值
	 * 
	 * @param key
	 */
	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);
		}
	}
 
	// --------------------------公用方法區(qū)------------------------------------
	/**
	 * 檢查值是否為null,如果為null返回true,不為null返回false
	 * 
	 * @param obj
	 * @return
	 */
	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;
	}
 
	/**
	 * 序列化一個(gè)對(duì)象
	 * 
	 * @param value
	 * @return
	 */
	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;
	}
 
	/**
	 * 反序列化一個(gè)對(duì)象
	 * 
	 * @param t
	 * @return
	 */
	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;
	}
 
	/**
	 * 序列化List集合
	 * 
	 * @param list
	 * @return
	 * @throws IOException
	 */
	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;
	}
 
	/**
	 * 反序列化List集合
	 * 
	 * @param data
	 * @param clazz
	 * @return
	 * @throws IOException
	 */
	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;
	}
 
}

到此這篇關(guān)于java操作Redis數(shù)據(jù)庫(kù)的redis工具的文章就介紹到這了,更多相關(guān)java操作Redis數(shù)據(jù)庫(kù)的redis工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論