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
- Malaysia
- program
- Java
- Book
- Spain
- erlang
- Kuala Lumpur
- web
- France
- management
- MySQL
- ubuntu
- django
- Python
- essay
- QT
- leadership
- hadoop
- programming_book
- hbase
- Book review
- Linux
- agile
- Programming
- psychology
- history
- comic agile
- RFID
- Software Engineering
- Italy
Archives
- Today
- Total
Jedis basic operation 본문
* src directory: main/java/redis/clients/jedis/Jedis.java
2011/08/11 - [Programming] - Install Jedis on Ubuntu 10.04
2011/08/11 - [Programming] - Install Jedis on Ubuntu 10.04
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 TestJedis { public static void main(String[] args) { JedisPool pool = new JedisPool(new JedisPoolConfig(), "flatbox"); Jedis jedis = pool.getResource(); try { // list for ( int i = 0; i < 6; ++i ) { final String key = "foo" + i; if ( jedis.exists(key) ) jedis.del(key); if ( i % 2 == 0 ) { jedis.set(key, "bar" + i); if ( i % 4 == 0 ) jedis.set(key, "bar_new" + i); } } for ( int i = 0; i < 6; ++i ) { final String key = "foo" + i; if ( jedis.exists(key) ) System.out.println("jedis.get(" + key + ") = " + jedis.get(key)); } // set final String[] sets = { "set1", "set2", "set3" }; for ( final String set : sets ) { for ( Iterator<String> memberItr = jedis.smembers(set).iterator(); memberItr.hasNext(); ) { final String member = memberItr.next(); if ( null != jedis.sismember(set, member) ) { //System.out.println("Delete (" + set + ", " + member + ")"); jedis.srem(set, member); } } for ( int i = 0; i < 3; ++i ) { jedis.sadd(set, "val" + i); } } // sorted set final String[] sortedSets = { "sortedSet1", "sortedSet2", "sortedSet3" }; for ( final String sortedSet : sortedSets ) { for ( Iterator<String> memberItr = jedis.zrange(sortedSet, 0, -1).iterator(); memberItr.hasNext(); ) { final String member = memberItr.next(); if ( null != jedis.zrank(sortedSet, member) ) { //System.out.println("Delete (" + sortedSet + ", " + member + ")"); jedis.zrem(sortedSet, member); } } for ( int i = 0; i < 3; ++i ) { jedis.zadd(sortedSet, i, "val" + i); } } // map final String[] maps = { "hash1", "hash2", "hash3" }; for ( final String map : maps ) { for ( Iterator<String> keyItr = jedis.hkeys(map).iterator(); keyItr.hasNext(); ) { final String key = keyItr.next(); if ( null != jedis.hget(map, key) ) { //System.out.println("Delete (" + map + ", " + key + ")"); jedis.hdel(map, key); } } for ( int i = 0; i < 3; ++i ) { jedis.hset(map, "key" + i, "val" + i); } } } 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