一开始以为Spring下操作哈希表,列表,真就是那么土。恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下。

通过spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令。其实还有一些命令,当前版本不支持。不过,这些List的操作方法可以实现队列,堆栈的正常操作,足够用了。

相关链接:

Redis实战

Redis实战之Redis + Jedis

Redis实战之征服 Redis + Jedis + Spring (一)

Redis实战之征服 Redis + Jedis + Spring (二)

Redis实战之征服 Redis + Jedis + Spring (三)

为了简便操作,我使用了StringRedisTemplate。用字符串操作做展示。当然,你可以继续使用RedisTemplate。

闲言少叙,上代码,一目了然:

  1. /**
  2. * Mar 5, 2013
  3. */
  4. package org.zlex.redis.support;
  5. import java.util.List;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.redis.core.StringRedisTemplate;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. *
  11. * @author snowolf
  12. * @version 1.0
  13. * @since 1.0
  14. */
  15. @Component("listOps")
  16. public class ListOps {
  17. @Autowired
  18. private StringRedisTemplate stringRedisTemplate;
  19. /**
  20. * 压栈
  21. *
  22. * @param key
  23. * @param value
  24. * @return
  25. */
  26. public Long push(String key, String value) {
  27. return stringRedisTemplate.opsForList().leftPush(key, value);
  28. }
  29. /**
  30. * 出栈
  31. *
  32. * @param key
  33. * @return
  34. */
  35. public String pop(String key) {
  36. return stringRedisTemplate.opsForList().leftPop(key);
  37. }
  38. /**
  39. * 入队
  40. *
  41. * @param key
  42. * @param value
  43. * @return
  44. */
  45. public Long in(String key, String value) {
  46. return stringRedisTemplate.opsForList().rightPush(key, value);
  47. }
  48. /**
  49. * 出队
  50. *
  51. * @param key
  52. * @return
  53. */
  54. public String out(String key) {
  55. return stringRedisTemplate.opsForList().leftPop(key);
  56. }
  57. /**
  58. * 栈/队列长
  59. *
  60. * @param key
  61. * @return
  62. */
  63. public Long length(String key) {
  64. return stringRedisTemplate.opsForList().size(key);
  65. }
  66. /**
  67. * 范围检索
  68. *
  69. * @param key
  70. * @param start
  71. * @param end
  72. * @return
  73. */
  74. public List<String> range(String key, int start, int end) {
  75. return stringRedisTemplate.opsForList().range(key, start, end);
  76. }
  77. /**
  78. * 移除
  79. *
  80. * @param key
  81. * @param i
  82. * @param value
  83. */
  84. public void remove(String key, long i, String value) {
  85. stringRedisTemplate.opsForList().remove(key, i, value);
  86. }
  87. /**
  88. * 检索
  89. *
  90. * @param key
  91. * @param index
  92. * @return
  93. */
  94. public String index(String key, long index) {
  95. return stringRedisTemplate.opsForList().index(key, index);
  96. }
  97. /**
  98. * 置值
  99. *
  100. * @param key
  101. * @param index
  102. * @param value
  103. */
  104. public void set(String key, long index, String value) {
  105. stringRedisTemplate.opsForList().set(key, index, value);
  106. }
  107. /**
  108. * 裁剪
  109. *
  110. * @param key
  111. * @param start
  112. * @param end
  113. */
  114. public void trim(String key, long start, int end) {
  115. stringRedisTemplate.opsForList().trim(key, start, end);
  116. }
  117. }
