redis serialize object operations

In the process of project development, we can cache some frequently used but basically unchanged object information, which can reduce the time for us to operate databases such as msql

There are two most commonly used methods for caching object information. One is to convert the object information into a string in the form of json and cache it, and the other is to serialize the object and cache it.

When redis caches byte data, it has advantages over caching string data in terms of time overhead and memory overhead, so compared to the first, the second caching method is more worrying.

 

The following is an example of caching user information user

user object information:

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 + "}";
    }
    
    
}

Serialize storage code for 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,"Zhang San",18 );
         int timeout = 5*60 ;
         // Store the user object in redis 
        String result = putUser(user,timeout);
        System.out.println( "Return result: " + 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;
    }
    
}

 

Code git address: https://gitee.com/sjcq/redis.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325161540&siteId=291194637