Created
July 25, 2012 02:06
-
-
Save charleehu/3173941 to your computer and use it in GitHub Desktop.
两种使用jedis的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationHandler; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Proxy; | |
import java.util.concurrent.TimeoutException; | |
import redis.clients.jedis.Jedis; | |
import redis.clients.jedis.JedisCommands; | |
import redis.clients.jedis.JedisPool; | |
import redis.clients.jedis.exceptions.JedisException; | |
public class JedisProxy implements InvocationHandler { | |
private final JedisPool jedisPool; | |
public JedisProxy(JedisPool pool) { | |
this.jedisPool = pool; | |
} | |
public static JedisCommands newInstance(JedisPool pool) { | |
return (JedisCommands) Proxy.newProxyInstance(Jedis.class.getClassLoader(), Jedis.class.getInterfaces(), new JedisProxy(pool)); | |
} | |
@Override | |
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { | |
Object result; | |
Jedis jedis = obtainJedis(); | |
try { | |
result = m.invoke(jedis, args); | |
} catch (InvocationTargetException e) { | |
throw e.getTargetException(); | |
} catch (Exception e) { | |
throw new JedisException("Unexpected proxy invocation exception: " + e.getMessage(), e); | |
} finally { | |
returnJedis(jedis); | |
} | |
return result; | |
} | |
private Jedis obtainJedis() { | |
Jedis jedis; | |
jedis = jedisPool.getResource(); | |
return jedis; | |
} | |
private void returnJedis(Jedis jedis) { | |
try { | |
if (jedis.isConnected()) { | |
jedis.ping(); | |
jedisPool.returnResource(jedis); | |
} else { | |
jedisPool.returnBrokenResource(jedis); | |
} | |
} catch (JedisException e) { | |
jedisPool.returnBrokenResource(jedis); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* $Id: RedisUtil.java 106 2012-07-24 07:47:20Z xiaowei.hu $ | |
* Copyright 2012-2014 Oak Pacific Interactive. All rights reserved. | |
*/ | |
package com.renren.socialgame.kp.support.redis; | |
import redis.clients.jedis.JedisCommands; | |
import redis.clients.jedis.ShardedJedis; | |
import com.renren.socialgame.kp.app.AppConfig; | |
/** | |
* @author <a href="mailto:xiaowei.hu@renren-inc.com">Xiaowei Hu</a> | |
* @version 1.0 2012-7-19 下午05:26:07 | |
* @since 1.0 | |
*/ | |
public class RedisUtil { | |
private static final RedisPool redisPool = AppConfig.getRedisPool(); | |
private static final ThreadLocal<ShardedJedis> shardedJedisTL = new ThreadLocal<ShardedJedis>(); | |
public static JedisCommands getConnection() { | |
ShardedJedis jedis = shardedJedisTL.get(); | |
if (jedis == null) { | |
jedis = redisPool.getResource(); | |
shardedJedisTL.set(jedis); | |
} | |
return jedis; | |
} | |
public static void closeConnection() { | |
ShardedJedis jedis = shardedJedisTL.get(); | |
if (jedis != null) { | |
redisPool.returnResource(jedis); | |
} | |
shardedJedisTL.remove(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment