springboot框架学习 集成redis缓存数据库并测试

能够容纳海量用户同时访问的项目的关键是使用了redis缓存数据库技术,它是单线程多路io复用,利用cache高速运行,比起常规的mysql的性能,无疑更快,前几次我们将redis安装在虚拟机端并实现了远程控制虚拟机,其中存在很多配置的问题,大家如果有解决不了的可以评论,我算是都经历一遍了,目前问题最多的就是springboot链接redis数据库出现拒绝访问或者连接不上的问题。其实最多的原因在于你的redis.conf文件的配置有一定的问题。例如bind 127.0.0.1的配置没有注掉,其次默认访问本地与后台启动都没有设置好。密码配置了却没有再yml文件写出来等等一系列问题都会导致连接不上。其次host为虚拟机的主机ip:自己设置的VM网络IP地址。端口为默认的6379。

一、xshell连接虚拟机开启redis服务

[root@localhost src]# ./redis-server /etc/redis.conf
[root@localhost src]# ps -ef | grep redis
root       2724      1  0 02:54 ?        00:00:00 ./redis-server *:6379
root       2730   2443  0 02:54 pts/0    00:00:00 grep --color=auto redis
[root@localhost src]# ./redis-cli -h 192.168.20.150 -p 6379
192.168.20.150:6379> auth hlc(接着图)

 二、pom文件导入两个关于redis集成的依赖项

   <!--redis正常使用的依赖导入-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.11.1</version>
        </dependency>

三、配置springboot核心yml配置文件内容

#redis的配置
spring:
  redis:
    host: 192.168.20.150
# redis服务器连接端口默认6379
    port: 6379
# 数据库索引默认为0号数据库这里选取1号数据库
    database: 1
    lettuce:
      pool:
        max-active: 50
        max-wait: 3000
        max-idle: 20
        min-idle: 2
# 连接超时时间
    timeout: 5000
    password: hlc

四、测试类里利用spring data redis 封装好的api:redisTemplate实现操作redis数据库

package com.hlc.redis_test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class RedisTestApplicationTests {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    void test1() {
//      redisTemplate.opsForValue().set("huang","111");
        String name = redisTemplate.opsForValue().get("huang");
        System.out.println(name);
    }
    @Test
    void test2(){
        redisTemplate.opsForValue().getAndSet("huang","黄林春");
        String name1 = redisTemplate.opsForValue().get("huang");
        System.out.println(name1);
    }
}

五、测试结果

test1(设置string键值对并获取键值输出):

 test2(修改键值的内容并获取输出):

 六、源码追溯:查看opsForValue();方法的源码追踪

我们可以通过idea快捷键Ctrl+鼠标左击利用反射原理追溯源码;

opsForValue:

  public ValueOperations<K, V> opsForValue() {
        return this.valueOps;
    }

继续找valueOps:

 private final ValueOperations<K, V> valueOps = new DefaultValueOperations(this);

继续跟着ValueOperations<K,V>:(我把注释@Nullable删掉了,这里全都是方法!供大家学习使用)

public interface ValueOperations<K, V> {
    void set(K key, V value);
    void set(K key, V value, long timeout, TimeUnit unit);
    default void set(K key, V value, Duration timeout) {
        Assert.notNull(timeout, "Timeout must not be null!");
        if (TimeoutUtils.hasMillis(timeout)) {
            this.set(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS);
        } else {
            this.set(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
        }

    }
    Boolean setIfAbsent(K key, V value);
    Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit);
    default Boolean setIfAbsent(K key, V value, Duration timeout) {
        Assert.notNull(timeout, "Timeout must not be null!");
        return TimeoutUtils.hasMillis(timeout) ? this.setIfAbsent(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS) : this.setIfAbsent(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
    }
    Boolean setIfPresent(K key, V value);
    Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit);
    default Boolean setIfPresent(K key, V value, Duration timeout) {
        Assert.notNull(timeout, "Timeout must not be null!");
        return TimeoutUtils.hasMillis(timeout) ? this.setIfPresent(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS) : this.setIfPresent(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
    }
  
    V get(Object key);
    V getAndDelete(K key);
    V getAndExpire(K key, long timeout, TimeUnit unit);
    V getAndExpire(K key, Duration timeout);
    V getAndPersist(K key);
    V getAndSet(K key, V value);
    Long increment(K key);
    Long increment(K key, long delta);
    Double increment(K key, double delta);
    Long decrement(K key);
    Long decrement(K key, long delta);
    Integer append(K key, String value);
    String get(K key, long start, long end);
    void set(K key, V value, long offset);
    Long size(K key);
    Boolean setBit(K key, long offset, boolean value);
    Boolean getBit(K key, long offset);
    List<Long> bitField(K key, BitFieldSubCommands subCommands);
    RedisOperations<K, V> getOperations();
}

当然这个只是封装好的,我们也可以自己写一个,然后用@Bean注解注入到spring容器中以供使用,明天我的任务就是自己编写一个RedisTemplate类实现redis数据库的操作。

兄弟们,自学太难了,一切问题都得自己在网上或者学习书籍里查原因,不过这也让我对操作更熟悉了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于学习Spring BootRedis的组合,你可以按照以下步骤进行: 1. 首先,确保你对Spring Boot有一定的了解。如果你对Spring Boot还不熟悉,可以先学习一些基础知识,比如Spring框架、依赖注入和控制反转等。 2. 接下来,了解Redis的基本概念和用法。Redis是一个开源的内存数据库,常用于缓存、消息队列和持久化等场景。你可以学习Redis的数据类型、常用命令以及如何在Java中使用Redis。 3. 在Spring Boot中使用Redis,你可以利用Spring提供的集成方式来实现。Spring Boot提供了对Redis的自动配置,只需添加相关依赖和配置即可使用Redis。 4. 在你的Spring Boot项目中添加Redis依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 5. 在配置文件中配置Redis连接信息。在application.properties或application.yml中添加以下配置: ```properties spring.redis.host=your_redis_host spring.redis.port=your_redis_port ``` 6. 创建一个Redis的操作类或服务类,用于封装对Redis的操作。你可以使用Spring提供的RedisTemplate或者自定义封装类来实现对Redis的读写操作。 7. 在你的业务代码中使用Redis。根据你的需求,可以使用Redis进行缓存数据、分布式锁、消息发布订阅等操作。 8. 最后,通过实际的项目开发和实践来深入学习和理解Spring BootRedis的使用。 希望以上步骤对你学习Spring BootRedis有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ForestSpringH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值