Jedis - Map 본문

Programming

Jedis - Map

halatha 2011. 8. 17. 03:30
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