/*** Mar 5, 2013*/
package org.zlex.redis.support;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;/*** * @author snowolf* @version 1.0* @since 1.0*/
@Component("listOps")
public class ListOps {@Autowiredprivate StringRedisTemplate stringRedisTemplate;/*** 压栈* * @param key* @param value* @return*/public Long push(String key, String value) {return stringRedisTemplate.opsForList().leftPush(key, value);}/*** 出栈* * @param key* @return*/public String pop(String key) {return stringRedisTemplate.opsForList().leftPop(key);}/*** 入队* * @param key* @param value* @return*/public Long in(String key, String value) {return stringRedisTemplate.opsForList().rightPush(key, value);}/*** 出队* * @param key* @return*/public String out(String key) {return stringRedisTemplate.opsForList().leftPop(key);}/*** 栈/队列长* * @param key* @return*/public Long length(String key) {return stringRedisTemplate.opsForList().size(key);}/*** 范围检索* * @param key* @param start* @param end* @return*/public List<String> range(String key, int start, int end) {return stringRedisTemplate.opsForList().range(key, start, end);}/*** 移除* * @param key* @param i* @param value*/public void remove(String key, long i, String value) {stringRedisTemplate.opsForList().remove(key, i, value);}/*** 检索* * @param key* @param index* @return*/public String index(String key, long index) {return stringRedisTemplate.opsForList().index(key, index);}/*** 置值* * @param key* @param index* @param value*/public void set(String key, long index, String value) {stringRedisTemplate.opsForList().set(key, index, value);}/*** 裁剪* * @param key* @param start* @param end*/public void trim(String key, long start, int end) {stringRedisTemplate.opsForList().trim(key, start, end);}
}

这里说明下,例如LPUSH,RPUSH,其实就是从左边压栈,还是从右边压栈的不同命令。可以把堆栈看作是一个从左至右的数组。如果左边压栈,右边出栈,那就是队列的入队/出队操作;如果左边压栈,左边出栈,那就是堆栈操作。

举个具体的例子:

队列操作:LPUSH入队,RPOP出队,同理,可把L|R替换。

堆栈操作:LPUSH压栈,LPOP出栈,同理,可把L|R替换。

下面进行测试用例,初始、结束时,分别做入队、出队操作,期间进行堆栈,队列操作。不用我细说了,看测试用例,很简单!

  1. /**
  2. * Mar 5, 2013
  3. */
  4. package org.zlex.redis;
  5. import static org.junit.Assert.*;
  6. import java.util.List;
  7. import org.junit.Before;
  8. import org.junit.After;
  9. import org.junit.Test;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;
  12. import org.zlex.redis.support.ListOps;
  13. /**
  14. *
  15. * @author snowolf
  16. * @version 1.0
  17. * @since 1.0
  18. */
  19. public class ListOpsTest {
  20. private ApplicationContext app;
  21. private ListOps listOps;
  22. private String key = "queue";
  23. @Before
  24. public void before() throws Exception {
  25. app = new ClassPathXmlApplicationContext("applicationContext.xml");
  26. listOps = (ListOps) app.getBean("listOps");
  27. System.out.println("------------IN---------------");
  28. for (int i = 0; i < 5; i++) {
  29. String uid = "u" + i;
  30. System.out.println(uid);
  31. listOps.in(key, uid);
  32. }
  33. }
  34. @After
  35. public void after() {
  36. // ------------OUT---------------
  37. System.out.println("------------OUT---------------");
  38. long length = listOps.length(key);
  39. for (long i = 0; i < length; i++) {
  40. String uid = listOps.out(key);
  41. System.out.println(uid);
  42. }
  43. }
  44. @Test
  45. public void stack() {
  46. // ------------PUSH---------------
  47. String key = "stack";
  48. int len = 5;
  49. System.out.println("------------PUSH---------------");
  50. for (int i = 0; i < len; i++) {
  51. String uid = "u" + System.currentTimeMillis();
  52. System.out.println(uid);
  53. listOps.push(key, uid);
  54. }
  55. long length = listOps.length(key);
  56. assertEquals(len, length);
  57. // ------------POP---------------
  58. System.out.println("------------POP---------------");
  59. for (long i = 0; i < length; i++) {
  60. String uid = listOps.pop(key);
  61. System.out.println(uid);
  62. }
  63. }
  64. @Test
  65. public void index() {
  66. // -------------INDEX-------------
  67. String value = listOps.index(key, 3);
  68. assertEquals("u3", value);
  69. }
  70. @Test
  71. public void range() {
  72. // -------------RANGE-------------
  73. List<String> list = listOps.range(key, 3, 5);
  74. boolean result1 = list.contains("u3");
  75. assertEquals(true, result1);
  76. boolean result2 = list.contains("u1");
  77. assertEquals(false, result2);
  78. }
  79. @Test
  80. public void trim() {
  81. // ------------TRIM---------------
  82. List<String> list = listOps.range(key, 3, 5);
  83. listOps.trim(key, 3, 5);
  84. boolean result3 = list.contains("u1");
  85. assertEquals(false, result3);
  86. }
  87. @Test
  88. public void set() {
  89. // ------------SET-----------------
  90. List<String> list = listOps.range(key, 3, 5);
  91. listOps.set(key, 4, "ux4");
  92. boolean result4 = list.contains("u4");
  93. assertEquals(true, result4);
  94. }
  95. @Test
  96. public void remove() {
  97. // ------------REMOVE-----------------
  98. listOps.remove(key, 4, "u4");
  99. String value = listOps.index(key, 4);
  100. assertEquals(null, value);
  101. }
  102. }
