redis直接存储集合
来源:互联网 发布:网络推广专员工作内容 编辑:程序博客网 时间:2024/06/14 05:47
package com.lq.test;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.List;import java.util.Random;import redis.clients.jedis.Jedis; public class JedisTest { /** * Test Data * @return */ public static List<User> buildTestData(){ User a = new User(); a.setName("a"); User b = new User(); b.setName("b"); List<User> list = new ArrayList<User>(); list.add(a); list.add(b); return list; } /** * Test for */ public static void testSetElements(){ List<User> testData = buildTestData(); Jedis jedis = new Jedis("localhost"); ; String key = "testSetElements" + new Random(1000).nextInt(); jedis.set(key.getBytes(), ObjectsTranscoder.serialize(testData)); //验证 byte[] in = jedis.get(key.getBytes()); List<User> list = ObjectsTranscoder.deserialize(in); for(User user : list){ System.out.println("testSetElements user name is:" + user.getName()); } } public static void testSetEnsemble(){ List<User> testData = buildTestData(); Jedis jedis = new Jedis("localhost"); String key = "testSetEnsemble" + new Random(1000).nextInt(); jedis.set(key.getBytes(), ListTranscoder.serialize(testData)); //验证 byte[] in = jedis.get(key.getBytes()); List<User> list = (List<User>)ListTranscoder.deserialize(in); for(User user : list){ System.out.println("testSetEnsemble user name is:" + user.getName()); } } public static void main(String[] args) { //testSetElements(); testSetEnsemble(); } public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { e.printStackTrace(); } } } static class User implements Serializable{ String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } //对象转换工具类 static class ObjectsTranscoder{ //对象转字节数组 public static byte[] serialize(List<User> value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv=null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); for(User user : value){ os.writeObject(user); } os.writeObject(null); os.close(); bos.close(); rv = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return rv; } //字节数组转对象 public static List<User> deserialize(byte[] in) { List<User> list = new ArrayList<User>(); ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if(in != null) { bis=new ByteArrayInputStream(in); is=new ObjectInputStream(bis); while (true) { User user = (User) is.readObject(); if(user == null){ break; }else{ list.add(user); } } is.close(); bis.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try {bis.close();is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } return list; } } //集合转换工具类 static class ListTranscoder{ //集合转字节数组 public static byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] rv=null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); os.close(); bos.close(); rv = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return rv; } //字节数组转集合 public static Object deserialize(byte[] in) { Object rv=null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if(in != null) { bis=new ByteArrayInputStream(in); is=new ObjectInputStream(bis); rv=is.readObject(); is.close(); bis.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try {bis.close();is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} } return rv; } } }
阅读全文
0 0
- redis直接存储集合
- Redis存储List<T>集合
- redis存储对象和集合
- 怎样在Redis通过StackExchange.Redis 存储集合类型List
- Redis序列化存储Java集合List等自定义类型
- Redis序列化存储Java集合List等自定义类型
- redis存储与读取对象和对象集合
- Redis学习06——存储字符串集合(set)
- redis-集合
- Redis集合
- Redis 集合
- Redis集合
- JAVA使用redis缓存的工具类,实现了javaBean,List集合,Map集合的直接存取
- Redis存储
- redis存储
- redis直接存取object
- 4 Springboot中使用redis存储集合数据,并模拟条件查询、分页读取
- Redis学习07——存储有序字符串集合(sorted set)
- Java中如何正确的终止一个线程
- myBatis基础配置入门
- 战舰世界酷炫涂装
- 51nod 1279 扔盘子(单调栈)
- 第六节 hiveshell的常用命令
- redis直接存储集合
- idea 用maven搭建web项目 (后续添加框架)
- WebLogic BEA-101020问题
- 第七节 hive的数据类型
- Array Nesting
- 并发(一)深入分析Volatile的实现原理
- 相干斑
- python requests用法总结
- 配置servlet