前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis 存储对

redis 存储对

作者头像
半条命专刊
发布2020-09-14 15:13:44
5280
发布2020-09-14 15:13:44
举报
文章被收录于专栏:程序猿那点儿事儿

如果需要用到Redis存储List对象,而list又不需要进行操作,可以按照MC的方式进行存储,不过Jedis之类的客户端没有提供API,可以有两种思路实现: 1. 分别序列化 elements ,然后 set 存储

2. 序列化List对象,set存储

这两种方法都类似MC的 Object方法存储,运用这种方式意味着放弃Redis对List提供的操作方法。

/**

* Created by IntelliJ IDEA.

* User: taomeng

* Date: 16-10-11

* Time: 上午11:10

* To change this template use File | Settings | File Templates.

*/

public class JedisTest {

private static Logger logger = LoggerFactory.getLogger(JedisTest.class);

public static JedisPool buildJedisPool(){

JedisPoolConfig config = new JedisPoolConfig();

config.setMaxActive(1);

config.setMinIdle(50);

config.setMaxIdle(3000);

config.setMaxWait(5000);

JedisPool jedisPool = new JedisPool(config,

"*****", ****);

return jedisPool;

}

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;

}

public static void testSetElements(){

List<User> testData = buildTestData();

Jedis jedis = buildJedisPool().getResource();

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 = buildJedisPool().getResource();

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());

}

}

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 (IOException e) {

logger.warn("Caught IOException decoding %d bytes of data",

in == null ? 0 : in.length, e);

} catch (ClassNotFoundException e) {

logger.warn("Caught CNFE decoding %d bytes of data",

in == null ? 0 : in.length, e);

} finally {

CloseUtil.close(is);

CloseUtil.close(bis);

}

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 (IOException e) {

logger.warn("Caught IOException decoding %d bytes of data"

in == null ? 0 : in.length, e);

} catch (ClassNotFoundException e) {

logger.warn("Caught CNFE decoding %d bytes of data",

in == null ? 0 : in.length, e);

} finally {

CloseUtil.close(is);

CloseUtil.close(bis);

}

return rv;

}

}

} PS:Redsi和memcached有什么区别哪?请看下回分解。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-09-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿那点儿事儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档