设为首页 收藏本站
查看: 1063|回复: 0

[经验分享] Redis实战之征服 Redis + Jedis + Spring (三)

[复制链接]

尚未签到

发表于 2015-7-22 03:18:35 | 显示全部楼层 |阅读模式
  一开始以为Spring下操作哈希表,列表,真就是那么土。恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下。
  通过spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令。其实还有一些命令,当前版本不支持。 DSC0000.gif 不过,这些List的操作方法可以实现队列,堆栈的正常操作,足够用了。
  
  相关链接:
  Redis实战
  Redis实战之Redis + Jedis
  Redis实战之征服 Redis + Jedis + Spring (一)
  Redis实战之征服 Redis + Jedis + Spring (二)
  Redis实战之征服 Redis + Jedis + Spring (三)
  
  为了简便操作,我使用了StringRedisTemplate。用字符串操作做展示。当然,你可以继续使用RedisTemplate。
  闲言少叙,上代码,一目了然:
  






Java代码    DSC0001.gif

  • /**
  • * 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 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);  
  •     }  
  • }  

/**
* 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 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替换。
  
  下面进行测试用例,初始、结束时,分别做入队、出队操作,期间进行堆栈,队列操作。不用我细说了,看测试用例,很简单!
  






Java代码   

  • /**
  • * 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 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 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 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);  
  •   
  •     }  
  • }  

/**
* 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 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 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 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 (三)
  
  
  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-89174-1-1.html 上篇帖子: Redis的安装 下篇帖子: 【redis源码】(九)Redis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表