springboot整合redis的使用、安装连接图形工具 Redis Desktop Manager(RDM)(2022-08-20-2329更新完成)

101 阅读4分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第23天,点击查看活动详情

目录

一、在pom.xml文件中导入redis依赖

二、本地安装redis、启动redis、redis可视化界面工具

1、安装redis

2、启动redis

3、安装 、连接 redis 图形界面工具

 三、代码使用 redis

1、引导类,开启 springboot 对缓存的支持

2、在业务层,需要使用缓存的方法上,写上注解

3、运行测试,springboot使用redis进行缓存的功能

4、查看 redis 图形界面

5、命令行查看 redis 数据


一、在pom.xml文件中导入redis依赖

        <!--导入redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

二、本地安装redis、启动redis、redis可视化界面工具

1、安装redis

JavaEE后台环境搭建:1、Mac系统安装配置Redis_鲁迷那的专栏—坚持实践后再写出来!-CSDN博客

2、启动redis

安装好之后。在Mac终端输入redis-server,就可以启动Redis服务器了。

当你看到这个熟悉的文字图形,就说明Redis启动成功了。

3、安装 、连接 redis 图形界面工具

这里推荐一个Redis Desktop Manager,下载地址:www.pc6.com/mac/486661.…

使用Redis-Desktop-Manager工具连接redis,实现可视化操作。

 

连接成功后如下图,redis默认有16个库。

 三、代码使用 redis

1、引导类,开启 springboot 对缓存的支持

package com.example.demo;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
import javax.persistence.Entity;


/**
 * springboot的引导类
 */
@SpringBootApplication
//@MapperScan(value = "com.example.demo.dao")
@EnableCaching //开启 springboot 对缓存的支持
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

2、在业务层,需要使用缓存的方法上,写上注解

package com.example.demo.service.impl;

import com.example.demo.bean.User;
import com.example.demo.dao.IUserDao;
import com.example.demo.dao.IUserMapper;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


/**
 * 用户的业务层实现类
 * @Author yyh
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;//spring data jpa的实现

    @Autowired
    private IUserMapper userMapper;// 由 jpa 升级为 mybatis 的实现

    
    @Override
    //表示当前方法使用缓存,并存入redis数据库中
    @Cacheable(value = "findAllCache", key = "'user.findAll'")
    //value属性:表示存入 redis 数据库的 key
    //key属性:用于指定方法执行返回值的key,该属性是spring用的。不写也有默认值
    //同时它还支持spring的 EL表达式 
    public List<User> findAllUser() {
        //return userDao.findAll();
        System.out.println("find database...");
        return userMapper.findAllUser();
    }


    @Override
    public List<User> findAllUserByName(String name) {
        System.out.println("name:"+name);
        return userMapper.findAllByName(name);
    }

}

查看源码如下:key默认值为空,同时它还支持 EL表达式 

3、运行测试,springboot使用redis进行缓存的功能

(1)第一次,取数据库里面的数据

启动程序,浏览器输入地址:http://localhost:8080/user/findAll

从数据库查询到的数据,如下:

得到的程序,输出结果: "find database..."

(2)第一次以后,刷新取的是redis缓存数据

之后无论你在浏览器上面刷新多少次,它不会再输出 "find database..."

说明:

执行了a ,去取redis的缓存数据;

没有执行b,打印输出和再次去查询数据库的数据

4、查看 redis 图形界面

我们会发现,db0 里面多了一个文件夹,如下:

因为数据里面有中文,所以看不到具体效果

5、命令行查看 redis 数据

 如下图:

首先,一个命令行界面启动 redis :

$ redis-server

然后,另一个命令行界面获取数据

$ redis-cli

127.0.0.1:6379> get findAllCache::user.findAll

"\xac\xed\x00\x05sr\x00\x13java.util.ArrayListx\x81\xd2\x1d\x99\xc7a\x9d\x03\x00\x01I\

x00\x04sizexp\x00\x00\x00\x02w\x04\x00\x00\x00\x02sr\x00\x1acom.example.demo.bean.User

\x9e\xaf\xdeX\x12H\x99\xff\x02\x00\x04L\x00\x02idt\x00\x10Ljava/lang/Long;L\x00\x04namet\x00

\x12Ljava/lang/String;L\x00\bpassWordq\x00~\x00\x04L\x00\tuser_nameq\x00~\x00\x04xpsr\x00

\x0ejava.lang.Long;\x8b\xe4\x90\xcc\x8f#\xdf\x02\x00\x01J\x00\x05valuexr\x00\x10java.lang.Number

\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x00\x00\x00\x00\x01t\x00\x06\xe5\xbc

\xa0\xe4\xb8\x89t\x00\x06123456t\x00\bzhangsansq\x00~\x00\x02sq\x00~\x00\x06\x00\x00\x00\x00

\x00\x00\x00\x02t\x00\x06\xe6\x9d\x8e\xe5\x9b\x9bt\x00\x06123456t\x00\x04lisix"

127.0.0.1:6379>