All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bootfastconfig.cache.redis.RedisSerializerBaseCodec Maven / Gradle / Ivy

Go to download

Parent pom providing dependency and plugin management for applications built with Maven

The newest version!
package com.github.bootfastconfig.cache.redis;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.redisson.client.codec.BaseCodec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;
import org.springframework.data.redis.serializer.RedisSerializer;

public class RedisSerializerBaseCodec extends BaseCodec {


    private final Encoder encoder;

    private final Decoder decoder;

    public RedisSerializerBaseCodec(ClassLoader classLoader, RedisSerializerBaseCodec redisSerializerBaseCodec) {
        this.encoder = redisSerializerBaseCodec.getValueEncoder();

        this.decoder = redisSerializerBaseCodec.getValueDecoder();
    }

    public RedisSerializerBaseCodec(RedisSerializer redisSerializer) {
        this.decoder = (buf, state) -> {
            byte[] bytes = new byte[buf.capacity()];
            buf.readBytes(bytes, 0, buf.capacity());
            return redisSerializer.deserialize(bytes);
        };

        this.encoder = in -> {
            ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
            out.writeBytes(redisSerializer.serialize(in));
            return out;
        };
    }


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

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