Redis + Jedis + Spring (list操作)

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

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

Java代码  

  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替换。

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

Java代码  

  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. }
时间: 2024-08-05 19:33:08

Redis + Jedis + Spring (list操作)的相关文章

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

一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Redis + Jedis 征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL) 征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET) 征服 Redis + Jedis + Spring (三)—— 列表

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

有日子没写博客了,真的是忙得要疯掉. 完成项目基础架构搭建工作,解决了核心技术问题,接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: 征服 Redis 征服 Redis + Jedis 征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL) 征服 Redis + Jedis + Spring (二)—— 哈希表操作(HMGET HMSET) 征服 Redis + Jedis + Spring (三)—— 列表操作 前文有述

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

不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实战之Redis + Jedis Redis实战之征服 Redis + Jedis + Spring (一) Redis实战之征服 Redis + Jedis + Spring (二) Redis实战之征服 Redis + Jedis + Spring (三) 一.预期 接上一篇,扩充User属性:

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

一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 通过spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令.其实还有一些命令,当前版本不支持.不过,这些List的操作方法可以实现队列,堆栈的正常操作,足够用了. 相关链接: Redis实战 Redis实战之Redis +

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

相关链接: Redis实战 Redis实战之Redis + Jedis Redis实战之征服 Redis + Jedis + Spring (一) Redis实战之征服 Redis + Jedis + Spring (二) Redis实战之征服 Redis + Jedis + Spring (三) 前文有述,Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb

征服 Redis + Jedis + Spring —— 配置&amp;常规操作

Spring提供了对于Redis的专门支持:spring-data-redis.此外,类似的还有: 我想大部分人对spring-data-hadoop.spring-data-mongodb.spring-data-redis以及spring-data-jpa表示关注. 一.简述 spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis自然是针对Redis的独立封装了. 当前版本1.0.1,主要是将jedis.jredis.rjc以及srp等Redi

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

目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上一篇,扩充User属性: Java代码   public class User implements Serializable { private static final long serialVersionUID = -1267719235225203410L; private String uid;

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

项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [spring.xml]:

Redis实战之Redis + Jedis[转]

http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报 目录(?)[-] 一简单使用Jedis 二池化使用Jedis 三一致性哈希 四Spring封装参考 用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET 等.基于这些限制,有必要考虑Redis! 相关链接: Redis实战 Redis实战之Redis + Jedis R