SpringBoot2 中 Redis 中的配置

1. 文件内容

1.1 pom.xml

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

1.2 application.properties

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

1.2 RedisConfig.java

package com.ykenan.ykenan.config;

import com.ykenan.ykenan.pojo.YEmployee;
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;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, YEmployee> redisTemplateConfig(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, YEmployee> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<YEmployee> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(YEmployee.class);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }

}

1.2 SpringbootApplicationTests.java

package com.ykenan.ykenan;

import com.ykenan.ykenan.mapper.YEmployeeMapper;
import com.ykenan.ykenan.pojo.YEmployee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot08ApplicationTests {

    @Autowired(required=false)
    private YEmployeeMapper yEmployeeMapper;

    /* 操作的 k-v 是字符串 */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /* 操作的 k-v 是对象 */
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate<Object, YEmployee> redisTemplateConfig;

    @Test
    public void contextLoads() {
        System.out.println(yEmployeeMapper.selectByExample(null));
    }

    @Test
    public void RedisTemplate() {
//        stringRedisTemplate.opsForValue().append("understanding", "Hello redis.");
        String understanding = stringRedisTemplate.opsForValue().get("understanding");
        System.out.println(understanding);
    }

}

2. Redis 配置

2.1 修改 redis.conf 文件

1. 注释 bind 127.0.0.1

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:

2. protected-mode 改为 no

# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

2. daemonize 改为 no

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:

2.2 重新启动 Redis

https://blog.csdn.net/YKenan/article/details/99628321

3. 测试

package com.ykenan.ykenan;

import com.ykenan.ykenan.mapper.YEmployeeMapper;
import com.ykenan.ykenan.pojo.YEmployee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot08ApplicationTests {

    @Autowired(required = false)
    private YEmployeeMapper yEmployeeMapper;

    /* 操作的 k-v 是字符串 */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /* 操作的 k-v 是对象 */
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate<Object, YEmployee> redisTemplateConfig;

    @Test
    public void contextLoads() {
        System.out.println(yEmployeeMapper.selectByExample(null));
    }

    @Test
    public void RedisTemplateValue() {
//        stringRedisTemplate.opsForValue().append("understanding", "Hello redis.");
        String understanding = stringRedisTemplate.opsForValue().get("understanding");
        System.out.println(understanding);
    }

    /**
     * leftPush --> Stack-like arrangement --- Forward is below, backward is above.
     */
    @Test
    public void RedisTemplate() {
//        stringRedisTemplate.opsForList().leftPush("redisList", "redis1");
//        stringRedisTemplate.opsForList().leftPush("redisList", "redis2");
//        stringRedisTemplate.opsForList().leftPush("redisList", "redis3");
//        stringRedisTemplate.opsForList().leftPush("redisList", "redis4");

        /* Look happy */
        if (stringRedisTemplate.opsForList().size("redisList") != null) {
            Long redisList = stringRedisTemplate.opsForList().size("redisList");
            if(redisList != null && redisList != 0L) {
                stringRedisTemplate.opsForList().trim("redisList", 0, redisList);
            }
        }

        /* output */
        List<String> redisList = stringRedisTemplate.opsForList().range("redisList", 0, -1);
        System.out.println(redisList);
    }


    @Test
    public void redisTemplate() {
        YEmployee yEmployee = yEmployeeMapper.selectByPrimaryKey(19164802);
        /* Serialization rules */
//        redisTemplate.opsForValue().set("employee_selectByPrimaryKey", yEmployee);
        /* Customized serialization rules */
        redisTemplateConfig.opsForValue().set("employee_selectByPrimaryKey", yEmployee);
    }

}

3.1 redisTemplate 测试 redisTemplateConfig 测试的区别

redisTemplate 测试 (序列化机制)
在这里插入图片描述
redisTemplateConfig 测试 (自定义的序列化规则)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值