微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Springboot集成Redis举例

依赖包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

配置文件(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<Object, Object> redistemplate(RedisConnectionFactory redisConnectionFactory) {
        Redistemplate<Object, Object> redistemplate = new Redistemplate<>();
        redistemplate.setConnectionFactory(redisConnectionFactory);
        // 修改key的认序列化器为 string
        RedisSerializer<String> 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<Object> 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);
    }

}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