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

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

能够容纳海量用户同时访问的项目的关键是使用了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集成的依赖项
   
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.apache.commons
            commons-pool2
            2.11.1
        
三、配置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 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 opsForValue() {
        return this.valueOps;
    }

继续找valueOps:

 private final ValueOperations valueOps = new DefaultValueOperations(this);

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

public interface ValueOperations {
    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 bitField(K key, BitFieldSubCommands subCommands);
    RedisOperations getOperations();
}

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/737483.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存