Redis存储List<T>集合
2017-05-24 15:29
441 查看
spring与redis整合
配置文件:<!-- 连接池配置 --> <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" /> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取连接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="true" /> <!-- 在空闲时检查有效性, 默认false --> <property name= 4000 "testWhileIdle" value="true" /> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- jedis客户端单机版 --> <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; } }
存储List集合
测试:@Autowired private JedisClientSingle jedisClient; //查询数据放入list集合,TaskManage实体类 List<TaskManage> 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<TaskManage> resultList = JSON.parseArray(result, TaskManage.class); } catch (Exception e) { e.printStackTrac``` (); } }
直接存储List
上面的例子需要转化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<Object> list = new ArrayList<Object>(); ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bais); while (bais.available() > 0) { Object obj = (Object) ois.readObject(); if (obj == null) { break; } list.add(obj); } } catch (Exception e) { e.printStackTrace(); } finally { close(bais); close(ois); } return list; } /** * 关闭io流对象 * * @param closeable */ public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } } }
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<TaskManage> list111 = (List<TaskManage>) jedisClient.getList("getlist");
相关文章推荐
- Redis存储Object 和 list<object>
- Redis存储Object 和 list<object>
- MongoDB存储泛型List<User>集合
- Redis存储Object 和 list<object>
- MongoDB存储泛型List<User>集合
- Redis存储List<Map<String, Object>>
- ArrayList、List<T>、HashSet<T>、LinkedList<T>各自优点和缺点,Dictionary<K,V>的内部存储数据方式有什么特殊的?
- 黑马程序员—最常用的一个集合 泛型集合List<>
- Struts2中表达式集合对象List<T>与对象<T>的取值时遇到的变量名的问题
- 仿照List<T>写的集合类。
- .net集合类的研究--Array,ArrayList,List<T>
- 将List集合中的map对象转为List<对象>形式--封装类
- 取List<Object> 集合
- Dictionary<K,V> 与 List<T> 集合的区别及效率
- 有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样高效取出两个集合中不同的元素?
- C#.Net:List<T>集合列表的Linq语句查询示例
- 如何将一个List<Map<String,String>>类型的集合数据转换成json格式输出
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- AsParallel \AsQueryable<T>().ToList() [System.ArgumentOutOfRangeException was unhandled" 索引超出范围。必须为非负值并小于集合大小]解决方法
- C# List<T> 数组,集合等排序