SpringBoot Redis 基础使用

SpringBoot Redis 基础使用

文章目录

  • [SpringBoot Redis 基础使用](#SpringBoot Redis 基础使用)

redis介绍

redis是一个key-value。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets)与范围查询, bitmaps, hyperloglogs和地理空间(geospatial)索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Luascripting), LRU驱动事件(LRU eviction),事务(transactions)和不同级别的 磁盘持久化(persistence), 并通过Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(highavailability)

redis官网地址:http://www.redis.cn/

3.2.100版本下载地址https://github.com/microsoftarchive/redis/releases

5.0.14版本下载地址https://github.com/tporadowski/redis/releases

Windows版的安装比较简单,解压Redis压缩包完成即安装完毕,安装的注意事项:

  • 解压的目录不要有中文
  • 目录结构层次不要太深
  • 硬盘空间剩余空间最少要大于你的内存空间,建议20G以上
  1. 启动redis

在这个目录下cmd,然后直接redis-server.exe redis.windows.conf,即可启动redis

或新建记事本文件stoppable,输入redis-server.exe redis.windows.conf

  1. 修改密码

解压目录下找到redis.windows.conf

(Redis-x64-3.0.504大概在386行,Redis-x64-5.0.14.1大概在503行左右)requirepass 下添加requirepass 密码(),保存,重新运行即可()requirepass 前不要空

  1. 下载安装RedisDesktopManager(可视化界面),并连接redis(关闭防火墙并设置允许远程连接此电脑)
  2. SpringBoot中Redis相关配置

1.引入相关JAR

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.application.yml spring下写(密码没改不用写)

复制代码
redis:
  database: 0 #Redis数据库索引(默认为0)
  port: 6379
  password: 123456
  host: 127.0.0.1
 
      # Redis数据库索引(默认为0)
      spring.redis.database=0 
      # Redis服务器地址
      spring.redis.host=127.0.0.1
      # Redis服务器连接端口
      spring.redis.port=6379 
      # Redis服务器连接密码(默认为空)
      spring.redis.password=123456
      # 连接池最大连接数(使用负值表示没有限制)
      spring.redis.pool.max-active=8 
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      spring.redis.pool.max-wait=-1 
      # 连接池中的最大空闲连接
      spring.redis.pool.max-idle=8 
      # 连接池中的最小空闲连接
      spring.redis.pool.min-idle=0 
      # 连接超时时间(毫秒)
      spring.redis.timeout=0 

3.开始测试

复制代码
@SpringBootTest
public class RedisText {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void addRedis(){
        ValueOperations vo = redisTemplate.opsForValue(); //字符串
        vo.set("ures","12345");
    }
}

 redisTemplate.opsForValue(); //字符串
        vo.set("ures","12345");
    }
}
相关推荐
段帅龙呀1 小时前
Redis构建缓存服务器
服务器·redis·缓存
ai小鬼头3 小时前
Ollama+OpenWeb最新版0.42+0.3.35一键安装教程,轻松搞定AI模型部署
后端·架构·github
萧曵 丶4 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
小七mod5 小时前
【MyBatis】MyBatis与Spring和Spring Boot整合原理
spring boot·spring·mybatis
猴哥源码5 小时前
基于Java+SpringBoot的动物领养平台
java·spring boot
老任与码5 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
华子w9089258595 小时前
基于 SpringBoot+VueJS 的农产品研究报告管理系统设计与实现
vue.js·spring boot·后端
猴哥源码6 小时前
基于Java+SpringBoot的在线小说阅读平台
java·spring boot
星辰离彬6 小时前
Java 与 MySQL 性能优化:Java应用中MySQL慢SQL诊断与优化实战
java·后端·sql·mysql·性能优化
GetcharZp7 小时前
彻底告别数据焦虑!这款开源神器 RustDesk,让你自建一个比向日葵、ToDesk 更安全的远程桌面
后端·rust