RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Springboot集成Redis实例分析

这篇“Springboot集成redis实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Springboot集成Redis实例分析”文章吧。

成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、黄浦网络推广、成都小程序开发、黄浦网络营销、黄浦企业策划、黄浦品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供黄浦建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com

依赖包

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

配置文件(application.properties)

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=x.x.x.x
# Redis服务器连接端口
spring.redis.port=6738
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=10000
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1ms
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0

配置文件(RedisConfig.java)

package com.gxr.dmsData.config;

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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.text.SimpleDateFormat;

/**
 * @author :gongxr
 * @description: 自定义RedisTemplate
 * @date :Created in 2021/6/30
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 修改key的默认序列化器为 string
        RedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setDefaultSerializer(stringRedisSerializer);

        // 自定义 对象转换
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        valueSerializer.setObjectMapper(objectMapper);
//        redisTemplate.setValueSerializer(valueSerializer);
//        redisTemplate.setHashValueSerializer(valueSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

测试代码

import com.gxr.dmsData.common.BaseTest;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

/**
 * @author :gongxr
 * @description:
 * @date :Created in 2021/6/30
 */
@Slf4j
public class TestRedis extends BaseTest {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * RedisTemplate中定义了对5种数据结构操作
     * redisTemplate.opsForValue();//操作字符串
     * redisTemplate.opsForHash();//操作hash
     * redisTemplate.opsForList();//操作list
     * redisTemplate.opsForSet();//操作set
     * redisTemplate.opsForZSet();//操作有序set
     */
    @Test
    public void testRedisGet() {
        String key = "adviceCalculateTime";
        Boolean b = redisTemplate.hasKey(key);
        log.info("key是否存在:{}", b);
        Object o = redisTemplate.boundValueOps(key).get();
        log.info(redisTemplate.toString());
        log.info("查询结果:{}", o);
    }

    /**
     * map类型
     */
    @Test
    public void testRedisHash() {
        String key = "RRS_CURRENCY_CACHE";
        Object o = redisTemplate.boundHashOps(key).get("590");
        log.info("查询结果:{}", o.toString());
    }

    /**
     * set类型
     */
    @Test
    public void testRedisSet() {
        String key = "goodsDataSyncSkc:set";
        Set set = redisTemplate.boundSetOps(key).members();
        log.info("查询结果:{}", set.size());
        String s = (String) redisTemplate.boundSetOps(key).randomMember();
        log.info("查询结果:{}", s);
    }

}

以上就是关于“Springboot集成Redis实例分析”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


本文名称:Springboot集成Redis实例分析
本文来源:http://scyingshan.cn/article/jdehis.html