solkin / disk-lru-cache Goto Github PK
View Code? Open in Web Editor NEW๐พ Disk LRU cache with persisted journal
License: MIT License
๐พ Disk LRU cache with persisted journal
License: MIT License
Sorry, I don't know how to do the pull request thing yet, so I thought I would just post the problem and resolution here.
Journal.java
readJournal() and writeJournal() are causing finalizer and "resource failed to call close" warnings. I believe the issue is with the wrapped input/output streams. Anyway, the below changes using auto resource management resolve the issue.
public void writeJournal() {
try (FileOutputStream fileStream = new FileOutputStream(file)) {
try (DataOutputStream stream = new DataOutputStream(fileStream)) {
stream.writeShort(JOURNAL_FORMAT_VERSION);
stream.writeInt(map.size());
for (Record record : map.values()) {
stream.writeUTF(record.getKey());
stream.writeUTF(record.getName());
stream.writeLong(record.getTime());
stream.writeLong(record.getSize());
}
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static Journal readJournal(FileManager fileManager) {
File file = fileManager.journal();
log("[.] Start journal reading", file.getName());
Journal journal = new Journal(file, fileManager);
try (FileInputStream fileStream = new FileInputStream(file)) {
try (DataInputStream stream = new DataInputStream(fileStream)) {
int version = stream.readShort();
if (version != JOURNAL_FORMAT_VERSION) {
throw new IllegalArgumentException("Invalid journal format version");
}
int count = stream.readInt();
long totalSize = 0;
for (int c = 0; c < count; c++) {
String key = stream.readUTF();
String name = stream.readUTF();
long time = stream.readLong();
long size = stream.readLong();
totalSize += size;
Record record = new Record(key, name, time, size);
journal.put(record);
}
journal.setTotalSize(totalSize);
log("[.] Journal read. Files count is %d and total size is %d", count, totalSize);
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return journal;
}
Thanks, jb
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.