1,redis配置文件(redis.properties)
#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=false
#当调用return Object方法时,是否进行有效性检查
redis.pool.testOnReturn=true
#IP
redis.ip=127.0.0.1
#Port
redis.port=6379
2,读取配置文件,创建jedis实例,JedisPool连接池
import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisPoolManager {
private static JedisPool pool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
if (bundle == null)
throw new IllegalArgumentException("[redis.properties] is not found");
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
}
public static Jedis createInstance() {
try{
Jedis jedis = pool.getResource();
return jedis;
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
public static void returnResource(Jedis jedis) {
pool.returnResource(jedis);
}
}
3,setValue,getValue 方法
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class CacheManager {
private Jedis jedis = RedisPoolManager.createInstance();
public void set(Map<String, String> entries) {
for (Map.Entry<String, String> entry : entries.entrySet()) {
jedis.set(entry.getKey(), entry.getValue());
}
}
public void set(String key, String value) {
jedis.set(key, value);
}
public void set(byte[] key, byte[] value) {
jedis.set(key, value);
}
public void setex(byte[] key, int seconds ,byte[] value) {
jedis.setex(key, seconds, value);
}
/**
* @function: 高并发set方法
* @param key
* @param value void
* @exception
*/
public void setnx(byte[] key, byte[] value){
jedis.setnx(key, value);
}
public boolean exists(byte[] key){
return jedis.exists(key);
}
public byte[] getValue(byte[] key) {
return jedis.get(key);
}
public Long deleteValue(byte[] key) {
return jedis.del(key);
}
public void setKeyLive(String key, int live, String value) {
jedis.setex(key, live, value);
}
public void append(String key, String value) {
jedis.append(key, value);
}
public String getValue(String key) {
return jedis.get(key);
}
public List<String> getValues(String... keys) {
return jedis.mget(keys);
}
public Long deleteValue(String key) {
return jedis.del(key);
}
public Long deleteValues(String... keys) {
return jedis.del(keys);
}
public void returnSource() {
RedisPoolManager.returnResource(jedis);
}
public long calculateSize() {
return jedis.dbSize();
}
}
4, 应用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RedisUtil
{
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
/**
* @function: 设置缓存key 和 value值
* @param key
* @param value
* @throws Exception void
* @exception
*/
public static void setValue(String key ,Object value) throws Exception{
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key) && null != value)
{
cacheManager = new CacheManager();
cacheManager.set(key.getBytes(), SerializeUtil.serialize(value));
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.setValue(String key ,Object value)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
}
/**
* @function: 高并发,设置缓存key 和 value值
* @param key
* @param value
* @throws Exception void
* @exception
*/
public static void setnxValue(String key ,Object value) throws Exception{
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key) && null != value)
{
cacheManager = new CacheManager();
cacheManager.setnx(key.getBytes(), SerializeUtil.serialize(value));
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.setnxValue(String key ,Object value)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
}
/**
* @function: 设置缓存key 和 value值 已经缓存存活时间(秒)
* @param key
* @param value
* @param live (秒)
* @throws Exception void
* @exception
*/
public static void setValue(String key ,Object value , int live) throws Exception
{
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key) && null != value)
{
cacheManager = new CacheManager();
cacheManager.setex(key.getBytes(), live, SerializeUtil.serialize(value));
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.setValue(String key ,Object value)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
}
/**
*
* @function: 通过 key 获取缓存中的Object 对象
* @param key
* @return
* @throws Exception boolean
* @exception
*/
public static Object getValue(String key) throws Exception{
Object obj = null;
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key))
{
cacheManager = new CacheManager();
byte[] value = cacheManager.getValue(key.getBytes());
if(null != value)
{
obj = SerializeUtil.unserialize(value);
}
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.getValue(String key)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
return obj;
}
/**
* @function: 根据 key 删除缓存中 value值
* @param key
* @return
* @throws Exception boolean
* @exception
*/
public static boolean deleteValue(String key) throws Exception{
boolean falg = false;
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key))
{
cacheManager = new CacheManager();
cacheManager.deleteValue(key.getBytes());
falg = true;
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.deleteValue(String key)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
return falg;
}
/**
* @function: 判断缓存中,key对于的数据是否存在
* @param key
* @return
* @throws Exception boolean
* @exception
*/
public static boolean exists(String key) throws Exception
{
boolean falg = false;
CacheManager cacheManager = null;
try
{
if(null != key && ! "".equals(key))
{
cacheManager = new CacheManager();
falg = cacheManager.exists(key.getBytes());
}
}
catch (Exception e)
{
log.error("==> 调用RedisUtil.exists(String key)方法异常:",e);
}
finally
{
if(null != cacheManager)
{
cacheManager.returnSource();
}
}
return falg;
}
}
对象序列化
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SerializeUtil {
private static final Logger logger = LoggerFactory.getLogger(SerializeUtil.class);
public static byte[] serialize(Object object) throws Exception {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
logger.info("缓存序列化异常" + e.toString());
throw e;
}
}
public static Object unserialize( byte[] bytes) throws Exception {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
logger.info("缓存序列化异常" + e.toString());
throw e;
}
}
}