更新時間:2022-05-07 09:28:09 來源:動力節(jié)點 瀏覽1412次
截止到目前為止,在redis教程的文檔和實現(xiàn)里面并沒有針對object對象緩存的方法,然而,在我們的實際開發(fā)需要中,在很多時候我們是需要進行對象緩存的,并且可以正確的讀取出來!
jedis.set(byte[], byte[])
看這個方法,是進行字節(jié)碼操作的,這讓我們很容易想到在一些遠程方法調用中,我們傳遞對象同樣傳遞的是字節(jié)碼,是不是可以參考呢?
首先,既然需要對對象進行字節(jié)操作,即可寫和可讀的操作,為了保證這個原則,那么緩存對象需要實現(xiàn)Serializable 接口,進行序列化和反序列化!
1.Serializable (接口,實現(xiàn)此接口的對象可以進行序列化)
2.ByteArrayOutputStream,ObjectOutputStream 對象轉換為字節(jié)碼輸出流
3.ByteArrayInputStream ,ObjectInputStream 字節(jié)碼轉換為對象的輸入流
了解了如上三點知識后,我們就可以對對象進行緩存操作了!
示例代碼如下,包括了對象緩存和List對象數(shù)組緩存,需要聲明的是,放入list數(shù)組中的對象同樣需要實現(xiàn)Serializable 接口:
public class ObjectsTranscoder extends SerializeTranscoder {
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(Object value) {
if (value == null) {
throw new NullPointerException("Can't serialize null");
}
byte[] result = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
os.writeObject(value);
os.close();
bos.close();
result = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public Object deserialize(byte[] in) {
Object result = null;
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
result = is.readObject();
is.close();
bis.close();
}
} catch (IOException e) {
logger.error(String.format("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
logger.error(String.format("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} finally {
close(is);
close(bis);
}
return result;
}
}
對象的轉換示例如上代碼:
數(shù)組緩存代碼:
public class ListTranscoder<M extends Serializable> extends SerializeTranscoder {
@SuppressWarnings("unchecked")
public List<M> deserialize(byte[] in) {
List<M> list = new ArrayList<>();
ByteArrayInputStream bis = null;
ObjectInputStream is = null;
try {
if (in != null) {
bis = new ByteArrayInputStream(in);
is = new ObjectInputStream(bis);
while (true) {
M m = (M)is.readObject();
if (m == null) {
break;
}
list.add(m);
}
is.close();
bis.close();
}
} catch (IOException e) {
logger.error(String.format("Caught IOException decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} catch (ClassNotFoundException e) {
logger.error(String.format("Caught CNFE decoding %d bytes of data",
in == null ? 0 : in.length) + e);
} finally {
close(is);
close(bis);
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public byte[] serialize(Object value) {
if (value == null)
throw new NullPointerException("Can't serialize null");
List<M> values = (List<M>) value;
byte[] results = null;
ByteArrayOutputStream bos = null;
ObjectOutputStream os = null;
try {
bos = new ByteArrayOutputStream();
os = new ObjectOutputStream(bos);
for (M m : values) {
os.writeObject(m);
}
// os.writeObject(null);
os.close();
bos.close();
results = bos.toByteArray();
} catch (IOException e) {
throw new IllegalArgumentException("Non-serializable object", e);
} finally {
close(os);
close(bos);
}
return results;
}
}
通過以上操作即可以實現(xiàn)對象的緩存和讀取了!如果大家想了解更多相關知識,不妨來關注一下動力節(jié)點的Java在線學習,里面的課程內(nèi)容從入門到精通,細致全面,很適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助哦。