Home Database Redis How to serialize redisson

How to serialize redisson

Nov 27, 2019 am 10:00 AM
redisson

How to serialize redisson

Redisson is a Java memory-resident data grid implemented on the basis of Redis. Compared with Jedis, which exposes underlying operations, Redisson provides a series of distributed Java common objects and also provides many distributed services. (Recommended learning: Redis video tutorial)

Serialization

Redisson’s object encoding class is used to convert objects Perform serialization and deserialization to read and store the object in Redis.

The default encoder by Redisson is JsonJacksonCodec. When JsonJackson serializes objects with bidirectional references, an infinite loop exception will occur. After fastjson checks out the double reference, it will automatically replace it with the reference character $ref and terminate the cycle.

In my case, I serialized a service. This service has been hosted by spring, and it is also injected into another service. Fastjson can be used to serialize to redis normally, while JsonJackson can Throws infinite loop exception.

In order to make the serialized content visible, there is no need to use redission’s other built-in binary encoders and implement the encoder by yourself:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;

import java.io.IOException;

public class FastjsonCodec extends BaseCodec {

 private final Encoder encoder = in -> {
 ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
 try {
 ByteBufOutputStream os = new ByteBufOutputStream(out);
 JSON.writeJSONString(os, in,SerializerFeature.WriteClassName);
 return os.buffer();
 } catch (IOException e) {
 out.release();
 throw e;
 } catch (Exception e) {
 out.release();
 throw new IOException(e);
 }
 };

 private final Decoder<Object> decoder = (buf, state) ->
 JSON.parseObject(new ByteBufInputStream(buf), Object.class);

 @Override
 public Decoder<Object> getValueDecoder() {
 return decoder;
 }

 @Override
 public Encoder getValueEncoder() {
 return encoder;
 }
}
Copy after login

More Redis related technical articles, Please visit the Redis Getting Started Tutorial column to learn!

The above is the detailed content of How to serialize redisson. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How SpringBoot integrates Redisson to implement delay queue How SpringBoot integrates Redisson to implement delay queue May 30, 2023 pm 02:40 PM

Usage scenario 1. The order was placed successfully but the payment was not made within 30 minutes. The payment timed out and the order was automatically canceled. 2. The order was signed and no evaluation was conducted for 7 days after signing. If the order times out and is not evaluated, the system defaults to a positive rating. 3. The order is placed successfully. If the merchant does not receive the order for 5 minutes, the order is cancelled. 4. The delivery times out, and push SMS reminder... For scenarios with long delays and low real-time performance, we can Use task scheduling to perform regular polling processing. For example: xxl-job Today we will pick

Java Redis Redisson configuration example analysis Java Redis Redisson configuration example analysis Apr 25, 2023 am 08:19 AM

需要的Mavenorg.springframework.bootspring-boot-starter-data-redisio.lettucelettuce-coreredis.clientsjedisorg.springframework.sessionspring-session-data-redisorg.redissonredisson3.17.5application-redis.ymlspring:redis:host:106.12.174.220port:6379password

The king solution among distributed locks - Redisson The king solution among distributed locks - Redisson Aug 24, 2023 pm 03:31 PM

If you have been using Redis before, you will get twice the result with half the effort by using Redisson. Redisson provides the simplest and most convenient way to use Redis. The purpose of Redisson is to promote users' separation of concerns (Separation of Concern) from Redis, so that users can focus more on processing business logic.

Learn about Redisson caching technology Learn about Redisson caching technology Jun 21, 2023 am 09:54 AM

Redisson is a Redis-based caching solution for Java applications. It provides many useful features that make using Redis as a cache in Java applications more convenient and efficient. The caching functions provided by Redisson include: 1. Distributed mapping (Map): Redisson provides some APIs for creating distributed maps. These maps can contain key-value pairs, hash entries, or objects, and they can support sharing among multiple nodes.

The differences and usage scenarios between Redis and Redisson frameworks The differences and usage scenarios between Redis and Redisson frameworks May 11, 2023 pm 03:40 PM

Redis and Redisson are two important tools in modern in-memory data storage and distributed data storage. Redis is an open source in-memory database that supports different data structures such as strings, lists, hash tables, sets, etc. Redisson is a distributed data service framework written in Java language, which can easily map Java objects to distributed storage. Redis and Redisson have some same usage scenarios, such as: Caching: Redis and R

Using Redisson for distributed lock processing in Java API development Using Redisson for distributed lock processing in Java API development Jun 17, 2023 pm 09:08 PM

With the continuous development of Internet technology and the diversification of application scenarios, distributed applications have become the standard for modern Internet applications. In distributed applications, in order to coordinate data synchronization and collaboration between nodes, a distributed lock mechanism needs to be used. Redisson is a distributed lock framework based on Redis technology. It provides a simple and easy-to-use API to facilitate Java developers to use distributed locks in development. This article mainly introduces the use of Redisson for distributed lock processing in JavaAPI development.

Java backend development: Using Redisson to implement distributed API locks Java backend development: Using Redisson to implement distributed API locks Jun 17, 2023 am 10:40 AM

As Internet applications continue to increase and the number of users continues to increase, the demand for distributed systems is getting higher and higher. In order to ensure the stability and data consistency of distributed systems, the use of locks is essential. However, in distributed systems, the implementation of locks is difficult and complex. Traditional lock implementation methods are difficult to meet the requirements of high concurrency and high availability. Therefore, this article will introduce how to use Redisson to implement distributed API locks to solve lock problems in distributed systems. Redisson is an implementation based on Redis

How spring boot integrates redisson How spring boot integrates redisson May 14, 2023 pm 07:46 PM

Integration and precautions redisson supports redis environment, stand-alone, cluster, sentinel, cloud, etc. Here I will talk about what you need to pay attention to in the cluster mode. Redisson will detect whether the master/slave node is normal when it is started. Generally speaking, there is no problem with 3 shards, 3 masters and 3 slaves. However, if the test environment has 1 shard, 1 master and 1 slave, or None of the 3 masters can be started. In addition to the environment, you also need to pay attention to compatibility with or without passwords. Manually inject the redisson configuration. Generally, the production environment has a password. If there is a password, it is recommended to manually inject the redisson configuration without using springboot to help you integrate it, because springboot may not be able to recognize the password.

See all articles