一、使用Maven导入Jedis的相关jar包。
?
?
class="xml" name="code"><dependencies>
    <dependency>
	    <groupId>redis.clients</groupId>
	    <artifactId>jedis</artifactId>
	    <version>2.2.0</version>
	    <type>jar</type>
	    <scope>compile</scope>
    </dependency>
  </dependencies>
?
?
二、配置Redis的pool以从pool中获取redis的对象:
#最大分配的对象数 redis.pool.maxActive=1024 #最大能够保持idel状态的对象数 redis.pool.maxIdle=200 #当池内没有返回对象时,最大等待时间 redis.pool.maxWait=1000 #当调用borrow Object方法时,是否进行有效性检查 redis.pool.testOnBorrow=false #当调用return Object方法时,是否进行有效性检查 redis.pool.testOnReturn=true #IP redis.ip=192.168.1.155 #Port redis.port=6379
?
三、相关的Java代码:
1、创建一个Java类用于管理Redis的Pool以产生Redis对象
package com.chuanliu.platform.activity.cache;
import java.util.ResourceBundle;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * Get the Redis Object from the Pool,
 * Redis using commons-pool to manage its own pool
 * 
 * @author Josh Wang(Sheng)
 *
 * @email  josh_wang23@hotmail.com
 *
 */
public class RedisPoolManager {
	private static JedisPool pool;
	
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) 
			throw new IllegalArgumentException("[redis.properties] is not found");
		
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
		config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
		config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
		
		pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
	}
	
	/**
	 * Get Jedis resource from the pool
	 * @return
	 */
	public static Jedis createInstance() {
		Jedis jedis = pool.getResource();
		jedis.auth("diandi");
		return jedis;
	}
	
	/**
	 * Return the resource to pool
	 * @param jedis
	 */
	public static void returnResource(Jedis jedis) {
		pool.returnResource(jedis);
	}
}
?
2. 定义一个Java类用于使用获取到的Redis对象往内存中进行CRUD基本操作。
package com.chuanliu.platform.activity.cache;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.sun.jersey.spi.resource.Singleton;
import redis.clients.jedis.Jedis;
/**
 * @author Josh Wang(Sheng)
 *
 * @email  josh_wang23@hotmail.com
 *
 */
@Singleton
@Component("cacheManager")
public class CacheManager {
	private Jedis jedis = RedisPoolManager.createInstance();
	
	public void set(Map<String, String> entries) {
		for (Map.Entry<String, String> entry : entries.entrySet()) {
			jedis.set(entry.getKey(), entry.getValue());
		}
	}
	
	public void set(String key, String value) {
		jedis.set(key, value);
	}
	
	/**
	 * Set the value to the key and specify the key's life cycle as seconds.
	 * @param key
	 * @param live
	 * @param value
	 */
	public void setKeyLive(String key, int live, String value) {
		jedis.setex(key, live, value);
	}
	
	/**
	 * Append the value to an existing key
	 * @param key
	 * @param value
	 */
	public void append(String key, String value) {
		jedis.append(key, value);
	}
	
	public String getValue(String key) {
		return jedis.get(key);
	}
	
	public List<String> getValues(String... keys) {
		return jedis.mget(keys);
	}
	
	public Long deleteValue(String key) {
		return jedis.del(key);
	}
	
	public Long deleteValues(String... keys) {
		return jedis.del(keys);
	}
	
	public void returnSource() {
		RedisPoolManager.returnResource(jedis);
	}
	
	public long calculateSize() {
		return jedis.dbSize();
	}
}
?
3. 创建相关的Unit Test类进行测试
package com.chuanliu.platform.activity.cache;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import com.chuanliu.platform.activity.basic.test.SpringBaseTest;
/**
 * @author Josh Wang(Sheng)
 *
 * @email  josh_wang23@hotmail.com
 */
public class TestCacheManager extends SpringBaseTest {
	
	private @Resource CacheManager cacheManager;
	
	@Before
	public void init() {
		printHighlight(cacheManager.hashCode() + "");
	}
	
	@Test
	public void set() {
		Map<String, String> kValues = new HashMap<String, String>();
		kValues.put("swang6", "WangSheng");
		kValues.put("23", "MJ");
		kValues.put("34", "O'Nel");
		kValues.put("10", "meixi");
		kValues.put("li", "li");
		cacheManager.append("li", "yun");
		
		cacheManager.set(kValues);
		printHighlight(cacheManager.getValue("li"));
		
	}
	
	@Test
	public void calculateSize() {
		printHighlight(cacheManager.getValue("foo"));
		printHighlight(cacheManager.calculateSize() + "");
	}
	
	@Test
	public void getValue() {
		String value = cacheManager.getValue("swang6");
		Assert.assertNotNull(value);
		Assert.assertEquals("WangSheng", value);
	}
}
?
这一篇讲了基本的Jedis的使用,下一篇讲接着讲解Jedis的高级使用,即Sharding。
?
?
?
?
 相关文章
                            相关文章