Bläddra i källkod

More work on caching.

Enable coverage.
master
Chris Smith 15 år sedan
förälder
incheckning
46ac5158e0

+ 1
- 0
.gitignore Visa fil

@@ -2,3 +2,4 @@
2 2
 /build
3 3
 /derby.log
4 4
 /dist
5
+/coverage

+ 6
- 0
nbproject/coverage.properties Visa fil

@@ -0,0 +1,6 @@
1
+#coverage viewer module properties for project
2
+#Tue Jan 06 09:54:06 GMT 2009
3
+coverage.activity=ON
4
+project.type=java
5
+coverage.templateFile=coverage/template.emma
6
+coverage.coverageFile=coverage/coverage.emma

+ 1
- 0
nbproject/private/private.properties Visa fil

@@ -5,3 +5,4 @@ javac.debug=true
5 5
 javadoc.preview=true
6 6
 jaxws.endorsed.dir=/usr/local/netbeans-6.5/java2/modules/ext/jaxws21/api:/usr/local/netbeans-6.5/ide10/modules/ext/jaxb/api
7 7
 user.properties.file=/home/chris/.netbeans/6.5/build.properties
8
+file.reference.emma.jar=/home/chris/.netbeans/6.5/modules/ext/emma.jar

+ 1
- 1
nbproject/project.properties Visa fil

@@ -70,7 +70,7 @@ run.classpath=\
70 70
 run.jvmargs=
71 71
 run.test.classpath=\
72 72
     ${javac.test.classpath}:\
73
-    ${build.test.classes.dir}
73
+    ${build.test.classes.dir}:${file.reference.emma.jar}
74 74
 source.encoding=UTF-8
75 75
 src.dir=src
76 76
 test.src.dir=test

+ 30
- 5
src/uk/co/md87/evetool/api/io/ApiCache.java Visa fil

@@ -14,18 +14,43 @@ import java.util.Map;
14 14
  */