/*** Mar 5, 2013*/
package org.zlex.redis;import static org.junit.Assert.*;import java.util.List;import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zlex.redis.support.ListOps;/*** * @author snowolf* @version 1.0* @since 1.0*/
public class ListOpsTest {private ApplicationContext app;private ListOps listOps;private String key = "queue";@Beforepublic void before() throws Exception {app = new ClassPathXmlApplicationContext("applicationContext.xml");listOps = (ListOps) app.getBean("listOps");System.out.println("------------IN---------------");for (int i = 0; i < 5; i++) {String uid = "u" + i;System.out.println(uid);listOps.in(key, uid);}}@Afterpublic void after() {// ------------OUT---------------System.out.println("------------OUT---------------");long length = listOps.length(key);for (long i = 0; i < length; i++) {String uid = listOps.out(key);System.out.println(uid);}}@Testpublic void stack() {// ------------PUSH---------------String key = "stack";int len = 5;System.out.println("------------PUSH---------------");for (int i = 0; i < len; i++) {String uid = "u" + System.currentTimeMillis();System.out.println(uid);listOps.push(key, uid);}long length = listOps.length(key);assertEquals(len, length);// ------------POP---------------System.out.println("------------POP---------------");for (long i = 0; i < length; i++) {String uid = listOps.pop(key);System.out.println(uid);}}@Testpublic void index() {// -------------INDEX-------------String value = listOps.index(key, 3);assertEquals("u3", value);}@Testpublic void range() {// -------------RANGE-------------List<String> list = listOps.range(key, 3, 5);boolean result1 = list.contains("u3");assertEquals(true, result1);boolean result2 = list.contains("u1");assertEquals(false, result2);}@Testpublic void trim() {// ------------TRIM---------------List<String> list = listOps.range(key, 3, 5);listOps.trim(key, 3, 5);boolean result3 = list.contains("u1");assertEquals(false, result3);}@Testpublic void set() {// ------------SET-----------------List<String> list = listOps.range(key, 3, 5);listOps.set(key, 4, "ux4");boolean result4 = list.contains("u4");assertEquals(true, result4);}@Testpublic void remove() {// ------------REMOVE-----------------listOps.remove(key, 4, "u4");String value = listOps.index(key, 4);assertEquals(null, value);}
}

回头继续整理,这个套路没错!

相关链接:

Redis实战

Redis实战之Redis + Jedis

Redis实战之征服 Redis + Jedis + Spring (一)

Redis实战之征服 Redis + Jedis + Spring (二)

Redis实战之征服 Redis + Jedis + Spring (三)

转载于:https://www.cnblogs.com/sand-tiny/p/3894930.html

Redis实战之征服 Redis + Jedis + Spring (三)相关推荐

  1. redis实战之使用redis实现排行榜

    转载:http://blog.csdn.net/u011250882/article/details/48632379 设想在一个游戏中,有上百万的玩家数据,如果现在需要你根据玩家的经验值整理一个前2 ...

  2. 【Redis实战】认识Redis中的全局哈希表

