Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Italy
- France
- Python
- Book review
- QT
- Kuala Lumpur
- programming_book
- Malaysia
- MySQL
- web
- Programming
- django
- management
- Software Engineering
- hadoop
- comic agile
- program
- Spain
- leadership
- Java
- erlang
- history
- RFID
- Linux
- psychology
- hbase
- essay
- Book
- ubuntu
- agile
Archives
- Today
- Total
Jedis - list 본문
import java.util.Set; import java.util.Iterator; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class TestJedisList { public static void main(String[] args) { final int ELEMENTS_NUM = 5; JedisPool pool = new JedisPool(new JedisPoolConfig(), "flatbox"); Jedis jedis = pool.getResource(); try { // list final String srcKey = "list"; if ( 0 < jedis.llen(srcKey) ) jedis.del(srcKey); // put item at the right end of list for ( int i = 0; i < ELEMENTS_NUM; ++i ) jedis.rpush(srcKey, "item" + i); // put item at the left end of list for ( int i = -ELEMENTS_NUM; i < 0; ++i ) jedis.lpush(srcKey, "item" + i); // lrange(key, 0, -1) returns all the items System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); // lrange(key, start, end) returns items from start idx to end idx System.out.println("jedis.lrange(" + srcKey + ", 0, 3) = " + jedis.lrange(srcKey, 0, 3)); // llen returns list length System.out.println("jedis.llen(" + srcKey + ") = " + jedis.llen(srcKey)); // ltrim(key, start, end) trims items from start idx to end idx System.out.println("jedis.ltrim(" + srcKey + ", 5, 9) = " + jedis.ltrim(srcKey, 5, 9)); System.out.println("jedis.rpush(" + srcKey + ", \"item0\") = " + jedis.rpush(srcKey, "item0")); System.out.println("jedis.rpush(" + srcKey + ", \"item0\") = " + jedis.rpush(srcKey, "item0")); System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); System.out.println("jedis.llen(" + srcKey + ") = " + jedis.llen(srcKey)); // lrem(key, count, item) removes item count times // and returns how many times item removed System.out.println("jedis.lrem(" + srcKey + ", 3, item0) = " + jedis.lrem(srcKey, 3, "item0")); System.out.println("jedis.lrem(" + srcKey + ", 1, item-0) = " + jedis.lrem(srcKey, 1, "item-0")); System.out.println("jedis.llen(" + srcKey + ") = " + jedis.llen(srcKey)); // lpop removes and returns the left most item System.out.println("jedis.lpop(" + srcKey + ") = " + jedis.lpop(srcKey)); // rpop removes and returns the right most item System.out.println("jedis.rpop(" + srcKey + ") = " + jedis.rpop(srcKey)); System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); System.out.println("jedis.lpush(" + srcKey + ", \"item0\") = " + jedis.lpush(srcKey, "item0")); System.out.println("jedis.rpush(" + srcKey + ", \"item4\") = " + jedis.rpush(srcKey, "item4")); System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); // lindex(key, index) returns list[index] member // it works even if index is negative (-1 means the last member) System.out.println("jedis.lindex(" + srcKey + ", 2) = " + jedis.lindex(srcKey, 2)); System.out.println("jedis.lindex(" + srcKey + ", -2) = " + jedis.lindex(srcKey, -2)); // lset(key, index, value) sets list[index] member as value System.out.println("jedis.lset(" + srcKey + ", 1, \"new\") = " + jedis.lset(srcKey, 1, "new")); System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); final String dstKey = "dstList"; if ( 0 < jedis.llen(dstKey) ) jedis.del(dstKey); System.out.println("jedis.lrange(" + dstKey + ", 0, -1) = " + jedis.lrange(dstKey, 0, -1)); // rpoplpush(srcKey, dstKey) is // combination of rpop(srcKey) and lpush(dstKey) System.out.println("jedis.rpoplpush(" + srcKey + ", " + dstKey + ") = " + jedis.rpoplpush(srcKey, dstKey)); System.out.println("jedis.rpoplpush(" + srcKey + ", " + dstKey + ") = " + jedis.rpoplpush(srcKey, dstKey)); System.out.println("jedis.lrange(" + srcKey + ", 0, -1) = " + jedis.lrange(srcKey, 0, -1)); System.out.println("jedis.lrange(" + dstKey + ", 0, -1) = " + jedis.lrange(dstKey, 0, -1)); } finally { /// ... it's important to return the Jedis instance to the pool //once you've finished using it pool.returnResource(jedis); } /// ... when closing your application: pool.destroy(); } }
Comments