在这里没有下载和安装redis的教程,如何redis还没有下载安装的,请先下载安装redis
1.导入redis的依赖,版本根据自己的springboot决定
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.5.4</version>
</dependency>
2.pom配置文件中添加redis的配置
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-search
server.port=12000
spring.couchbase.env.timeouts.socket-connect=60000
spring.thymeleaf.cache=false
#redis 服务器ip
spring.redis.host=192.168.56.10
spring.redis.port=6379
spring.session.store-type=redis
# spring.redis.password=leyou
#redis请求超时时间,超过此值redis自动断开连接
spring.redis.timeout=10000ms
注意此处,如果redis设置了密码,必须配置密码spring.redis.password=redis密码
3.在需要使用redis的地方注入StringRedisTemplate
/**
* @author wangli
* @create 2022-01-16 22:02
*/
@Controller
public class LoginController {
@Autowired
ThirdPartFeignService thirdPartFeignService;
@Autowired
StringRedisTemplate redisTemplate;
@Autowired
MemberFeignService memberFeignService;
@ResponseBody
@GetMapping("/sms/sendCode")
public R sendCode(@RequestParam("phone") String phone){
//TODO 接口防刷
String redisCode = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
long l = Long.parseLong(redisCode.split("_")[1]);
if (System.currentTimeMillis()-1<60000){
// 60s内不能在发送验证码
return R.error(BizCodeEnume.SMS_CODE_EXCEPTION.getCode(),BizCodeEnume.SMS_CODE_EXCEPTION.getMsg());
}
String code = UUID.randomUUID().toString().substring(0, 5);
String substring = code+"_"+System.currentTimeMillis();
redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,substring,10, TimeUnit.MINUTES);
thirdPartFeignService.sendCode(phone,code);
return R.ok();
}
4.调用StringRedisTemplate的方法进行redis操作
StringRedisTemplate实现了RedisOperations,RedisOperations中有许多操作redis的方法
public interface RedisOperations<K, V> {
@Nullable
<T> T execute(RedisCallback<T> var1);
@Nullable
<T> T execute(SessionCallback<T> var1);
List<Object> executePipelined(RedisCallback<?> var1);
List<Object> executePipelined(RedisCallback<?> var1, RedisSerializer<?> var2);
List<Object> executePipelined(SessionCallback<?> var1);
List<Object> executePipelined(SessionCallback<?> var1, RedisSerializer<?> var2);
@Nullable
<T> T execute(RedisScript<T> var1, List<K> var2, Object... var3);
@Nullable
<T> T execute(RedisScript<T> var1, RedisSerializer<?> var2, RedisSerializer<T> var3, List<K> var4, Object... var5);
@Nullable
<T extends Closeable> T executeWithStickyConnection(RedisCallback<T> var1);
@Nullable
Boolean hasKey(K var1);
@Nullable
Long countExistingKeys(Collection<K> var1);
@Nullable
Boolean delete(K var1);
@Nullable
Long delete(Collection<K> var1);
@Nullable
Boolean unlink(K var1);
@Nullable
Long unlink(Collection<K> var1);
@Nullable
DataType type(K var1);
@Nullable
Set<K> keys(K var1);
5.可以自己写工具类
自己对redis的调用和操作进行封装,自己写一些常用的工具类,方便的进行redis的操作
5.1 创建接口RedisService
/**
* @author wangli
* @data 2022/3/18 17:36
* @Description:
*/
public interface RedisService {
/**
* 存储数据
*/
void set(String key, String value);
/**
* 存储数据
*/
void set(String key, String value, Duration minutes);
/**
* 获取数据
*/
String get(String key);
/**
* 设置超期时间
*/
boolean expire(String key, long expire);
/**
* 删除数据
*/
void remove(String key);
/**
* 自增操作
* @param delta 自增步长
*/
Long increment(String key, long delta);
/**
* 是否失效
* @param key
* @return
*/
Boolean hasKey(String key);
}
5.2 创建实现类RedisServiceImpl
/**
* @author wangli
* @data 2022/3/18 17:37
* @Description:
*/
public class RedisServiceImpl implements RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public void set(String key, String value, Duration minutes) {
stringRedisTemplate.opsForValue().set(key, value,minutes);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public boolean expire(String key, long expire) {
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public void remove(String key) {
stringRedisTemplate.delete(key);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key,delta);
}
@Override
public Boolean hasKey(String key) {
return stringRedisTemplate.hasKey(key);
}
}