    文章目录 Redis是如何支持基于Key的快速访问的 全局哈希表 哈希表结构 哈希冲突 一张图 相关源码 Redis是如何支持基于Key的快速访问的 一谈到Redis,马上能想到的就是:"快 ...

  3. redis实战(12):Redis性能监控指标汇总

    1. 添加依赖 <parent><groupId>org.springframework.boot</groupId><artifactId>sprin ...

  4. Redis —— Redis In Action —— Redis 实战—— 基础篇 —— Redis 的常见命令解析 以及 Redis 结合客户端的快速上手使用—— SpringData Redis

  5. Redis实战之Redis + Jedis

    用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET 等.基于这些限制,有必要考虑Redis! 相关链接: Redis实战 Redis实战之Redi ...

  6. 2021-03-23 - 高性能 Redis 实战

    1.应用场景 主要用于学习Redis 底层实现,编程思想,以及可参考借鉴的技术, 同时在日常开发中如何高效实践高性能 Redis服务. 2.学习/操作 1.文档阅读 高性能 Redis 实战 | La ...

  7. redis实战教程_Redis详解新手教程_Redis入门到精通

    redis实战教程由一线互联网公司技术大牛亲自打造,结合真实项目实战演练,小白也能轻松入门,胜任企业岗位需求,薪资8K起!redis学习和使用,11年架构师指导学习,让你少走弯路,轻松学会! redi ...

  8. Redis 实战篇:GEO 助我邂逅附近女神

    码老湿,阅读了你的巧用数据类型实现亿级数据统计之后,我学会了如何游刃有余的使用不同的数据类型(String.Hash.List.Set.Sorted Set.HyperLogLog.Bitmap)去解 ...

  9. 征服 Redis + Jedis + Spring (一)—— 配置常规操作(GET SET DEL)

    有日子没写博客了,真的是忙得要疯掉. 完成项目基础架构搭建工作,解决了核心技术问题,接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: 征服 Redis 征服 Redis + J ...

最新文章

  1. 行情跌宕起伏,或许你可以买这个代币?
  2. python花数_Python数据分析实战,尾鸢花数据集数据分析
  3. 谈谈Activity如何启动的
  4. UNITY 多SCENE加载与编辑
  5. 股市永不过时的黄金铁律:零下金叉疯狂买,零上死叉立即卖
  6. 这是啥?也太秀了吧?
  7. JavaScript | 数组的常用属性和方法
  8. 懂得一些基本常识,就不会被《非酒精類致命飲料》或者叫做《我一辈子都不再喝可口可乐》的这篇文章所蒙蔽...
  9. 文字超过省略_纯CSS实现“文本溢出截断省略”的几种方法
  10. 获取设置套接字选项:getsockopt, setsockopt
  11. 使用dsoframer演示ppt
  12. 大厂面试必考的假设检验
  13. 基于RGMII的FPGA千兆以太网设计
  14. python re模块的(...),group(),groups()
  15. PGP Shredder的使用,加密解密
  16. 通州区机器人比赛活动总结_机器人大赛赛后总结
  17. SpringBoot自动装配的魔力
  18. oracle rac告警,oracle11.2.0.4 RAC 日志总有告警
  19. Birt报表开发工具及Birt runtime部署
  20. vagrant启动虚拟机Stderr: VBoxManage.exe: error: Call to WHvSetupPartition failed: ERROR_SUCCESS (Last=0xc

热门文章

  1. 如何把代码写的优雅一些
  2. python【数据结构与算法】表达式(前缀中缀后缀表达式)与Two fork tree
  3. Android移动开发之【Android实战项目】DAY10-App端耗流量场景问题及减少消耗办法
  4. Android之Pull解析XML
  5. mysql, 一对多查询, 统计一表数量
  6. linux生成ext4镜像,mkfs.ext4生成分区镜像自动把文件转成了sparse怎么破?
  7. 如何用python画爱心型线_python怎么画爱心
  8. 模拟海_浙江平湖“海水稻”丰收的背后,有这家来自上海企业的科技“秘方”...
  9. linux卸载netbeans,如何清除NetBeans中的缓存
  10. matlab模块 python,Matlab 和Python结合使用