Skip to content

Commit

Permalink
Server: Automatic cache clearing!
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Feb 26, 2024
1 parent 7db36d7 commit da04cd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/src/main/java/xyz/e3ndr/athena/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static class SessionConfig {
public static class TranscodeConfig {
public boolean enable = true;
public TranscodeAcceleration acceleration = TranscodeAcceleration.SOFTWARE_ONLY;
public @JsonField("cache_rentention_h") int cacheRetentionHours = 36;
}

}
30 changes: 30 additions & 0 deletions server/src/main/java/xyz/e3ndr/athena/transcoding/Transcoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.jetbrains.annotations.Nullable;

Expand All @@ -39,6 +40,35 @@ public class Transcoder {

private static final FastLogger logger = new FastLogger();

static {
if (Athena.config.transcoding.cacheRetentionHours != -1) {
AsyncTask.create(() -> {
while (true) {
for (File file : Athena.cacheDirectory.listFiles()) {
if (file.getName().endsWith(".lastaccess")) continue;

try {
File lastAccessFile = new File(file.getCanonicalPath() + ".lastaccess");
long lastAccess = Long.parseLong(Files.readString(lastAccessFile.toPath()));

if (System.currentTimeMillis() - lastAccess < TimeUnit.HOURS.toMillis(Athena.config.transcoding.cacheRetentionHours)) {
continue; // Not expired!
}

if (file.delete()) {
lastAccessFile.delete();
logger.info("Successfully deleted %s from the transcode cache.", file);
}
} catch (NumberFormatException | IOException ignored) {}
}
try {
TimeUnit.MINUTES.sleep(5);
} catch (InterruptedException e) {}
}
});
}
}

@SneakyThrows
public static @Nullable TranscodeSession start(File targetFile, Media media, VideoQuality desiredQuality, VideoCodec desiredVCodec, AudioCodec desiredACodec, ContainerFormat desiredContainer, int... streamIds) {
if (!Athena.config.transcoding.enable && (desiredACodec != AudioCodec.SOURCE || desiredVCodec != VideoCodec.SOURCE)) {
Expand Down

0 comments on commit da04cd8

Please sign in to comment.