Redis的简单使用

在我们使用Redis之前,我们需要启动Redis(在服务里面查看Redis有没有启动,不知道怎么启动,就去看看怎么下载安装,怎么启动)

jar导入

              <!--redis-->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.16.4</version>
        </dependency>

application.properties


# Redis 服务器地址本地(localhost)
redis.host=localhost

# Redis 服务器端口
redis.port=6379

# Redis 访问密码(如果有设置密码)
redis.password=

# 连接池最大连接数
redis.maxConnections=100

# 连接池最大空闲连接数
redis.maxIdle=10

# 连接池最小空闲连接数
redis.minIdle=5

# 连接超时时间(单位:毫秒)
redis.timeout=5000

application.yml

spring:
  redis:
    # 超时时间
    timeout: 10000ms
    # 服务器地址
    host: localhost
    # 服务器端口
    port: 6379
    # 数据库
    database: 0
    # 密码
    password: 123456
    lettuce:
      pool:
        # 最大连接数,默认8
        max-active: 1024
        # 最大连接阻塞等待时间,默认-1
        max-wait: 10000ms
        # 最大空闲连接
        max-idle: 200
        # 最小空闲连接
        min-idle: 5

引用

// 正确的  
   @Autowired
    private RedisTemplate<String, String> redisTemplate;


    //<String, String> 如果不写泛型就会乱码 
    @Autowired
    private RedisTemplate redisTemplate;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架,而Redis是一个开源的内存数据存储系统。结合使用Spring Boot和Redis可以实现高效的数据缓存和持久化。 在Spring Boot中使用Redis,首先需要在项目的pom.xml文件中添加Redis的依赖。然后,在application.properties或application.yml文件中配置Redis的连接信息,包括主机名、端口号、密码等。 接下来,可以通过使用Spring Data Redis来简化对Redis的操作。Spring Data Redis提供了一系列的注解和模板类,可以方便地进行数据的读取、写入和删除等操作。 以下是一个简单的示例,演示了如何在Spring Boot中使用Redis: 1. 添加依赖: 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 配置Redis连接信息: 在application.properties或application.yml文件中添加以下配置: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis操作类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisUtil { @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public void delete(String key) { redisTemplate.delete(key); } } ``` 4. 使用Redis操作类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private RedisUtil redisUtil; @GetMapping("/user/{id}") public User getUser(@PathVariable String id) { // 先从缓存中获取数据 User user = (User) redisUtil.get("user:" + id); if (user == null) { // 如果缓存中不存在,则从数据库中获取数据 user = userService.getUserById(id); // 将数据存入缓存 redisUtil.set("user:" + id, user); } return user; } } ``` 以上示例中,我们创建了一个RedisUtil类来封装对Redis的操作,然后在UserController中使用RedisUtil来实现对用户数据的缓存。当请求用户数据时,先从缓存中获取,如果缓存中不存在,则从数据库中获取,并将数据存入缓存

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值