征服 Redis + Jedis

Memcached,對於緩存對象大小有要求,單個對象不得大於1MB,且不支持複雜的數據類型,譬如SETjava

等。基於這些限制,有必要考慮Redisredis

相關連接:spring

征服 Redis緩存

征服 Redis + Jedisapp

征服 Redis + Jedis + Spring (一)—— 配置&常規操做(GET SET DEL)分佈式

征服 Redis + Jedis + Spring (二)—— 哈希表操做(HMGET HMSET)ide

征服 Redis + Jedis + Spring (三)—— 列表操做工具

 

言歸正傳,目前Redis大概有3中基於Java語言的Client:spa

  • Jrediscode

  • Jedis

  • Redis4J

這裏只說Jedis,由於它是官方提供的惟一Redis Client For Java Provider!

 

1、簡單使用Jedis

須要Jedis就從Maven獲取吧!
Maven Pom.xml

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.1.0</version>
	<type>jar</type>
	<scope>compile</scope>
</dependency>

若是隻是簡單使用Jedis,如下這麼幾行代碼足夠:

Jedis jedis = new Jedis("10.11.20.140");
String keys = "name";

// 刪數據
jedis.del(keys);
// 存數據
jedis.set(keys, "snowolf");
// 取數據
String value = jedis.get(keys);
System.out.println(value);

2、池化使用Jedis

Jedis使用commons-pool完成池化實現。

先作個配置文件:

#最大分配的對象數
redis.pool.maxActive=1024
#最大可以保持idel狀態的對象數
redis.pool.maxIdle=200
#當池內沒有返回對象時,最大等待時間
redis.pool.maxWait=1000
#當調用borrow Object方法時,是否進行有效性檢查
redis.pool.testOnBorrow=true
#當調用return Object方法時,是否進行有效性檢查
redis.pool.testOnReturn=true
#IP
redis.ip=10.11.20.140
#Port
redis.port=6379

 在靜態代碼段中完成初始化:

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")));
	}

而後,修改前面那段jedis操做Redis

// 從池中獲取一個Jedis對象
		Jedis jedis = pool.getResource();
		String keys = "name";

		// 刪數據
		jedis.del(keys);
		// 存數據
		jedis.set(keys, "snowolf");
		// 取數據
		String value = jedis.get(keys);

		System.out.println(value);

		// 釋放對象池
		pool.returnResource(jedis);

改成從對象池中,獲取Jedis實例:

// 從池中獲取一個Jedis對象
Jedis jedis = pool.getResource();

切記,最後使用後,釋放Jedis對象:

// 釋放對象池
pool.returnResource(jedis);

3、一致性哈希

 

Memcached徹底基於分佈式集羣,而RedisMaster-Slave,若是想把Reids,作成集羣模式,無外乎多作幾套Master-Slave,每套Master-Slave完成各自的容災處理,經過Client工具,完成一致性哈希。

PS:Memcached是在Server端完成ShardingRedis只能依靠各個ClientSharding。可能會在Redis 3.0系列支持ServerSharding

 

保留前面的JedisPoolConfig,新增兩個Redis的IP(redis1.ip,redis2.ip),完成兩個JedisShardInfo實例,並將其丟進List中:

JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
				bundle.getString("redis1.ip"), Integer.valueOf(bundle						.getString("redis.port")));
JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
				bundle.getString("redis2.ip"), Integer.valueOf(bundle						.getString("redis.port")));

List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
list.add(jedisShardInfo1);
list.add(jedisShardInfo2);

初始化ShardedJedisPool代替JedisPool:

 

ShardedJedisPool pool = new ShardedJedisPool(config, list);

改由ShardedJedis,獲取Jedis對象:

// 從池中獲取一個Jedis對象
		ShardedJedis jedis = pool.getResource();
		String keys = "name";
		String value = "snowolf";
		// 刪數據
		jedis.del(keys);
		// 存數據
		jedis.set(keys, value);
		// 取數據
		String v = jedis.get(keys);

		System.out.println(v);

		// 釋放對象池
		pool.returnResource(jedis);

4、Spring封裝參考

 Ok,完成上述代碼足夠完成簡單任務,若是有必要,能夠用Spring封裝初始化:

<context:property-placeholder location="classpath:redis.properties" />
	<bean
		id="jedisPoolConfig"
		class="redis.clients.jedis.JedisPoolConfig"
	>
		<property
			name="maxActive"
			value="${redis.pool.maxActive}" />
		<property
			name="maxIdle"
			value="${redis.pool.maxIdle}" />
		<property
			name="maxWait"
			value="${redis.pool.maxWait}" />
		<property
			name="testOnBorrow"
			value="${redis.pool.testOnBorrow}" />
	</bean>
	<bean
		id="shardedJedisPool"
		class="redis.clients.jedis.ShardedJedisPool"
	>
		<constructor-arg
			index="0"
			ref="jedisPoolConfig" />
		<constructor-arg index="1">
			<list>
				<bean class="redis.clients.jedis.JedisShardInfo">
					<constructor-arg
						index="0"
						value="${redis1.ip}" />
					<constructor-arg
						index="1"
						value="${redis.port}"
						type="int" />
				</bean>
				<bean class="redis.clients.jedis.JedisShardInfo">
					<constructor-arg
						index="0"
						value="${redis2.ip}" />
					<constructor-arg
						index="1"
						value="${redis.port}"
						type="int" />
				</bean>
			</list>
		</constructor-arg>
	</bean>

代碼能夠更簡潔一些:

private ApplicationContext app;
	private ShardedJedisPool pool;

	@Before
	public void before() throws Exception {
		app = new ClassPathXmlApplicationContext("applicationContext.xml");
		pool = (ShardedJedisPool) app.getBean("shardedJedisPool");
	}

	@Test
	public void test() {

		// 從池中獲取一個Jedis對象
		ShardedJedis jedis = pool.getResource();
		String keys = "name";
		String value = "snowolf";
		// 刪數據
		jedis.del(keys);
		// 存數據
		jedis.set(keys, value);
		// 取數據
		String v = jedis.get(keys);

		System.out.println(v);

		// 釋放對象池
		pool.returnResource(jedis);

		assertEquals(value, v);
	}

固然,Spring提供了對於Redis的專門支持:spring-data-redis,之後有機會再深刻研究。



詳見附件!

 

相關連接:

征服 Redis

征服 Redis + Jedis

征服 Redis + Jedis + Spring (一)—— 配置&常規操做(GET SET DEL)

征服 Redis + Jedis + Spring (二)—— 哈希表操做(HMGET HMSET)

征服 Redis + Jedis + Spring (三)—— 列表操做

相關文章
相關標籤/搜索