allow user to create a custom DiskUsage class (or extend LruDiskUsage class) to implement custom cache keep/delete rules

This commit is contained in:
Joe Page
2016-12-30 15:38:05 -06:00
committed by Alexey Danilov
parent 351cf2b986
commit f5dd92efff
4 changed files with 29 additions and 2 deletions

View File

@@ -99,6 +99,16 @@ private HttpProxyCacheServer newProxy() {
}
```
or even implement your own `DiskUsage` strategy:
```java
private HttpProxyCacheServer newProxy() {
return new HttpProxyCacheServer.Builder(this)
.diskUsage(new MyCoolDiskUsageStrategy())
.build();
}
```
### Listen caching progress
Use `HttpProxyCacheServer.registerCacheListener(CacheListener listener)` method to set listener with callback `onCacheAvailable(File cacheFile, String url, int percentsAvailable)` to be aware of caching progress. Do not forget to to unsubscribe listener with help of `HttpProxyCacheServer.unregisterCacheListener(CacheListener listener)` method to avoid memory leaks.

View File

@@ -414,6 +414,16 @@ public class HttpProxyCacheServer {
return this;
}
/**
* Set custom DiskUsage logic for handling when to keep or clean cache.
*
* @param diskUsage a disk usage strategy, cant be {@code null}.
*/
public Builder diskUsage(DiskUsage diskUsage) {
this.diskUsage = checkNotNull(diskUsage);
return this;
}
/**
* Builds new instance of {@link HttpProxyCacheServer}.
*

View File

@@ -1,11 +1,15 @@
package com.danikula.videocache.file;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
@@ -16,6 +20,8 @@ import java.util.List;
*/
class Files {
private static final Logger LOG = LoggerFactory.getLogger("Files");
static void makeDir(File directory) throws IOException {
if (directory.exists()) {
if (!directory.isDirectory()) {
@@ -46,7 +52,8 @@ class Files {
if (!modified) {
modify(file);
if (file.lastModified() < now) {
throw new IOException("Error set last modified date to " + file);
// NOTE: apparently this is a known issue (see: http://stackoverflow.com/questions/6633748/file-lastmodified-is-never-what-was-set-with-file-setlastmodified)
LOG.warn("Last modified date {} is not set for file {}", new Date(file.lastModified()), file.getAbsolutePath());
}
}
}

View File

@@ -15,7 +15,7 @@ import java.util.concurrent.Executors;
*
* @author Alexey Danilov (danikula@gmail.com).
*/
abstract class LruDiskUsage implements DiskUsage {
public abstract class LruDiskUsage implements DiskUsage {
private static final Logger LOG = LoggerFactory.getLogger("LruDiskUsage");
private final ExecutorService workerThread = Executors.newSingleThreadExecutor();