guava缓存数据到本地_扩展Guava缓存以溢出到磁盘
guava緩存數(shù)據(jù)到本地
緩存使您可以輕松地顯著加速應(yīng)用程序。 Java平臺(tái)的兩種出色的緩存實(shí)現(xiàn)是Guava緩存和Ehcache 。 盡管Ehcache功能豐富得多(例如其Searchable API ,將緩存持久化到磁盤或溢出到大內(nèi)存的可能性),但與Guava相比,它也帶來了相當(dāng)大的開銷。 在最近的項(xiàng)目中,我發(fā)現(xiàn)需要將全面的緩存溢出到磁盤上,但與此同時(shí),我經(jīng)常需要使該緩存的特定值無效。 由于Ehcache的Searchable API僅可用于內(nèi)存中的緩存,因此這使我陷入了兩難境地。 但是,擴(kuò)展Guava緩存以允許以結(jié)構(gòu)化方式溢出到磁盤非常容易。 這使我既溢出到磁盤又需要必需的失效功能。 在本文中,我想展示如何實(shí)現(xiàn)這一目標(biāo)。
我將以實(shí)際Guava Cache實(shí)例的包裝器形式實(shí)現(xiàn)此文件持久性緩存FilePersistingCache 。 當(dāng)然,這不是最優(yōu)雅的解決方案(更優(yōu)雅的做法是使用這種行為來實(shí)現(xiàn)實(shí)際的Guava Cache ),但是在大多數(shù)情況下,我都會(huì)這樣做。
首先,我將定義一個(gè)受保護(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 。 我們將這兩個(gè)實(shí)現(xiàn)定義為FilePersistingCache內(nèi)部類:
private class PersistingRemovalListener implements RemovalListener<K, V> {@Overridepublic 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> {@Overridepublic 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 ,緩存條目沒有由用戶手動(dòng)刪除,但它已被刪除作為高速緩存的驅(qū)逐策略的結(jié)果。 因此,如果用戶不希望刪除條目,我們將僅嘗試保留一個(gè)緩存條目。 CacheLoader將首先嘗試從磁盤還原現(xiàn)有值并僅在無法還原該值時(shí)創(chuàng)建一個(gè)新值。
缺少的方法定義如下:
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)閉的同時(shí),還要注意對值進(jìn)行序列化和反序列化。 最后四種方法仍然是抽象的,并由緩存的用戶來實(shí)現(xiàn)。 directoryFor(K)方法應(yīng)為每個(gè)密鑰標(biāo)識(shí)一個(gè)唯一的文件名。 在最簡單的情況下,密鑰的K類的toString方法是以這種方式實(shí)現(xiàn)的。 此外,我還對persist , readPersisted和isPersist方法進(jìn)行了抽象化處理,以實(shí)現(xiàn)自定義序列化策略,例如使用Kryo 。 在最簡單的情況下,您將使用內(nèi)置的Java功能,該功能使用ObjectInputStream和ObjectOutputStream 。 對于isPersist ,假設(shè)僅在需要序列化時(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接口將是一個(gè)更優(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ā)生讀/寫沖突。 但是,這種異步性確實(shí)意味著即使內(nèi)存中仍然有一個(gè)值,也很少有機(jī)會(huì)重新創(chuàng)建值條目。 如果需要避免這種情況,請確保在包裝器的get方法中調(diào)用基礎(chǔ)緩存的cleanUp方法。 最后,記住在緩存過期時(shí)清理文件系統(tǒng)。 最佳地,您將使用系統(tǒng)的臨時(shí)文件夾存儲(chǔ)高速緩存條目,從而完全避免此問題。 在示例代碼中,目錄由名為persistenceDirectory的實(shí)例字段表示,該實(shí)例字段可以例如在構(gòu)造函數(shù)中初始化。
更新 :我對上面描述的內(nèi)容進(jìn)行了干凈的實(shí)現(xiàn),您可以在Git Hub頁面和Maven Central上找到這些實(shí)現(xiàn)。 如果需要將緩存對象存儲(chǔ)在磁盤上,請隨時(shí)使用它。
翻譯自: https://www.javacodegeeks.com/2013/12/extending-guava-caches-to-overflow-to-disk.html
guava緩存數(shù)據(jù)到本地
總結(jié)
以上是生活随笔為你收集整理的guava缓存数据到本地_扩展Guava缓存以溢出到磁盘的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云电脑免费手机号码(手机云电脑永久免费版
- 下一篇: b站电脑版稍后再看(b站电脑版稍后再看在