一開始覺得Spring下操做哈希表,列表,真就是那麼土。恍惚間發現「stringRedisTemplate.opsForList()」的強大,抓緊時間惡補下。java
經過spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, LREM, LSET, LTRIM, RPOP, RPUSH命令。其實還有一些命令,當前版本不支持。不過,這些List的操做方法能夠實現隊列,堆棧的正常操做,足夠用了。web
相關連接:redis
Redis實戰 spring
Redis實戰之徵服 Redis + Jedis + Spring (一)app
Redis實戰之徵服 Redis + Jedis + Spring (二)測試
Redis實戰之徵服 Redis + Jedis + Spring (三)ui
爲了簡便操做,我使用了StringRedisTemplate。用字符串操做作展現。固然,你能夠繼續使用RedisTemplate。spa
閒言少敘,上代碼,一目瞭然:.net
/** * 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); } }
回頭繼續整理,這個套路沒錯!
相關連接:
Redis實戰之徵服 Redis + Jedis + Spring (一)