redis

说明

一款开源的键值对非关系型数据库。

下载

yum版

yum install epel-release
yum makecache
yum install redis -y
systemctl start redis
systemctl enable redis

# 配置文件修改
bind 0.0.0.0  # 配置文件修改允许远程连接
requirepass 196691 # 设置密码

# 重启
systemctl restart redis
systemctl status redis

docker版

# 拉镜像
docker pull redis
# 创建目录
mkdir -p /mnt/redis/{data,conf}

# 创建配置文件
touch /mnt/redis/conf/redis.conf


# 在/mnt/redis/redis.conf下面添加
bind 0.0.0.0
port 6379
dir /data
dbfilename dump.rdb
appendonly no
requirepass 196691


# 创建容器
docker run \
-d \
--name redis \
-p 6379:6379 \
--restart unless-stopped \
-v /mnt/redis/data:/data \
-v /mnt/redis/conf/redis.conf:/etc/redis/redis.conf \
redis:latest \
redis-server /etc/redis/redis.conf

# 进入容器
docker exec -it redis /bin/bash
redis-cli -a 123456
set name zou
keys *
# 重启
docker restart redis

#查看端口
netstat -tuln

配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
  data:
    redis:
      #  Redis 服务器地址
      host: localhost
      #  Redis 服务器连接端口
      port: 6379
      #  Redis 服务器密码(如果没有设置,可以省略或留空)
      password: yourpassword 
      #  连接的数据库索引,默认为0
      database: 0
      #  连接超时时间
      timeout: 5s
      #  Lettuce 客户端连接池配置(可选,但推荐配置以提升性能)
      lettuce:
        pool:
          # 最大活跃连接数
          max-active: 8
          # 最大空闲连接数
          max-idle: 8
          # 最小空闲连接数
          min-idle: 0
          # 获取连接的最大等待时间,-1表示无限等待
          max-wait: -1ms

序列化

package com.example.demo.config;

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

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // Key 采用 String 序列化
        template.setKeySerializer(new StringRedisSerializer());
        // Hash 的 Key 也采用 String 序列化
        template.setHashKeySerializer(new StringRedisSerializer());

        // Value 和 Hash Value 采用 JSON 序列化 (自动处理对象转 JSON)
        // Spring Boot 3 推荐使用 GenericJackson2JsonRedisSerializer,它会自动识别类型
        GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

用法

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 1. 存储字符串
    public void setCache(String key, String value) {
        redisTemplate.opsForValue().set(key, value, 30, TimeUnit.MINUTES); // 30分钟过期
    }

    // 2. 获取字符串
    public String getCache(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }

    // 3. 存储对象 (自动序列化为 JSON)
    public void setUserObj(String key, User user) {
        redisTemplate.opsForValue().set(key, user, 1, TimeUnit.HOURS);
    }

    // 4. 获取对象 (自动反序列化为 User)
    public User getUserObj(String key) {
        return (User) redisTemplate.opsForValue().get(key);
    }

    // 5. 计数器 (原子操作,高性能)
    public long incrementVisitCount(String key) {
        return redisTemplate.opsForValue().increment(key, 1);
    }
    
    // 6. 删除
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}

redis
http://example.com/2026/03/14/redis/
作者
zouwen
发布于
2026年3月14日
许可协议