Jedis Client的使用以及序列化

 

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){

//需要还回给pool

this.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)));

}

}


 

 

你可能感兴趣的:(client)