Created
September 4, 2012 01:43
-
-
Save allwefantasy/3615650 to your computer and use it in GitHub Desktop.
jedis example
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
public class RedisClient { | |
private JedisPool pool; | |
@Inject | |
public RedisClient(Settings settings) { | |
try { | |
pool = new JedisPool(new JedisPoolConfig(), settings.get("redis.host"), settings.getAsInt("redis.port", 6379)); | |
} catch (SettingsException e) { | |
// ignore | |
} | |
} | |
public interface Callback { | |
public void execute(Jedis jedis); | |
} | |
public void operate(Callback callback) { | |
Jedis jedis = borrow(); | |
try { | |
callback.execute(jedis); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String get(String key) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.get(key); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String bGet(String key) { | |
Jedis jedis = borrow(); | |
try { | |
byte[] value = jedis.get(key.getBytes()); | |
if (value != null) return GZip.decodeWithGZip(value); | |
return null; | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String set(String key, String value) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.set(key, value); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public void del(String key) { | |
Jedis jedis = borrow(); | |
try { | |
jedis.del(key); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String bSet(String key, String value) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.set(key.getBytes(), GZip.encodeWithGZip(value)); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public byte[] bGetJ(String key) { | |
Jedis jedis = borrow(); | |
try { | |
byte[] value = jedis.get(key.getBytes()); | |
if (value != null) return value; | |
return null; | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String bSetJ(String key, byte[] value) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.set(key.getBytes(), value); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public boolean exits(String key) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.exists(key); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public List<String> mget(String[] keys) { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.mget(keys); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public String info() { | |
Jedis jedis = borrow(); | |
try { | |
return jedis.info(); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public List<String> bMget(String[] keys) { | |
Jedis jedis = borrow(); | |
int len = keys.length; | |
byte[][] bkeys = new byte[len][]; | |
for (int i = 0; i < keys.length; i++) { | |
bkeys[i] = keys[i].getBytes(); | |
} | |
try { | |
List<byte[]> list = jedis.mget(bkeys); | |
List<String> temp_list = new ArrayList<String>(list.size()); | |
for (byte[] temp : list) { | |
temp_list.add(GZip.decodeWithGZip(temp)); | |
} | |
return temp_list; | |
} finally { | |
revert(jedis); | |
} | |
} | |
public Set<String> sCopy(String key, String new_key) { | |
Jedis jedis = borrow(); | |
try { | |
Set<String> oldSets = jedis.smembers(key); | |
for (String str : oldSets) { | |
jedis.sadd(new_key, str); | |
} | |
return oldSets; | |
} finally { | |
revert(jedis); | |
} | |
} | |
public void sClear(String key, String oldKey) { | |
Jedis jedis = borrow(); | |
try { | |
Set<String> oldSets = jedis.smembers(key); | |
for (String str : oldSets) { | |
jedis.del(oldKey + ":" + str); | |
} | |
jedis.del(key); | |
} finally { | |
revert(jedis); | |
} | |
} | |
public Set<String> sMove(String key, String new_key) { | |
Jedis jedis = borrow(); | |
try { | |
Set<String> oldSets = jedis.smembers(key); | |
for (String str : oldSets) { | |
jedis.smove(key, new_key, str); | |
} | |
return oldSets; | |
} finally { | |
revert(jedis); | |
} | |
} | |
public void destory() { | |
pool.destroy(); | |
} | |
public Jedis borrow() { | |
return pool.getResource(); | |
} | |
public void revert(Jedis jedis) { | |
pool.returnResource(jedis); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment