Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cache file expires after 1 day

  • Loading branch information...
commit 1a661856167c60ada96ba2c7aa9c884a801b8321 1 parent ecfeea2
@tindandelion authored
View
33 Radio-T-Test/src/org/dandelion/radiot/integration/FilePodcastsCacheTest.java
@@ -32,11 +32,6 @@ public void testSavingAndLoadingPodcastList() throws Exception {
assertThat(restored.getTitle(), equalTo(original.getTitle()));
}
- public void testCacheIsValidWhenItHasData() throws Exception {
- assertThat(cache, not(valid()));
- cache.updateWith(aListWith(aPodcastItem()));
- assertThat(cache, is(valid()));
- }
public void testResettingTheCache() throws Exception {
cache.updateWith(aListWith(aPodcastItem()));
@@ -51,6 +46,12 @@ public void testResettingTheInvalidCacheIsNotAnError() throws Exception {
assertThat(cache, not(valid()));
}
+ public void testCacheIsValidWhenItHasData() throws Exception {
+ assertThat(cache, not(valid()));
+ cache.updateWith(aListWith(aPodcastItem()));
+ assertThat(cache, is(valid()));
+ }
+
public void testCacheIsInvalidIfFileIsCorrupt() throws Exception {
createValidCacheFile(FORMAT_VERSION);
@@ -58,6 +59,28 @@ public void testCacheIsInvalidIfFileIsCorrupt() throws Exception {
assertThat(cache, not(valid()));
}
+ public void testCacheIsInvalidIfOlderThanOneDay() throws Exception {
+ createValidCacheFile(FORMAT_VERSION);
+ assertThat(cache, is(valid()));
+
+ cacheCreated(hoursAgo(23));
+ assertThat(cache, is((valid())));
+
+ cacheCreated(hoursAgo(25));
+ assertThat(cache, is(not(valid())));
+ }
+
+ @SuppressWarnings("ResultOfMethodCallIgnored")
+ private void cacheCreated(long timestamp) {
+ cacheFile.setLastModified(timestamp);
+ }
+
+ private long hoursAgo(int hours) {
+ long current = System.currentTimeMillis();
+ int millis = (hours * 3600 * 1000);
+ return current - millis;
+ }
+
private void createValidCacheFile(int formatVersion) {
new FilePodcastsCache(cacheFile, formatVersion).updateWith(aListWith(aPodcastItem()));
}
View
13 Radio-T/src/org/dandelion/radiot/podcasts/core/FilePodcastsCache.java
@@ -3,6 +3,9 @@
import java.io.*;
public class FilePodcastsCache implements PodcastsCache {
+ // A threshold of one day
+ private static final long LIFETIME_THRESHOLD = 24 * 3600 * 1000;
+
private final File file;
private final int formatVersion;
@@ -89,12 +92,16 @@ private void writeItemsInto(ObjectOutputStream out, PodcastList data) throws IOE
@Override
public boolean isValid() {
try {
- return readVersion() == formatVersion;
+ return isCurrent() && isValidVersion();
} catch (Exception ex) {
return false;
}
}
+ private boolean isValidVersion() throws IOException {
+ return readVersion() == formatVersion;
+ }
+
private int readVersion() throws IOException {
ObjectInputStream in = openInputStream();
try {
@@ -104,4 +111,8 @@ private int readVersion() throws IOException {
}
}
+ public boolean isCurrent() {
+ long lifetime = System.currentTimeMillis() - file.lastModified();
+ return lifetime < LIFETIME_THRESHOLD;
+ }
}
View
6 TODO
@@ -7,10 +7,8 @@ Backlog:
* New icon with ICS guidelines
Version 1.5:
- * Cache podcast list locally
- * Cache expiration
-
* Google Analytics
+ DONE Cache podcast list locally
DONE Android 4.0 UI
DONE New status bar icons
DONE Button background styling
@@ -24,6 +22,8 @@ Version 1.5:
DONE Center logo text
DONE Home screen buttons
* Testing: podcast loading performance
+ * Memory usage
+ * Time to load from cache
* Testing: file to download already exists
* Debt:

0 comments on commit 1a66185

Please sign in to comment.
Something went wrong with that request. Please try again.