Skip to content

Commit

Permalink
read cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerIn-wonderland committed May 17, 2024
1 parent 1942f8f commit 807b72e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions lib/9p-filer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const VIRTIO_9P_F_MOUNT_TAG = 0;
// Assumed max tag length in bytes.
const VIRTIO_9P_MAX_TAGLEN = 254;

const FSCACHE = new Map()

// TODO
// flush

Expand Down Expand Up @@ -954,19 +956,37 @@ Virtio9p.prototype.ReceiveRequest = async function(bufchain) {
_read(data);
return;
}
if (!FSCACHE.has(path))
fs.readFile(path, function(err, data) {
if (self.shouldAbortRequest(tag)) return;

fs.readFile(path, function(err, data) {
if (self.shouldAbortRequest(tag)) return;
if (err) {
self.SendError(tag, err);
self.SendReply(bufchain)
return;
}

if (err) {
self.SendError(tag, err);
self.SendReply(bufchain)
return;
}
self.pendingTags[tag].data = data;
FSCACHE.set(path, {data: data, remaining: data.byteLength})
_read(data);
});
else {
const file = FSCACHE.get(path);
file.remaining -= count;

self.pendingTags[tag].data = data;
_read(data);
});
_read(file.data)
setTimeout(() => {
if (FSCACHE.has(path)) {
// console.log("Deleted FSCACHE entry for: " + path + "\nReason: over 1 minute");
FSCACHE.delete(path);
}
}, 60 * 1000)
if (file.remaining <= 0) {
// console.log("Deleted FSCACHE entry for: " + path + "\nReason: Used up");
FSCACHE.delete(path);
}
}

break;
case 118: // write
Expand Down

0 comments on commit 807b72e

Please sign in to comment.