写在前面
- redis是一种可基于内存也可基于持久话的日志型、key-value数据库。因为性能高,存储数据类型丰富等优势常被用作数据缓存。
- 本文介绍了springboot2.2.0整合redis的常规步骤。阅读本文,你大概需要5分钟左右的时间
整合redis
一. 安装redis
- 根据你的操作系统选择redis版本下载并安装redis点击下载
- 下载文件重命名为redis->打开该文件->cmd到当前文件夹下->redis-server.exe redis.windows.conf 即可启动redis
- 可以为redis设置环境变量,这个大家都懂的。
注意:防火墙应该为redis打开!
二.引入redis依赖
在pom.xml文件下引入redis相关依赖
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar --> <exclusions> <exclusion> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </exclusion> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加jedis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!--spring2.0集成redis所需common-pool2--> <!-- 必须加上,jedis依赖此 --> <!-- spring boot 2.0 的操作手册有标注 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.5.0</version> </dependency>
3. 全局配置单服务器redis
在properties文件中,加入以下基本配置
spring.redis.port=6379spring.redis.host=127.0.0.1#我的redis连接不需要密码#spring.redis.password=123spring.redis.jedis.pool.max-active=100spring.redis.jedis.pool.max-idle=5spring.redis.jedis.pool.max-wait=60000spring.redis.database=0spring.redis.timeout=10000#若开启redis方式的session存储 type值应为redisspring.session.store-type=redisspring.session.timeout=10server.servlet.session.timeout=10
4.新建RedisServer.java。创建一个对redis的基本操作的类
package com.dbc.usermanager.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Servicepublic class RedisService { @Autowired protected StringRedisTemplate redisTemplate; /** * 写入redis缓存(不设置expire存活时间) * @param key * @param value * @return */ public boolean set(final String key, String value){ boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { System.out.println("写入redis缓存失败!错误信息为:" + e.getMessage()); } return result; } /** * 写入redis缓存(设置expire存活时间) * @param key * @param value * @param expire * @return */ public boolean set(final String key, String value, Long expire){ boolean result = false; try { ValueOperations operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expire, TimeUnit.SECONDS); result = true; } catch (Exception e) { System.out.println("写入redis缓存(设置expire存活时间)失败!错误信息为:" + e.getMessage()); } return result; } /** * 读取redis缓存 * @param key * @return */ public Object get(final String key){ Object result = null; try { ValueOperations operations = redisTemplate.opsForValue(); result = operations.get(key); } catch (Exception e) { System.out.println("读取redis缓存失败!错误信息为:" + e.getMessage()); } return result; } /** * 判断redis缓存中是否有对应的key * @param key * @return */ public boolean exists(final String key){ boolean result = false; try { result = redisTemplate.hasKey(key); } catch (Exception e) { System.out.println("判断redis缓存中是否有对应的key失败!错误信息为:" + e.getMessage()); } return result; } /** * redis根据key删除对应的value * @param key * @return */ public boolean remove(final String key){ boolean result = false; try { if(exists(key)){ redisTemplate.delete(key); } result = true; } catch (Exception e) { System.out.println("redis根据key删除对应的value失败!错误信息为:" + e.getMessage()); } return result; } /** * redis根据keys批量删除对应的value * @param keys * @return */ public void remove(final String... keys){ for(String key : keys){ remove(key); } }}