原文网址:Redisson--使用/教程/实例_IT利刃出鞘的博客-CSDN博客
说明
本文用示例介绍Redisson的用法。
官网
官网:https://redisson.org/
git:https://github.com/redisson/redisson
git文档:https://github.com/redisson/redisson/wiki
获得Redisson客户端
@Autowired
private RedissonClient redissonClient;
根据key获得value
所有get开头的方法:如果key不存在会在保存时以此key为key保存。
getList。它返回RList,RList实现了java.util.List。所以我们可以像操作List一样操作RList。(其他类型,例如:RMap等也是一样,和JDK的接口一样的操作)。
将key-value写回Redis
所有添加都会自动将值写回Redis。例如:
RSet<String> set = redisson.getSet("anySet");
set.add("Tony");
package com.knife.test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/test")
public String test(){
RBucket<Object> bucket = redissonClient.getBucket("key1");
// 此处可以是任意对象,不仅仅是字符串
bucket.set("value1");
String str1 = (String) rBucket.get();
System.out.println(str1);// 此处会打印出:value1
// 删除"key1"这一项数据
bucket.delete();
return "success";
}
}
本处只展示getList。它返回RList,RList实现了java.util.List。所以我们可以像操作List一样操作RList。(其他类型,例如:RMap等也是一样,和JDK的接口一样的操作)
package com.knife.test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/test")
public String test(){
RList<Object> list1 = redissonClient.getList("list1");
list1.add(123);
list1.add("abc");
list1.remove("abc");
return "success";
}
}
package com.knife.order;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
private RedissonClient redissonClient;
/**
* 提交订单
*/
@PostMapping("/submitOrder")
public String submitOrder(){
RLock lock = redissonClient.getLock(product);
try {
lock.lock();//阻塞
// boolean b = lock.tryLock();//非阻塞
//数据库操作:插入订单数据,扣减库存等
} finally {
lock.unlock();//释放锁
}
return "success";
}
}
官网网址
RKeys - redisson 3.10.0 javadoc
相关网址
Redis--模糊查询--方法/实例_IT利刃出鞘的博客-CSDN博客
实例
package com.knife.test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/test")
public String test(){
RKeys keys = redissonClient.getKeys();
Iterable<String> keysByPattern = keys.getKeysByPattern("abc*");
long delete = keys.deleteByPattern("def?");
return "success";
}
}
所有的类型都有过期时间,例如:
RSet<String> set = redisson.getSet("anySet");
set.add("Tony");
set.expire(3, TimeUnit.MINUTES);
过期时间在类型为空时是无效的,例如:
RSet<String> set = redisson.getSet("anySet");
set.expire(3, TimeUnit.MINUTES);
set.add("Tony");
给Set的某一个值设置过期时间
RSetCache<String> set = redisson.getSetCache("anySet");
// ttl = 10 seconds
set.add("abc", 10, TimeUnit.SECONDS);
给Map的某一个小Key设置过期时间
RMapCache<Object, Object> outerMap = redissonClient.getMapCache("outer");
outerMap.put("innerKey1", "innerValue1", 10, TimeUnit.SECONDS);
方法 | 返回类型 |
getSet("xxx") | RSet<SomeObject> |
getList("xxx") | RList<SomeObject> |
getMap("anyMap"); | RMap<String, SomeObject> |
getSetMultimap("myMultimap"); | RSetMultimap<SimpleKey, SimpleValue> |
getSortedSet("anySet"); | RSortedSet<Integer> |
getScoredSortedSet("simple"); | RScoredSortedSet<SomeObject> |
getLexSortedSet("simple"); | RLexSortedSet set |
getQueue("anyQueue"); | RQueue<SomeObject> |
getDeque("anyDeque"); | RDeque<SomeObject> |
getBlockingQueue("anyQueue"); | BlockingQueue<SomeObject> |
getBoundedBlockingQueue("anyQueue"); | RBoundedBlockingQueue<SomeObject> |
getBlockingDeque("anyDeque"); | RBlockingDeque<Integer> |
getBlockingFairQueue("myQueue"); | RBlockingFairQueue |
getBlockingFairDeque("myDeque"); | RBlockingFairDeque |
getDelayedQueue(distinationQueue); | RDelayedQueue<String |
getPriorityQueue("anyQueue"); | RPriorityQueue<Integer> |
getPriorityDeque("anyQueue"); | RPriorityDeque<Integer> |
getPriorityBlockingQueue("anyQueue"); | RPriorityBlockingQueue<Integer> |
RPriorityBlockingDeque<Integer> | getPriorityBlockingDeque("anyQueue"); |
Redisson最新版starter模式集成_明洋的专栏-CSDN博客_redisson-spring-boot-starter
最简单的spring-boot-starter-redisson发布啦! | 纳兰小筑
2-(1)、SpringBoot整合redisson实现分布式锁 - 简书
(Redis使用系列) Springboot 整合Redisson 实现分布式锁 七-CSDN博客
SpringBoot集成redisson分布式锁 - 自行车上的程序员 - 博客园