Redis實戰之徵服 Redis + Jedis + Spring (三)
阿新 • • 發佈:2019-01-27
一開始以為Spring下操作雜湊表,列表,真就是那麼土。恍惚間發現“stringRedisTemplate.opsForList()”的強大,抓緊時間惡補下。
通過spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令。其實還有一些命令,當前版本不支援。不過,這些List的操作方法可以實現佇列,堆疊的正常操作,足夠用了。
相關連結:
為了簡便操作,我使用了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")
- publicclass 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
- */
- publicvoid 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
- */
- publicvoid set(String key, long index, String value) {
- stringRedisTemplate.opsForList().set(key, index, value);
- }
- /**
- * 裁剪
- *
- * @param key
- * @param start
- * @param end
- */
- publicvoid 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<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替換。
下面進行測試用例,初始、結束時,分別做入隊、出隊操作,期間進行堆疊,佇列操作。不用我細說了,看測試用例,很簡單!
Java程式碼- /**
- * Mar 5, 2013
- */
- package org.zlex.redis;
- importstatic 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
- */
- publicclass ListOpsTest {
- private ApplicationContext app;
- private ListOps listOps;
- private String key = "queue";
- @Before
- publicvoid 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
- publicvoid 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
- publicvoid 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
- publicvoid index() {
- // -------------INDEX-------------
- String value = listOps.index(key, 3);
- assertEquals("u3", value);
- }
- @Test
- publicvoid 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
- publicvoid trim() {
- // ------------TRIM---------------
- List<String> list = listOps.range(key, 3, 5);
- listOps.trim(key, 3, 5);
- boolean result3 = list.contains("u1");
- assertEquals(false, result3);
- }
- @Test
- publicvoid set() {
- // ------------SET-----------------
- List<String> list = listOps.range(key, 3, 5);
- listOps.set(key, 4, "ux4");
- boolean result4 = list.contains("u4");
- assertEquals(true, result4);
- }
- @Test
- publicvoid 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<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);
}
}
回頭繼續整理,這個套路沒錯!
相關連結: