更新時間:2022-05-31 08:55:26 來源:動力節(jié)點(diǎn) 瀏覽687次
緩存使您可以輕松地顯著加速應(yīng)用程序。 Java平臺的兩種出色的緩存實(shí)現(xiàn)是Guava緩存工具和Ehcache 。 盡管Ehcache功能豐富得多(例如其Searchable API ,將緩存持久化到磁盤或溢出到大內(nèi)存的可能性),但與Guava相比,它也帶來了相當(dāng)大的開銷。
下面小編將以實(shí)際Guava Cache實(shí)例的包裝器形式實(shí)現(xiàn)此文件持久性緩存FilePersistingCache 。
首先,將定義一個受保護(hù)的方法,該方法創(chuàng)建前面提到的后備緩存:
private LoadingCache<K, V> makeCache() {
return customCacheBuild()
.removalListener(new PersistingRemovalListener())
.build(new PersistedStateCacheLoader());
}
protected CacheBuilder<K, V> customCacheBuild(CacheBuilder<K, V> cacheBuilder) {
return CacheBuilder.newBuilder();
}
第一種方法將在內(nèi)部使用以構(gòu)建必要的緩存。 為了實(shí)現(xiàn)對緩存的任何自定義要求(例如,過期策略),應(yīng)該重寫第二種方法。 例如,這可以是條目或軟引用的最大值。 此緩存將與其他任何Guava緩存一樣使用。 緩存功能的關(guān)鍵是用于此緩存的RemovalListener和CacheLoader 。 我們將這兩個實(shí)現(xiàn)定義為FilePersistingCache內(nèi)部類:
private class PersistingRemovalListener implements RemovalListener<K, V> {
@Override
public void onRemoval(RemovalNotification<K, V> notification) {
if (notification.getCause() != RemovalCause.COLLECTED) {
try {
persistValue(notification.getKey(), notification.getValue());
} catch (IOException e) {
LOGGER.error(String.format("Could not persist key-value: %s, %s",
notification.getKey(), notification.getValue()), e);
}
}
}
}
public class PersistedStateCacheLoader extends CacheLoader<K, V> {
@Override
public V load(K key) {
V value = null;
try {
value = findValueOnDisk(key);
} catch (Exception e) {
LOGGER.error(String.format("Error on finding disk value to key: %s",
key), e);
}
if (value != null) {
return value;
} else {
return makeValue(key);
}
}
}
從代碼中可以明顯FilePersistingCache ,這些內(nèi)部類調(diào)用了我們尚未定義的FilePersistingCache方法。 這使我們可以通過重寫此類來定義自定義序列化行為。 刪除偵聽器將檢查清除緩存條目的原因。 如果RemovalCause被COLLECTED ,緩存條目沒有由用戶手動刪除,但它已被刪除作為高速緩存的驅(qū)逐策略的結(jié)果。 因此,如果用戶不希望刪除條目,我們將僅嘗試保留一個緩存條目。 CacheLoader將首先嘗試從磁盤還原現(xiàn)有值并僅在無法還原該值時創(chuàng)建一個新值。
缺少的方法定義如下:
private V findValueOnDisk(K key) throws IOException {
if (!isPersist(key)) return null;
File persistenceFile = makePathToFile(persistenceDirectory, directoryFor(key));
(!persistenceFile.exists()) return null;
FileInputStream fileInputStream = new FileInputStream(persistenceFile);
try {
FileLock fileLock = fileInputStream.getChannel().lock();
try {
return readPersisted(key, fileInputStream);
} finally {
fileLock.release();
}
} finally {
fileInputStream.close();
}
}
private void persistValue(K key, V value) throws IOException {
if (!isPersist(key)) return;
File persistenceFile = makePathToFile(persistenceDirectory, directoryFor(key));
persistenceFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(persistenceFile);
try {
FileLock fileLock = fileOutputStream.getChannel().lock();
try {
persist(key, value, fileOutputStream);
} finally {
fileLock.release();
}
} finally {
fileOutputStream.close();
}
}
private File makePathToFile(@Nonnull File rootDir, List<String> pathSegments) {
File persistenceFile = rootDir;
for (String pathSegment : pathSegments) {
persistenceFile = new File(persistenceFile, pathSegment);
}
if (rootDir.equals(persistenceFile) || persistenceFile.isDirectory()) {
throw new IllegalArgumentException();
}
return persistenceFile;
}
protected abstract List<String> directoryFor(K key);
protected abstract void persist(K key, V value, OutputStream outputStream)
throws IOException;
protected abstract V readPersisted(K key, InputStream inputStream)
throws IOException;
protected abstract boolean isPersist(K key);
所實(shí)現(xiàn)的方法在同步文件訪問并保證流被適當(dāng)關(guān)閉的同時,還要注意對值進(jìn)行序列化和反序列化。 最后四種方法仍然是抽象的,并由緩存的用戶來實(shí)現(xiàn)。 directoryFor(K)方法應(yīng)為每個密鑰標(biāo)識一個唯一的文件名。 在最簡單的情況下,密鑰的K類的toString方法是以這種方式實(shí)現(xiàn)的。 此外,小編還對persist , readPersisted和isPersist方法進(jìn)行了抽象化處理,以實(shí)現(xiàn)自定義序列化策略,例如使用Kryo 。 在最簡單的情況下,您將使用內(nèi)置的Java功能,該功能使用ObjectInputStream和ObjectOutputStream 。 對于isPersist ,假設(shè)僅在需要序列化時才使用此實(shí)現(xiàn),則將返回true 。 添加了此功能以支持混合緩存,在混合緩存中,您只能將值序列化為某些鍵。 確保不要在persist和readPersisted方法中關(guān)閉流,因?yàn)槲募到y(tǒng)鎖依賴于要打開的流。 上面的實(shí)現(xiàn)將為您關(guān)閉流。
最后,添加了一些服務(wù)方法來訪問緩存。 當(dāng)然,實(shí)現(xiàn)Guava的Cache接口將是一個更優(yōu)雅的解決方案:
public V get(K key) {
return underlyingCache.getUnchecked(key);
}
public void put(K key, V value) {
underlyingCache.put(key, value);
}
public void remove(K key) {
underlyingCache.invalidate(key);
}
protected Cache<K, V> getUnderlyingCache() {
return underlyingCache;
}
當(dāng)然,可以進(jìn)一步改善該解決方案。 如果在并發(fā)場景中使用緩存,請注意, RemovalListener是除大多數(shù)Guava緩存方法以外的異步執(zhí)行的。 從代碼中可以明顯看出,添加了文件鎖,以避免在文件系統(tǒng)上發(fā)生讀/寫沖突。如果大家想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Guava教程,里面有更豐富的知識等著大家去學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743