redisTemplate序列化以及封装方法

1、问题描述

大家运行redis得 keys * 命令时,查看key值,有的会发现以下情况, key值前面都有一些其它字符,但是不影响取key值,这是因为redisTemplate没有序列化:
在这里插入图片描述

2、redisTemplate序列化

使用redis时,添加以下代码即可序列化,该类可以作为一个工具类或配置类使用

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;


@Configuration
public class RedisConfig {

	@Autowired(required = false)
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        //定义key序列化方式
        //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
        //定义value的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // template.setKeySerializer(redisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

3、自定义封装的方法

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    public Object getObject(String key) {
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        return valueOperations.get(key);
    }

    public <T> void hset(String key,String field,T value){
        redisTemplate.opsForHash().put(key,field,value);
        return;
    }

    public void hget(String key){
        Map<String,Object> maps = redisTemplate.opsForHash().entries(key);
        for (Map.Entry<String, Object> entry : maps.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= "
                    + entry.getValue());
        }
    }

    public Map<String, JSONObject> hscan(String key){

        Map<String, JSONObject> map = new HashMap<>();
        Cursor<Map.Entry<String, Object>> cursor = redisTemplate.opsForHash().scan(key, ScanOptions.NONE);
        while (cursor.hasNext()){
            Map.Entry<String, Object> entry = cursor.next();
            //JSONObject jsonObject = new JSONObject();
            //jsonObject.put(entry.getKey(), entry.getValue());
            map.put(entry.getKey(), JSONObject.parseObject(entry.getValue().toString()));
        }
        return map;
    }

    public Map<Object,Object> hentries(String key){
        Map<Object,Object> map = redisTemplate.opsForHash().entries(key);
        return map;

    }

    public long hsize(String key){
        return redisTemplate.opsForHash().size(key);
    }

    public boolean hasKey(String key,String field){
        return redisTemplate.opsForHash().hasKey(key,field);
    }


    public <T> T hgetValue(String key,String field){
        T value = (T) redisTemplate.opsForHash().get(key,field);
        return value;
    }

    public <K,T> Map<K,T> hgetByKey(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    public void hdel(String key,String field){
        redisTemplate.opsForHash().delete(key,field);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值