前言

最近准备自己写个redis的公共类,用于对redis进行过操作,结果发现,存进去的key前面多了很多符号:
例如:
image.png

正文

这里是因为序列化的问题,那么我们需要在项目中,手动配置一下redis的序列化,对于key和value都进行设置:

@Configuration
@EnableAutoConfiguration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        ClassLoader classLoader = this.getClass().getClassLoader();
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer(classLoader);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;

    }

}

结语

附上自己编写的redis工具:

public class BaseRedisDaoImpl<T> implements BaseRedisDao<T> {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置过期时间
     * @param key
     * @param data
     * @param time
     */
    @Override
    public void saveWithCache(String key, T data , long time) {
        try {
            ValueOperations<String,T> valueOperations = redisTemplate.opsForValue();
            valueOperations.set(key,data,time);
            log.info("The key {} , has successfully cached",key);
        }catch (Throwable t){
            log.warn("The Key {} cached failed caused : {}",key,t);
        }
    }

    /**
     * 不设置过期时间
     * @param key
     * @param data
     */
    public void saveWithCache(String key,T data){
        saveWithCache(key,data,-1);
    }

    /**
     * 获取key
     * @param key
     * @return
     */
    @Override
    public T getWithCache(String key) {
        try{
            ValueOperations<String,T> valueOperations = redisTemplate.opsForValue();
            return valueOperations.get(key);
        }catch (Throwable t){
            log.warn("Get key {} failed , caused : {}",key,t);
            return null;
        }
    }

    /**
     * 判断是否存在key
     * @param key
     * @return
     */
    @Override
    public Boolean containsKey(String key) {
        try{
            return Optional.ofNullable(redisTemplate.hasKey(key)).orElse(Boolean.FALSE);
        }catch (Throwable t){
            log.warn("get key {} failed , caused by {}",key,t);
            return null;
        }
    }

    /**
     * 判断是否存在list
     * @param key
     * @return
     */
    @Override
    public Boolean containListKey(String key) {
        return containsKey(key);
    }

    /**
     * 判断是否存在set
     * @param key
     * @return
     */
    @Override
    public Boolean containSetKey(String key) {
        return containsKey(key);
    }

    /**
     * 判断是否存在hash
     * @param key
     * @return
     */
    @Override
    public Boolean containHashKey(String key) {
        return containsKey(key);
    }

    /**
     * 删除key
     * @param key
     */
    @Override
    public void removeKey(String key) {
        try {
            redisTemplate.delete(key);
        }catch (Throwable t){
            log.warn("delete key failed , caused by {}",t);
        }
    }

    /**
     * 缓存list
     * @param key
     * @param data
     */
    @Override
    public void cacheList(String key, T data , long time) {
        try{
            ListOperations<String, T> listOperations = redisTemplate.opsForList();
            listOperations.rightPushAll(key,data);
            if (time > 0){
                redisTemplate.expire(key,time, TimeUnit.SECONDS);
            }
        }catch (Throwable t){
            log.error("cache list key {} failed",key);
        }
    }


}

Q.E.D.


Hello welcome to my blog