springboot 集成Redis 以及 RedisTemplate部分操作

配置类

package com.zfh.springbootdemo.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
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;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        rcm.setUsePrefix(true);
        return rcm;
    }

    @Bean
    @SuppressWarnings("all")//作用是抑制编译器警告
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
//        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // key采用String的序列化方式

        template.setKeySerializer(stringRedisSerializer);

        // hash的key也采用String的序列化方式

        template.setHashKeySerializer(stringRedisSerializer);

        // value序列化方式采用jackson

        template.setValueSerializer(jackson2JsonRedisSerializer);

        // hash的value序列化方式采用jackson

        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();

        return template;

    }

}

(1) String类型:

  • 插入操作

 

UserInfo markIfFirstSync = new UserInfo();
userRedisTemplate.opsForValue().set(CACHE_KEY, markIfFirstSync);
  • 向redis中某个string类型的key下面插入一个对象。

 

批量插入操作
public Map<String, OrgRelationInfo> mappingRelationRefresh = new HashMap<>();
redisTemplate.opsForValue().multiSet(mappingRelationRefresh);
  • 获取对象操作

 

  userRedisTemplate.opsForValue().get(CACHE_NAME + CACHE_KEY_EXIST_PREFIX);
  • 从redis中获取某个key下面的某个对象,如果不存在就返回null。

 

 //批量获取对象操作
  List<String> sourceIdList = new ArrayList<>();
  List<OrgRelationInfo> orgMappingRelationList = redisTemplate.opsForValue().multiGet(sourceIdList);
//从redis中获取多个key下面的多个对象,返回一个List列表,但是即使对应的key下面没有值,这个**value也会返回**,不过是
//null,因此要判断是否List都为空,不能够用isEmpty直接判断,而应该一个一个的判断是否为空,才能判断整体为空。

(2) List类型

  • 批量插入

 

//向redis的某个key下面的list列表里面插入一个list列表,不会去重。
List<OrgRelationInfo> remainOrgNodes = new ArrayList<>();
redisTemplate.opsForList().leftPushAll(CACHE_KEY, remainOrgNodes);
  • 批量取出

 

//从redis中取出某一个key下面的list列表, 0表示从列表的第0个元素开始取,-1表示直取到倒数第一个元素,也就是整个列表的所有元素都取出来。
List<OrgRelationInfo> lastRemainOrgNodeList = redisTemplate.opsForList().range(CACHE_NAME + CACHE_REMAIN_KEY_PREFIX, 0, -1);

(3) Hash类型

  • 批量插入

 

// 向redis中某个key下面插入key,hash的Map。
Map<Long, UserRelationInfo> value = new HashMap<>();
userHashRedisTemplate.opsForHash().putAll(KEY, value );

-单个删除

 

//从redis中某个key下面删除掉某个hashkey所在的value。
userHashRedisTemplate.opsForHash().delete(key, sourceOrgId);
  • 单个获取

 

//从redis中某个key下面得到这个key对应的hashkey的value值。前一个key只能是String类型,hashKey可以声明为自己需要的类型。
userHashRedisTemplate.opsForHash().get(Key, hashKey);
  • 批量获取

 

//从redis中得到某个key下面的所有的hashkey和hashvalue值。
Map<Object, Object> userOrgMap = userHashRedisTemplate.opsForHash().entries(getUserNodeCacheKey(syncUserNode.getSourceId()));

(4) Set类型

  • 单个插入

 

//向redis的某个key下面的set列表里面插入一个元素,回去重,且无序。
userRoleSetRedisTemplate.opsForSet().add(KEY, cloudtOrgRoleInfo);
  • 批量取出

 

//从redis的某个key下面得到set集合的所有元素,返回的也是一个Set集合。
cloudtOrgRoleSet = userRoleSetRedisTemplate.opsForSet().members(KEY);
  • 单个删除

 

//从redis的某个key下面的set集合中删除掉一个元素。
userRoleSetRedisTemplate.opsForSet().remove( KEY, subDeleteOrgRoleUserArray[i]);

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值