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

相关链接:

征服 Redis

征服 Redis + Jedis

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

征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)

征服 Redis + Jedis + Spring (三)—— 列表操作

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

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

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

详见附件!

相关链接:

征服 Redis

征服 Redis + Jedis

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

征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET)

征服 Redis + Jedis + Spring (三)—— 列表操作

征服 Redis + Jedis + Spring (三)—— 列表操作【转】的更多相关文章

  1. 征服 Redis + Jedis + Spring —— 配置&常规操作

    Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spri ...

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

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

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

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

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

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

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

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

  6. Redis + Jedis + Spring (list操作)

    为了简便操作,我使用了StringRedisTemplate.用字符串操作做展示.当然,你可以继续使用RedisTemplate. 闲言少叙,上代码,一目了然: /** * Mar 5, 2013 * ...

  7. Redis + Jedis + Spring 实例(对象的操作)

        目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上 ...

  8. redis jedis存储对象简单操作,map list 自定义对象

    安装好redis,进行了基本的操作.包括对map list 和自定义对象的基本操作.笔记都在代码注释里,直接上代码. private Jedis jedis; @Before public void ...

  9. Redis + Jedis + Spring整合遇到的异常(转)

    项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: Caused b ...

随机推荐

  1. 理解和熟练运用js中的call及apply

    call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...

  2. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...

  3. Codeforces Round #197 (Div. 2) : A

    水题一个: 直接贴代码: #include<cstdio> #include<algorithm> #include<cstring> using namespac ...

  4. C语言中的volatile

    转自C语言的那些小秘密之volatile volatile的重要性对于搞嵌入式的程序员来说是不言而喻的,对于volatile的了解程度常常被不少公司在招聘嵌入式编程人员面试的时候作为衡量一个应聘者是否 ...

  5. 一篇文章让你读懂 OpenStack 的起源、架构和应用

    OpenStack 是一个面向 IaaS 层的开源项目,用于实现公有云和私有云的部署及管理.拥有众多大公司的行业背书和数以千计的社区成员, OpenStack 被看作是云计算的未来.目前 OS 基金会 ...

  6. Ubuntu 安装基础教程

    转自:http://teliute.org/linux/Ubsetup/index.html 1.进入 live cd 桌面  1)设置好启动后,断开网络,然后重启动计算机,可以用硬盘启动,也可以刻成 ...

  7. Web Development Terms

    I've come across lots of terms while learning web development. I'm feeling myself overwhelmed. Here ...

  8. 安卓高手之路之java层Binder

    很多人一提到Binder就说代理模式,人云亦云的多,能理解精髓的少. 本篇文章就从设计角度分析一下java层BInder的设计目标,以及设计思路,设计缺陷,从而驾驭它. 对于[邦德儿]的理解, 从通信 ...

  9. VM Depot 喜迎中国本土开源镜像!

     发布于 2014-04-07 作者 陈 忠岳 VM Depot 登陆中国之际,我非常高兴地告诉大家,一批各位耳熟能详的中国本地开源镜像已同时上线!得益于开源社区的大力支持,Ubuntu 麒麟13 ...

  10. 【转】android-support-v7-appcompat.jar 的安装及相关问题解决 --- 汇总整理

    原文网址:http://tdppro.blog.51cto.com/749956/1388853 1.DownLoading the Support Libraries 1)Start the And ...