• redisson常用api


            redisson提供了很多对象类型的api,下面介绍下一些常用的对象api。 

    RBucket

            可操作任何对象的api,前提是要确定好泛型,方法比较少。大小限制为512Mb。

    1. RBucket bucket = redisson.getBucket("anyObject");
    2. bucket.set(new AnyObject(1));
    3. AnyObject obj = bucket.get();
    4. bucket.trySet(new AnyObject(3));
    5. bucket.compareAndSet(new AnyObject(4), new AnyObject(5));
    6. bucket.getAndSet(new AnyObject(6));

    RMap

            专门操作map的对象,实现了ConcurrentMap接口,并且put、set操作直接作用于redis

    1. RMap map = redisson.getMap("anyMap");
    2. SomeObject prevObject = map.put("123", new SomeObject());
    3. SomeObject currentObject = map.putIfAbsent("323", new SomeObject());
    4. SomeObject obj = map.remove("123");
    5. // use fast* methods when previous value is not required
    6. map.fastPut("a", new SomeObject());
    7. map.fastPutIfAbsent("d", new SomeObject());
    8. map.fastRemove("b");
    9. RFuture putAsyncFuture = map.putAsync("321");
    10. RFuture fastPutAsyncFuture = map.fastPutAsync("321");
    11. map.fastPutAsync("321", new SomeObject());
    12. map.fastRemoveAsync("321");

    RList

            专门操作list的对象,实现了java.util.List, add、set等方法直接作用于redis。

    1. RList list = redisson.getList("anyList");
    2. list.add(new SomeObject());
    3. list.get(0);
    4. list.remove(new SomeObject());

    自定义工具类代码 

    1. package com.springboot.demo.base.utils;
    2. import java.time.Duration;
    3. import java.util.List;
    4. import java.util.Map;
    5. import java.util.concurrent.TimeUnit;
    6. import org.apache.commons.lang3.ObjectUtils;
    7. import org.redisson.api.RBucket;
    8. import org.redisson.api.RList;
    9. import org.redisson.api.RLock;
    10. import org.redisson.api.RMap;
    11. import org.redisson.api.RedissonClient;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.stereotype.Component;
    14. /**
    15. * @description: redisson工具类
    16. * @author: 小花卷的Dad
    17. * @create: 2023/8/24
    18. */
    19. @Component
    20. public class RedissonUtil {
    21. private static RedissonClient redissonClient;
    22. /**
    23. * 锁默认释放时间
    24. */
    25. private static final long default_lease_time = 5L;
    26. @Autowired
    27. public void setRedissonClient(RedissonClient redissonClient) {
    28. RedissonUtil.redissonClient = redissonClient;
    29. }
    30. /**
    31. * key是否存在
    32. * @param key
    33. * @return
    34. */
    35. public static boolean isExists(String key){
    36. return redissonClient.getBucket(key).isExists();
    37. }
    38. /**
    39. * 获取生命周期
    40. * @param key
    41. * @return
    42. */
    43. public static long getExpireTime(String key){
    44. return redissonClient.getBucket(key).remainTimeToLive();
    45. }
    46. /**
    47. * 设置生命周期
    48. * @param key
    49. * @param time(毫秒)
    50. * @return
    51. */
    52. public static boolean setExpireTime(String key, Long expire){
    53. return redissonClient.getBucket(key).expire(Duration.ofMillis(expire));
    54. }
    55. public static boolean delete(String key){
    56. if(!isExists(key)){
    57. return true;
    58. }
    59. return redissonClient.getBucket(key).delete();
    60. }
    61. /**
    62. * 保存字符串
    63. * @param key
    64. * @param value
    65. */
    66. public static void setStr(String key, String value){
    67. RBucket rBucket = redissonClient.getBucket(key);
    68. rBucket.set(value);
    69. }
    70. /**
    71. * 保存字符串
    72. * @param key
    73. * @param value
    74. * @param expire
    75. */
    76. public static void setStr(String key, String value, Long expire){
    77. RBucket rBucket = redissonClient.getBucket(key);
    78. rBucket.set(value, Duration.ofMillis(expire));
    79. }
    80. /**
    81. * 查询字符串
    82. * @param key
    83. * @return
    84. */
    85. public static String getStr(String key){
    86. if(isExists(key)){
    87. return null;
    88. }
    89. RBucket rBucket = redissonClient.getBucket(key);
    90. return rBucket.get();
    91. }
    92. /**
    93. * 保存对象
    94. * @param key
    95. * @param value
    96. * @param
    97. */
    98. public static void setObject(String key, T value){
    99. RBucket rBucket = redissonClient.getBucket(key);
    100. rBucket.set(value);
    101. }
    102. /**
    103. * 保存对象
    104. * @param key
    105. * @param value
    106. * @param expire
    107. * @param
    108. */
    109. public static void setObject(String key, T value, Long expire){
    110. RBucket rBucket = redissonClient.getBucket(key);
    111. rBucket.set(value, Duration.ofMillis(expire));
    112. }
    113. /**
    114. * 查询对象
    115. * @param key
    116. * @return
    117. */
    118. public static T getObject(String key){
    119. RBucket rBucket = redissonClient.getBucket(key);
    120. return rBucket.get();
    121. }
    122. /**
    123. * map.get
    124. * @param key
    125. * @param mapKey
    126. * @param
    127. * @return
    128. */
    129. public static T mapGet(String key, String mapKey){
    130. if(!isExists(key)){
    131. return null;
    132. }
    133. Map rMap = redissonClient.getMap(key);
    134. return rMap.get(mapKey);
    135. }
    136. /**
    137. * 查询map
    138. * @param key
    139. * @param
    140. * @return
    141. */
    142. public static Map mapGetAll(String key){
    143. RMap rMap = redissonClient.getMap(key);
    144. return rMap.readAllMap();
    145. }
    146. /**
    147. * map.put
    148. * @param key
    149. * @param mapKey
    150. * @param mapValue
    151. * @param
    152. */
    153. public static void mapPut(String key, String mapKey,T mapValue){
    154. RMap rMap = redissonClient.getMap(key);
    155. rMap.put(mapKey, mapValue);
    156. }
    157. /**
    158. * map.putAll
    159. * @param key
    160. * @param map
    161. * @param
    162. */
    163. public static void mapPutAll(String key, Map map){
    164. RMap rMap = redissonClient.getMap(key);
    165. rMap.putAll(map);
    166. }
    167. /**
    168. * map.contains
    169. * @param key
    170. * @param mapKey
    171. * @return
    172. */
    173. public static boolean mapContains(String key, String mapKey){
    174. if(!isExists(key)){
    175. return false;
    176. }
    177. Map rMap = redissonClient.getMap(key);
    178. return rMap.containsKey(mapKey);
    179. }
    180. /**
    181. * list.get
    182. * @param key
    183. * @param listIndex
    184. * @param
    185. * @return
    186. */
    187. public static T listGet(String key, int listIndex){
    188. if(!isExists(key)){
    189. return null;
    190. }
    191. if(listIndex < 0){
    192. return null;
    193. }
    194. RList rList = redissonClient.getList(key);
    195. if(rList.size()-1 < listIndex){
    196. return null;
    197. }
    198. return rList.get(listIndex);
    199. }
    200. /**
    201. * list.getAll
    202. * @param key
    203. * @param
    204. * @return
    205. */
    206. public static List listGetAll(String key){
    207. RList rList = redissonClient.getList(key);
    208. return rList.readAll();
    209. }
    210. /**
    211. * list.add
    212. * @param key
    213. * @param addValue
    214. * @param
    215. */
    216. public static void listAdd(String key, T addValue){
    217. RList rList = redissonClient.getList(key);
    218. rList.add(addValue);
    219. }
    220. /**
    221. * list.add
    222. * @param key
    223. * @param addList
    224. * @param
    225. */
    226. public static void listAddAll(String key, List addList){
    227. RList rList = redissonClient.getList(key);
    228. rList.addAll(addList);
    229. }
    230. /**
    231. * list.set
    232. * @param key
    233. * @param listIndex
    234. * @param setValue
    235. * @param
    236. */
    237. public static void listSet(String key, int listIndex, T setValue){
    238. RList rList = redissonClient.getList(key);
    239. if(rList.size()-1 < listIndex){
    240. return;
    241. }
    242. rList.set(listIndex, setValue);
    243. }
    244. }

  • 相关阅读:
    如何在 DigitalOcean Kubernetes 上设置 NGINX 入口控制器
    ELK学习总结
    Anycloud37D平台移植wpa_supplicant
    【JavaScript高级程序设计】重点-第四章笔记:原始值与引用值、执行上下文与作用域、垃圾回收
    Uniapp零基础开发学习笔记(11)-安装扩展组件uni-ui/uView及微信小程序开发环境
    DNS解析
    红细胞膜包裹仿生型六价铬还原去除剂/磁性纳米马达/PFC高负载的聚合物仿生纳米颗粒的研究
    力扣1482.制作m束花所需的最少时间
    深入了解 Spring 篇之 BeanDefinition 结构
    开源大模型与闭源大模型那个更好?
  • 原文地址:https://blog.csdn.net/lizsy/article/details/132695107