更新時間:2022-05-07 09:28:09 來源:動力節(jié)點(diǎn) 瀏覽1746次
截止到目前為止,在redis教程的文檔和實(shí)現(xiàn)里面并沒有針對object對象緩存的方法,然而,在我們的實(shí)際開發(fā)需要中,在很多時候我們是需要進(jìn)行對象緩存的,并且可以正確的讀取出來!
jedis.set(byte[], byte[])
看這個方法,是進(jìn)行字節(jié)碼操作的,這讓我們很容易想到在一些遠(yuǎn)程方法調(diào)用中,我們傳遞對象同樣傳遞的是字節(jié)碼,是不是可以參考呢?
首先,既然需要對對象進(jìn)行字節(jié)操作,即可寫和可讀的操作,為了保證這個原則,那么緩存對象需要實(shí)現(xiàn)Serializable 接口,進(jìn)行序列化和反序列化!
1.Serializable (接口,實(shí)現(xiàn)此接口的對象可以進(jìn)行序列化)
2.ByteArrayOutputStream,ObjectOutputStream 對象轉(zhuǎn)換為字節(jié)碼輸出流
3.ByteArrayInputStream ,ObjectInputStream 字節(jié)碼轉(zhuǎn)換為對象的輸入流
了解了如上三點(diǎn)知識后,我們就可以對對象進(jìn)行緩存操作了!
示例代碼如下,包括了對象緩存和List對象數(shù)組緩存,需要聲明的是,放入list數(shù)組中的對象同樣需要實(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;
}
}
對象的轉(zhuǎn)換示例如上代碼:
數(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;
}
}
通過以上操作即可以實(shí)現(xiàn)對象的緩存和讀取了!如果大家想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的課程內(nèi)容從入門到精通,細(xì)致全面,很適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對大家能夠有所幫助哦。

初級 202925

初級 203221

初級 202629

初級 203743