一开始以为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 { @Autowired
private 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"; @Before
public 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);
}
} @After
public 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);
}
} @Test
public 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);
}
} @Test
public void index() { // -------------INDEX-------------
String value = listOps.index(key, 3);
assertEquals("u3", value);
} @Test
public 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);
} @Test
public void trim() {
// ------------TRIM---------------
List<String> list = listOps.range(key, 3, 5);
listOps.trim(key, 3, 5);
boolean result3 = list.contains("u1");
assertEquals(false, result3);
} @Test
public void set() {
// ------------SET-----------------
List<String> list = listOps.range(key, 3, 5);
listOps.set(key, 4, "ux4");
boolean result4 = list.contains("u4");
assertEquals(true, result4); } @Test
public 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 (三)

Redis实战之征服 Redis + Jedis + Spring (三)的更多相关文章

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

    Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: Redis实战 Re ...

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

    不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...

  3. Redis实战 | 5种Redis数据类型详解

    我们知道Redis是目前非常主流的KV数据库,它因高性能的读写能力而著称,其实还有另外一个优势,就是Redis提供了更加丰富的数据类型,这使得Redis有着更加广泛的使用场景.那Redis提供给用户的 ...

  4. Redis 实战 —— 13. 扩展 Redis

    简介 当数据量增大或者读写请求增多后,一台 Redis 服务器可能没办法再存储所有数据或者处理所有读写请求,那么就需要对 Redis 进行扩展,保证 Redis 在能存储所有数据对情况下,同时能正常处 ...

  5. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战

    笔记 4.Redis工具类封装讲解和实战     简介:高效开发方式 Redis工具类封装讲解和实战         1.常用客户端 https://redisdesktop.com/download ...

  6. Redis实战之Redis + Jedis

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

  7. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...

  8. Redis实战

    大约一年多前,公司同事开始使用Redis,不清楚是配置,还是版本的问题,当时的Redis经常在使用一段时间后,连接爆满且不释放.印象中,Redis 2.4.8以下的版本由于设计上的主从库同步问题,就会 ...

  9. Redis实战篇

    Redis实战篇 1 Redis 客户端 1.1 客户端通信 原理 客户端和服务器通过 TCP 连接来进行数据交互, 服务器默认的端口号为 6379 . 客户端和服务器发送的命令或数据一律以 \r\n ...

随机推荐

  1. innodb b+树

    http://www.admin10000.com/document/5372.html

  2. poj3252

    好了,我的数论渣爆了………… 首先[n,m]内的round number显然就是f[m]-f[n-1] 即问0~x内有多少round number: 设x的二进制位数为t: 首先很好分析出在这个范围 ...

  3. php简单实现MVC

    在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中.MVC足以应对大多数的情况,但还有一些情况是其不太适合的,如比较简单的个人博客,对于只有几百篇文章量级的博客,使用MVC让人觉得有些太复杂 ...

  4. webapp开发经验和资料

    开发经验: 开发资料: 1. http://xuui.net/librarys/webapps/webapp-development-of-commonly-used-code-snippets.ht ...

  5. 前端js框架收藏

    1. AngularJS :AngularJS是为克服HTML在构建应用上的不足而设计的. 2. knockout 3. avalon :MVVM是前端究极的解决方案,因此之后我大多数时间都在折腾av ...

  6. 枚举类型的单例模式(java)

    Inspired by Effective Java. Singleton模式是在编程实践中应用最广泛的几种设计模式之一.以前知道的,实现单例的方法有两种(下面的A.B).刚刚在读<Effect ...

  7. 0、IOS8:Xcode6 playground

    一.Playground介绍 Playground是Xcode6中自带的Swift代码开发环境.俗话说“功欲善其事,必先利其器”.以前在Xcode5中编写脚本代码,例如编写JS,其编写过程很痛苦,Xc ...

  8. rt-thread博客分享

    对于理解rtos, 国内有一个rt-thread的开源社区,里面讲解了一些rtos的很多概念,方便了理解很多问题点,博客地址如下: http://www.cnblogs.com/King-Gentle ...

  9. HTML5学习之FileReader接口

    http://blog.csdn.net/zk437092645/article/details/8745647 用来把文件读入内存,并且读取文件中的数据.FileReader接口提供了一个异步API ...

  10. Cocos2d-x中jsb结构剖析

    libs/javascript下有两部分bindings和spidermonkey.其中spidermonkey为js虚拟机,暂时不去管它.bindings下分为四部分,分别为主干部分,generat ...