Redis + Jedis + Spring (list操作)
2024-10-10 15:38:30
为了简便操作,我使用了StringRedisTemplate。用字符串操作做展示。当然,你可以继续使用RedisTemplate。
闲言少叙,上代码,一目了然:
- /**
- * 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替换。
下面进行测试用例,初始、结束时,分别做入队、出队操作,期间进行堆栈,队列操作。不用我细说了,看测试用例,很简单!
- /**
- * 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);
- }
- }
最新文章
- 台式机装原版Win2008R2
- Stanford NLP学习笔记:7. 情感分析(Sentiment)
- 深入浅出设计模式——命令模式(Command Pattern)
- ForeignKey 的第二个位置参数on_delete
- JSOI2015 Round1——完挂
- php/java bridge
- Java 集合 集合与数组之间的转换
- myeclipse 调试JSP页面
- web离线应用 Web SQL Database
- Nordic官网/Infocenter/Devzone/Github简介
- 使用免费 mongodb数据库 + 免费node.js服务器搭建小程序接口
- Beta冲刺(2/7)
- socket FTP-1
- C++和extern C
- visual studio 2017调试时闪退。
- node解决跨域问题
- where 1=1和 0=1 的作用
- WebSocket和Socket的区别
- [转]Tornado get/post请求异步处理框架分析
- Bzoj4548 小奇的糖果(链表+树状数组)
热门文章
- Vue + element-ui
- CSS介绍
- iOS 消息推送原理
- mysql学习之五:sql语句学习3
- Cuts the cake_hdu_2134.java
- 使用Win32::OLE操作Excel——Excel对象模型
- oendir(),readdir(),closedir() 打开/读取/关闭目录
- Android与JS混编(多图选择器)
- 读取并解析properties文件
- delete语句与reference约束 FK_subplan_job_id冲突问题,导致job无法删除解决办法