zoukankan      html  css  js  c++  java
  • 使用RedisTemplate存储Map集合的一点注意

    SpringBoot框架无需进行复杂整合,可使用SpringBoot依赖所引入的 org.springframework.data.redis.core.RedisTemplate来直接进行操作,但需要一些特殊的配置:

    配置类中,实例化时需要进行序列化配置

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
     
    @Configuration
    public class RedisConfig {
     
        /**
         * 实例化 RedisTemplate 对象
         *
         * @return
         */
        @Bean
        public RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
            return redisTemplate;
        }
     
        /**
         * 设置数据存入 redis 的序列化方式,并开启事务
         * 
         * @param redisTemplate
         * @param factory
         */
        private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
            // 如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            // 开启事务
            redisTemplate.setEnableTransactionSupport(true);
            redisTemplate.setConnectionFactory(factory);
        }
     
    }

    使用其进行Map的操作

        @Resource
        private RedisTemplate<String,Object> template;
    
        public void setKey(String key, Map<String, Object> map) {
            this.template.opsForHash().putAll(key, map);
        }
    
        public Map<Object, Object> getMapValue(String key) {
            return this.template.opsForHash().entries(key);
        }
    
        public Object getValue(String key, String hashKey) {
            return this.template.opsForHash().get(key, hashKey);
        }
    
        public void deleteData(List<String> keys) {
            // 执行批量删除操作时先序列化template
            template.setKeySerializer(new JdkSerializationRedisSerializer());
            template.delete(keys);
        }
  • 相关阅读:
    PMP:9.项目资源管理
    @JsonIgnore忽略JSON字段
    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    android加载不到.so文件
    报错Failed to install the following SDK components: platforms;android-29 Android SDK Platform 29
    Mac 终端启动运行Redis
    Mac 命令行执行Sublime
    Bean转为Json指定字段名,防止被修改大小写
    Rest接口入参忽略大小写 使用jackson注解
    mongo批量导入
  • 原文地址:https://www.cnblogs.com/zjfjava/p/14099213.html
Copyright © 2011-2022 走看看