配置文件:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="30" />
<property name="maxIdle" value="10" />
<property name="numTestsPerEvictionRun" value="1024" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<property name="maxWaitMillis" value="1500" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="blockWhenExhausted" value="false" />
bean>
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="127.0.0.1">constructor-arg>
<constructor-arg name="port" value="6379">constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
bean>
封装redis操作类JedisClientSingle :
public class JedisClientSingle {
@Autowired
private JedisPool jedisPool;
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}
}
测试:
@Autowired
private JedisClientSingle jedisClient;
//查询数据放入list集合,TaskManage实体类
List list = taskManageDao.findList(taskManage);
//向缓存中添加内容
try {
//把list转换成字符串
//向缓存中存入数据
jedisClient.hset("qqqq:", 11 + "", JSON.toJSON(list).toString());
} catch (Exception e) {
e.printStackTrace();
}
//从缓存中取内容
try {
//获取数据
String result = jedisClient.hget("qqqq:", 11 + "");
if (!StringUtils.isBlank(result)) {
//把字符串转换成list
List resultList = JSON.parseArray(result, TaskManage.class);
} catch (Exception e) {
e.printStackTrac```
();
}
}
上面的例子需要转化list,比较繁琐,那能不能直接存储list集合呢? 这就要需要序列化List集合了。
序列化公共类:(本例是Jdk自带jar序列化,存在效率低,后续引入第三方jar)
package com.cci.community.service.untils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 序列化工具类
* @author cym
* 2017/5/24
*/
public class SerializeUtil {
/**
* 单个序列化
* @param object
* @return
*/
public static byte[] serialize(Object object) {
if (object == null) {
return null;
}
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(oos);
close(baos);
}
return bytes;
}
/**
* 单个反序列化
*
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
if (bytes == null) {
return null;
}
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(bais);
close(ois);
}
return null;
}
/**
* 序列化 list 集合
*
* @param list
* @return
*/
public static byte[] serializeList(List> list) {
if (list == null || list.size() == 0) {
return null;
}
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
for (Object obj : list) {
oos.writeObject(obj);
}
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(oos);
close(baos);
}
return bytes;
}
/**
* 反序列化 list 集合
*
* @param lb
* @return
*/
public static List> unserializeList(byte[] bytes) {
if (bytes == null) {
return null;
}
List
redis封装类JedisClientSingle :
/**
* 设置List集合
* @param key
* @param list
*/
public void setList(String key ,List> list){
Jedis jedis = jedisPool.getResource();
try {
if(list == null || list.size() == 0){
jedis.set(key.getBytes(), "".getBytes());
}else{//如果list为空,则设置一个空
jedis.set(key.getBytes(), SerializeUtil.serializeList(list));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
/**
* 获取List集合
* @param key
* @return
*/
public List> getList(String key){
Jedis jedis = jedisPool.getResource();
if(jedis == null || !jedis.exists(key)){
return null;
}
byte[] data = jedis.get(key.getBytes());
jedis.close();
return SerializeUtil.unserializeList(data);
}
测试:
@Autowired
private JedisClientSingle jedisClient;
//存储缓存
jedisClient.setList("getlist", list);
//取缓存
ist list111 = (List) jedisClient.getList("getlist");