mirror of
https://github.com/zhigang1992/AndroidVideoCache.git
synced 2026-01-26 22:23:00 +08:00
allow user to create a custom DiskUsage class (or extend LruDiskUsage class) to implement custom cache keep/delete rules
This commit is contained in:
10
README.md
10
README.md
@@ -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.
|
||||
|
||||
|
||||
@@ -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}.
|
||||
*
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user