码迷,mamicode.com
首页 > 其他好文 > 详细

redis序列化对象操作

时间:2018-04-30 13:29:32      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:oid   tar   tee   err   turn   git   timeout   数据   close   

在项目开发过程中,一些经常用到但又基本不变的对象信息我们可以把它缓存起来,这样可以减少我们操作msql等数据库的时间

缓存对象信息最常用的有两种,一种是将对象信息转成json形式的字符串缓存起来,另一种将对象系列化缓存起来

redis缓存byte数据时无论是在时间开销还是内存开销上都比缓存string数据有优势,所以相较于第一种,第二种缓存方式更忧一点

 

下面以缓存用户信息user为例进行操作

user对象信息:

package com.study.model;

public class User {

    private Integer id;
    
    private String name;
    
    private Integer age;
    
    public User() {
        super();
    }

    public User(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User {id:" + id + ", name:" + name + ", age:" + age + "}";
    }
    
    
}

对user进行系列化存储代码:

package com.study.util;

import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.study.model.User;

import redis.clients.jedis.Jedis;

public class RedisSerializable {
    
    private static RuntimeSchema<User> schema = RuntimeSchema.createFrom(User.class);

    public static void main(String[] args) {
        
        User user = new User(1,"张三",18);
        int timeout = 5*60;
        //将user对象存入redis
        String result = putUser(user,timeout);
        System.out.println("返回结果:" + result);
        
        User user1 = getUser(1);
        System.out.println(user1.toString());
    }
    
    public static String putUser(User user,int timeout) {
        String result = "";
        try {
            Jedis jedis = RedisUtil.getJedis();
            try {
                String key = "user:" + user.getId();
                byte[] bytes = ProtostuffIOUtil.toByteArray(user, schema, 
                        LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
                result = jedis.setex(key.getBytes(), timeout, bytes);
                return result;
            }finally {
                jedis.close();
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public static User getUser(int id) {
        try {
            Jedis jedis = RedisUtil.getJedis();
            try {
                String key = "user:"+id;
                byte[] bytes = jedis.get(key.getBytes());
                if(bytes != null && bytes.length>0) {
                    User user = schema.newMessage();
                    ProtostuffIOUtil.mergeFrom(bytes, user, schema);
                    
                    return user;
                }
            }finally {
                jedis.close();
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
}

 

代码git地址:https://gitee.com/sjcq/redis.git

redis序列化对象操作

标签:oid   tar   tee   err   turn   git   timeout   数据   close   

原文地址:https://www.cnblogs.com/sjcq/p/8973353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!