liulin
2024-11-24 1e3764d82bd9b77a89a7fb88d60dd411ecd4f11b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.lunhan.xxx.common.cache;
 
import com.lunhan.xxx.common.config.SysConfig;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.lang.Nullable;
 
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
 
public class RedisKeySerializer implements RedisSerializer<String> {
    private static final Charset charset = StandardCharsets.UTF_8;
 
    /*
     * (non-Javadoc)
     * @see org.springframework.data.redis.serializer.RedisSerializer#deserialize(byte[])
     */
    @Override
    public String deserialize(@Nullable byte[] bytes) {
        return (bytes == null ? null : new String(bytes, charset).replaceFirst(SysConfig.redis.getKeyPrefix(), ""));
    }
 
    /*
     * (non-Javadoc)
     * @see org.springframework.data.redis.serializer.RedisSerializer#serialize(java.lang.Object)
     */
    @Override
    public byte[] serialize(@Nullable String string) {
        return (string == null ? null : (SysConfig.redis.getKeyPrefix() + string).getBytes(charset));
    }
}