JedisPool pool = new JedisPool(poolConfig, IP, PORT, timeout);public String set(String key,String value) {Jedis jedis = null;boolean success = true;try {jedis = this.pool.getResource();return jedis.set(key, value);}catch (JedisException e) {success = false;if(jedis != null){//jedis异常,销毁pool.returnBrokenResource(jedis);}throw e;}catch (Exception e) {system.out.println("jedis exception");}finally{if(success && jedis != null){//需要还回给poolthis.pool.returnResource(jedis);}}}
------------------------jedis序列化RedisSerializeUtil --------------
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class RedisSerializeUtil {//序列化public static byte[] serialize(Object object){ObjectOutputStream objectOutputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { byteArrayOutputStream = new ByteArrayOutputStream(); objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(object); byte[] bytes = byteArrayOutputStream.toByteArray(); return bytes;} catch (Exception e) {e.printStackTrace();}return null;}// 反序列化public static Object deSeialize(byte[] bytes) {ByteArrayInputStream byteArrayOutputStream = null;try {byteArrayOutputStream = new ByteArrayInputStream(bytes);ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayOutputStream);return objectInputStream.readObject();} catch (Exception e) {system.out.println("deserialize exception");}return null;}public static void main(String[] args) {String str = "tobytes";System.out.print(RedisSerializeUtil.deSeialize(RedisSerializeUtil.serialize(str)));}}