添加依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.78</version>
		</dependency>

添加配置

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认为8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认为-1
spring.redis.lettuce.pool.max-wait=-1ms
# 连接池中的最大空闲连接 默认为8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认为 0
spring.redis.lettuce.pool.min-idle=0

自定义 RedisTemplate

package com.example.demo.redis;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author  guochao
 * @date  2021/12/13 23:15
 * @version 1.0
 */
public class ObjectRedisTemplate extends RedisTemplate<String, Object> {

	public ObjectRedisTemplate() {
		setKeySerializer(RedisSerializer.string());
		setValueSerializer(new GenericFastJsonRedisSerializer());
		setHashKeySerializer(RedisSerializer.string());
		setHashValueSerializer(new GenericFastJsonRedisSerializer());
	}

	public ObjectRedisTemplate(RedisConnectionFactory connectionFactory) {
		this();
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}
}

package com.example.demo.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author  guochao
 * @date  2021/12/13 23:16
 * @version 1.0
 */
@Configuration
public class RedisConfig {
    @Bean
    public ObjectRedisTemplate objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return new ObjectRedisTemplate(redisConnectionFactory);
    }
}

定义测试实体类

package com.example.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author guochao
 * @version 1.0
 * @date 2021/12/13 23:18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

定义测试类

package com.example.demo;

import com.example.demo.entity.User;
import com.example.demo.redis.ObjectRedisTemplate;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@Slf4j
public class RedisTest {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private ObjectRedisTemplate objectRedisTemplate;

    /**
     * 对象存储实例
     */
    @Test
    public void testObject() {
        String key = "user:1";
        objectRedisTemplate.opsForValue().set(key, new User(1, "郭碧婷", 20));
        User user = (User) objectRedisTemplate.opsForValue().get(key);
        log.info("uesr: " + user);
    }

    /**
     * 字符串存储实例
     */
    @Test
    public void testString() {
        stringRedisTemplate.opsForValue().set("key", "唐嫣");
        String key = stringRedisTemplate.opsForValue().get("key");
        log.info(key);
    }

    /**
     * 测试时间存储实例
     */
    @Test
    public void testTime() {
        //存储20秒
        stringRedisTemplate.opsForValue().set("key", "鱼闪闪", 20, TimeUnit.SECONDS);
        String key = stringRedisTemplate.opsForValue().get("key");
        log.info(key);
    }

    /**
     * 测试时间存储实例2
     */
    @Test
    public void testTime2() {
        //存储20秒
        stringRedisTemplate.opsForValue().set("key", "鱼闪闪", Duration.ofSeconds(20));
        String key = stringRedisTemplate.opsForValue().get("key");
        log.info(key);
    }
}

源码地址

https://github.com/ofoo/my-spring-boot/tree/master/spring-boot-redis

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