Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#112: Cache trimmed only after cache hits #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class HttpProxyCacheServerClients {
public HttpProxyCacheServerClients(String url, Config config) {
this.url = checkNotNull(url);
this.config = checkNotNull(config);
this.uiCacheListener = new UiListenerHandler(url, listeners);
this.uiCacheListener = new UiListenerHandler(url, listeners,config);
}

public void processRequest(GetRequest request, Socket socket) throws ProxyCacheException, IOException {
Expand Down Expand Up @@ -90,17 +90,25 @@ private static final class UiListenerHandler extends Handler implements CacheLis

private final String url;
private final List<CacheListener> listeners;
private final Config config;

public UiListenerHandler(String url, List<CacheListener> listeners) {
public UiListenerHandler(String url, List<CacheListener> listeners, Config config) {
super(Looper.getMainLooper());
this.url = url;
this.listeners = listeners;
this.config=config;
}

@Override
public void onCacheAvailable(File file, String url, int percentsAvailable) {
Message message = obtainMessage();
message.arg1 = percentsAvailable;

//if the file is fully cached, trim cache.
if(percentsAvailable==100){
config.diskUsage.trim(file.getParentFile());
}

message.obj = file;
sendMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface DiskUsage {

void touch(File file) throws IOException;

void trim(File folder);

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public synchronized void close() throws ProxyCacheException {
try {
dataFile.close();
diskUsage.touch(file);
diskUsage.trim(file.getParentFile());
} catch (IOException e) {
throw new ProxyCacheException("Error closing file " + file, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ abstract class LruDiskUsage implements DiskUsage {
private static final Logger LOG = LoggerFactory.getLogger("LruDiskUsage");
private final ExecutorService workerThread = Executors.newSingleThreadExecutor();

@Override
public void trim(File folder){
//folder passed in is the cache folder. Trim this if required
workerThread.submit(new TrimCallable(folder));
}

@Override
public void touch(File file) throws IOException {
workerThread.submit(new TouchCallable(file));
}

private void trimInBackground(File folder) throws IOException {
List<File> files = Files.getLruListFiles(folder);
trim(files);
}

private void touchInBackground(File file) throws IOException {
Files.setLastModifiedNow(file);
List<File> files = Files.getLruListFiles(file.getParentFile());
Expand Down Expand Up @@ -60,6 +71,7 @@ private long countTotalSize(List<File> files) {
return totalSize;
}


private class TouchCallable implements Callable<Void> {

private final File file;
Expand All @@ -74,4 +86,19 @@ public Void call() throws Exception {
return null;
}
}

private class TrimCallable implements Callable<Void> {

private final File folder;

public TrimCallable(File folder) {
this.folder = folder;
}

@Override
public Void call() throws Exception {
trimInBackground(folder);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public class UnlimitedDiskUsage implements DiskUsage {
public void touch(File file) throws IOException {
// do nothing
}

@Override
public void trim(File folder){
// do nothing
}
}