blob: 43c9f7729ef9648a48bbbb737c5694b134d410e0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.file;
import java.time.Instant;
import java.util.Optional;
/**
* Class to avoid repeated reads of file content when the file seldom changes.
*
* @author hakonhall
*/
class FileContentCache {
private final UnixPath path;
private Optional<byte[]> value = Optional.empty();
private Optional<Instant> modifiedTime = Optional.empty();
FileContentCache(UnixPath path) {
this.path = path;
}
byte[] get(Instant lastModifiedTime) {
if (!value.isPresent() || lastModifiedTime.compareTo(modifiedTime.get()) > 0) {
value = Optional.of(path.readBytes());
modifiedTime = Optional.of(lastModifiedTime);
}
return value.get();
}
void updateWith(byte[] content, Instant modifiedTime) {
this.value = Optional.of(content);
this.modifiedTime = Optional.of(modifiedTime);
}
}
|