-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[env manager] more efficient zulu cache and add filters to list command
- Loading branch information
1 parent
eb222a4
commit 3279782
Showing
8 changed files
with
174 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
env-manager/src/main/java/io/yupiik/dev/shared/http/Cache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.yupiik.dev.shared.http; | ||
|
||
import io.yupiik.fusion.framework.api.scope.ApplicationScoped; | ||
import io.yupiik.fusion.framework.build.api.json.JsonModel; | ||
import io.yupiik.fusion.json.JsonMapper; | ||
|
||
import java.io.IOException; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.Clock; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.time.Clock.systemDefaultZone; | ||
import static java.util.stream.Collectors.toMap; | ||
|
||
@ApplicationScoped | ||
public class Cache { | ||
private final Path cache; | ||
private final long cacheValidity; | ||
private final JsonMapper jsonMapper; | ||
private final Clock clock; | ||
|
||
protected Cache() { | ||
this.cache = null; | ||
this.jsonMapper = null; | ||
this.clock = null; | ||
this.cacheValidity = 0L; | ||
} | ||
|
||
public Cache(final HttpConfiguration configuration, final JsonMapper jsonMapper) { | ||
this.jsonMapper = jsonMapper; | ||
try { | ||
this.cache = configuration.isCacheEnabled() ? null : Files.createDirectories(Path.of(configuration.cache())); | ||
} catch (final IOException e) { | ||
throw new IllegalArgumentException("Can't create HTTP cache directory : '" + configuration.cache() + "', adjust --http-cache parameter"); | ||
} | ||
this.cacheValidity = configuration.cacheValidity(); | ||
this.clock = systemDefaultZone(); | ||
} | ||
|
||
public void save(final Path key, final HttpResponse<String> result) { | ||
save( | ||
key, | ||
result.headers().map().entrySet().stream() | ||
.filter(it -> !"content-encoding".equalsIgnoreCase(it.getKey())) | ||
.collect(toMap(Map.Entry::getKey, l -> String.join(",", l.getValue()))), | ||
result.body()); | ||
} | ||
|
||
public void save(final Path cacheLocation, final Map<String, String> headers, final String body) { | ||
final var cachedData = jsonMapper.toString(new Response(headers, body, clock.instant().plusMillis(cacheValidity).toEpochMilli())); | ||
try { | ||
Files.writeString(cacheLocation, cachedData); | ||
} catch (final IOException e) { | ||
try { | ||
Files.deleteIfExists(cacheLocation); | ||
} catch (final IOException ex) { | ||
// no-op | ||
} | ||
} | ||
} | ||
|
||
public CachedEntry lookup(final HttpRequest request) { | ||
return lookup(request.uri().toASCIIString()); | ||
} | ||
|
||
public CachedEntry lookup(final String key) { | ||
if (cache == null) { | ||
return null; | ||
} | ||
|
||
final var cacheLocation = cache.resolve(Base64.getUrlEncoder().withoutPadding().encodeToString(key.getBytes(UTF_8))); | ||
if (Files.exists(cacheLocation)) { | ||
try { | ||
final var cached = jsonMapper.fromString(Response.class, Files.readString(cacheLocation)); | ||
if (cached.validUntil() > clock.instant().toEpochMilli()) { | ||
return new CachedEntry(cacheLocation, cached); | ||
} | ||
} catch (final IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
return new CachedEntry(cacheLocation, null); | ||
} | ||
|
||
public record CachedEntry(Path key, Response hit) { | ||
} | ||
|
||
@JsonModel | ||
public record Response(Map<String, String> headers, String payload, long validUntil) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.