15 15
 public interface ApiCache {
16 16
 
17
-    public static enum ApiCacheStatus {
17
+    public static enum CacheStatus {
18 18
         MISS,
19 19
         EXPIRED,
20
-        CACHED;
20
+        HIT;
21
+    }
22
+
23
+    public static class CacheResult {
24
+
25
+        private final String data;
26
+        private final long cachedAt;
27
+        private final long cachedUntil;
28
+
29
+        public CacheResult(String data, long cachedAt, long cachedUntil) {
30
+            this.data = data;
31
+            this.cachedAt = cachedAt;
32
+            this.cachedUntil = cachedUntil;
33
+        }
34
+
35
+        public long getCachedAt() {
36
+            return cachedAt;
37
+        }
38
+
39
+        public long getCachedUntil() {
40
+            return cachedUntil;
41
+        }
42
+
43
+        public String getData() {
44
+            return data;
45
+        }
46
+        
21 47
     }
22 48
 
23 49
     void setCache(final String method, final Map<String, String> args,
24 50
             final String data, final long cacheUntil);
25 51
 
26
-    ApiCacheStatus getCacheStatus(final String method, final Map<String, String> args);
52
+    CacheStatus getCacheStatus(final String method, final Map<String, String> args);
27 53
 
28
-    // TODO: This should return meta-data too
29
-    String getCache(final String method, final Map<String, String> args);
54
+    CacheResult getCache(final String method, final Map<String, String> args);
30 55
 
31 56
 }

+ 17
- 12
src/uk/co/md87/evetool/api/io/ApiDownloader.java Visa fil

@@ -11,7 +11,8 @@ import java.util.Map;
11 11
 import java.util.logging.Level;
12 12
 import java.util.logging.Logger;
13 13
 
14
-import uk.co.md87.evetool.api.io.ApiCache.ApiCacheStatus;
14
+import uk.co.md87.evetool.api.io.ApiCache.CacheStatus;
15
+import uk.co.md87.evetool.api.parser.ApiResult;
15 16
 
16 17
 /**
17 18
  *
@@ -46,25 +47,29 @@ public class ApiDownloader {
46 47
         this.charID = charID;
47 48
     }
48 49
 
49
-    public String getPage(final String method, final Map<String, String> args) {
50
-        final Map<String, String> ourArgs = new HashMap<String, String>(args);
51
-
52
-        // TODO: Abstract
50
+    protected void addArgs(final Map<String, String> args) {
53 51
         if (userID != null) {
54
-            ourArgs.put("userID", userID);
52
+            args.put("userID", userID);
55 53
         }
56 54
 
57 55
         if (apiKey != null) {
58
-            ourArgs.put("apiKey", apiKey);
56
+            args.put("apiKey", apiKey);
59 57
         }
60 58
 
61 59
         if (charID != null) {
62
-            ourArgs.put("characterID", charID);
60
+            args.put("characterID", charID);
63 61
         }
62
+    }
64 63
 
65
-        final ApiCacheStatus cacheStatus = cache.getCacheStatus(method, args);
64
+    public ApiCache.CacheResult getPage(final String method, final Map<String, String> args) {
65
+        final Map<String, String> ourArgs = new HashMap<String, String>(args);
66
+        addArgs(ourArgs);
66 67
 
67
-        if (cacheStatus == ApiCacheStatus.CACHED) {
68
+        // TODO: Refactor to avoid duplicate gets
69
+
70
+        final CacheStatus cacheStatus = cache.getCacheStatus(method, args);
71
+
72
+        if (cacheStatus == CacheStatus.HIT) {
68 73
             return cache.getCache(method, args);
69 74
         }
70 75
 
@@ -76,10 +81,10 @@ public class ApiDownloader {
76 81
 
77 82
             cache.setCache(method, args, builder.toString(),
78 83
                     System.currentTimeMillis() + 20000); // TODO: Proper time
79
-            return builder.toString();
84
+            return cache.getCache(method, args);
80 85
         } catch (IOException ex) {
81 86
             LOGGER.log(Level.WARNING, "API request failed", ex);
82
-            if (cacheStatus == ApiCacheStatus.EXPIRED) {
87
+            if (cacheStatus == CacheStatus.EXPIRED) {
83 88
                 return cache.getCache(method, args);
84 89
             } else {
85 90
                 return null;

+ 9
- 7
src/uk/co/md87/evetool/api/io/DBCache.java Visa fil

@@ -58,7 +58,7 @@ public class DBCache implements ApiCache {
58 58
     public void setCache(final String method, final Map<String, String> args,
59 59
             final String data, final long cacheUntil) {
60 60
         try {
61
-            if (getCacheStatus(method, args) == ApiCacheStatus.MISS) {
61
+            if (getCacheStatus(method, args) == CacheStatus.MISS) {
62 62
                 // The page has never been requested before.
63 63
                 
64 64
                 prepInsert.setString(1, method);
@@ -84,7 +84,7 @@ public class DBCache implements ApiCache {
84 84
 
85 85
     /** {@inheritDoc} */
86 86
     @Override
87
-    public ApiCacheStatus getCacheStatus(final String method, final Map<String, String> args) {
87
+    public CacheStatus getCacheStatus(final String method, final Map<String, String> args) {
88 88
         try {
89 89
             prepCheck.setString(1, method);
90 90
             prepCheck.setString(2, Downloader.encodeArguments(args));
@@ -94,19 +94,19 @@ public class DBCache implements ApiCache {
94 94
             if (rs.next()) {
95 95
                 return rs.getTimestamp("PC_CACHEDUNTIL")
96 96
                         .before(new Date(System.currentTimeMillis()))
97
-                        ? ApiCacheStatus.EXPIRED : ApiCacheStatus.CACHED;
97
+                        ? CacheStatus.EXPIRED : CacheStatus.HIT;
98 98
             } else {
99
-                return ApiCacheStatus.MISS;
99
+                return CacheStatus.MISS;
100 100
             }
101 101
         } catch (SQLException ex) {
102 102
             LOGGER.log(Level.SEVERE, "Error checking cache status", ex);
103
-            return ApiCacheStatus.MISS;
103
+            return CacheStatus.MISS;
104 104
         }
105 105
     }
106 106
 
107 107
     /** {@inheritDoc} */
108 108
     @Override
109
-    public String getCache(final String method, final Map<String, String> args) {
109
+    public CacheResult getCache(final String method, final Map<String, String> args) {
110 110
         try {
111 111
             prepRetrieve.setString(1, method);
112 112
             prepRetrieve.setString(2, Downloader.encodeArguments(args));
@@ -115,7 +115,9 @@ public class DBCache implements ApiCache {
115 115
 
116 116
             if (rs.next()) {
117 117
                 final Clob clob = rs.getClob("PC_DATA");
118
-                return clob.getSubString(1, (int) clob.length());
118
+                final long at = rs.getTimestamp("PC_CACHEDAT").getTime();
119
+                final long until = rs.getTimestamp("PC_CACHEDUNTIL").getTime();
120
+                return new CacheResult(clob.getSubString(1, (int) clob.length()), at, until);
119 121
             } else {
120 122
                 LOGGER.log(Level.WARNING, "No cache result for " + method);
121 123
                 return null;

+ 6
- 6
test/uk/co/md87/evetool/api/io/DBCacheTest.java Visa fil

@@ -28,19 +28,19 @@ public class DBCacheTest {
28 28
     public void testBasicInsert() throws SQLException {
29 29
         final Map<String, String> args = new HashMap<String, String>();
30 30
         final Connection conn = DriverManager.getConnection(dbURL);
31
-        final EveApi api = new EveApi(conn); // To create tables
31
+        new EveApi(conn); // To create tables
32 32
         final DBCache cache = new DBCache(conn);
33 33
 
34 34
         args.put("foo", "bar");
35 35
         args.put("rand", String.valueOf(System.currentTimeMillis()));
36 36
 
37
-        assertEquals(ApiCache.ApiCacheStatus.MISS, cache.getCacheStatus("/unittest", args));
37
+        assertEquals(ApiCache.CacheStatus.MISS, cache.getCacheStatus("/unittest", args));
38 38
         cache.setCache("/unittest", args, "testing 123", System.currentTimeMillis() - 100000);
39
-        assertEquals(ApiCache.ApiCacheStatus.EXPIRED, cache.getCacheStatus("/unittest", args));
40
-        assertEquals("testing 123", cache.getCache("/unittest", args));
39
+        assertEquals(ApiCache.CacheStatus.EXPIRED, cache.getCacheStatus("/unittest", args));
40
+        assertEquals("testing 123", cache.getCache("/unittest", args).getData());
41 41
         cache.setCache("/unittest", args, "testing 456", System.currentTimeMillis() + 100000);
42
-        assertEquals(ApiCache.ApiCacheStatus.CACHED, cache.getCacheStatus("/unittest", args));
43
-        assertEquals("testing 456", cache.getCache("/unittest", args));
42
+        assertEquals(ApiCache.CacheStatus.HIT, cache.getCacheStatus("/unittest", args));
43
+        assertEquals("testing 456", cache.getCache("/unittest", args).getData());
44 44
         conn.close();
45 45
     }
46 46
 

Laddar…
Avbryt
Spara