为了简便操作,我使用了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. }

这里说明下,例如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. }

最新文章

  1. 台式机装原版Win2008R2
  2. Stanford NLP学习笔记:7. 情感分析(Sentiment)
  3. 深入浅出设计模式——命令模式(Command Pattern)
  4. ForeignKey 的第二个位置参数on_delete
  5. JSOI2015 Round1——完挂
  6. php/java bridge
  7. Java 集合 集合与数组之间的转换
  8. myeclipse 调试JSP页面
  9. web离线应用 Web SQL Database
  10. Nordic官网/Infocenter/Devzone/Github简介
  11. 使用免费 mongodb数据库 + 免费node.js服务器搭建小程序接口
  12. Beta冲刺(2/7)
  13. socket FTP-1
  14. C++和extern C
  15. visual studio 2017调试时闪退。
  16. node解决跨域问题
  17. where 1=1和 0=1 的作用
  18. WebSocket和Socket的区别
  19. [转]Tornado get/post请求异步处理框架分析
  20. Bzoj4548 小奇的糖果(链表+树状数组)

热门文章

  1. Vue + element-ui
  2. CSS介绍
  3. iOS 消息推送原理
  4. mysql学习之五:sql语句学习3
  5. Cuts the cake_hdu_2134.java
  6. 使用Win32::OLE操作Excel——Excel对象模型
  7. oendir(),readdir(),closedir() 打开/读取/关闭目录
  8. Android与JS混编(多图选择器)
  9. 读取并解析properties文件
  10. delete语句与reference约束 FK_subplan_job_id冲突问题,导致job无法删除解决办法