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");
    }
}
相关推荐
懒羊羊大王呀8 分钟前
Ubuntu20.04中 Redis 的安装和配置
linux·redis
鳄鱼杆11 分钟前
服务器 | Centos 9 系统中,如何部署SpringBoot后端项目?
服务器·spring boot·centos
千|寻17 分钟前
【画江湖】langchain4j - Java1.8下spring boot集成ollama调用本地大模型之问道系列(第一问)
java·spring boot·后端·langchain
程序员岳焱31 分钟前
Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
后端·sql·mysql
龚思凯37 分钟前
Node.js 模块导入语法变革全解析
后端·node.js
天行健的回响39 分钟前
枚举在实际开发中的使用小Tips
后端
wuhunyu1 小时前
基于 langchain4j 的简易 RAG
后端
techzhi1 小时前
SeaweedFS S3 Spring Boot Starter
java·spring boot·后端
酷爱码1 小时前
Spring Boot 整合 Apache Flink 的详细过程
spring boot·flink·apache
cacyiol_Z1 小时前
在SpringBoot中使用AWS SDK实现邮箱验证码服务
java·spring boot·spring