33import java .util .*;
44import java .util .HashMap ;
55import java .text .SimpleDateFormat ;
6+ import static SSHelpers .Util .*;
67
78public class SCache {
9+ // private Helpers helpers = new Helpers();
10+
11+ // A key value store in memory
812 private Map <String , HttpResponse > cacheStore ;
13+ // A HttpRequest used to send If-None-Match and If-Modified-Since requests to the server
914 HttpRequest request ;
1015
1116 /**
12- * [SCache description]
17+ * This SCache class contains the cache for the proxy.
18+ * It provides methods to add, validate and get items from the cache.
19+ * The cache uses the URL as a key to lookup items in the cache
1320 */
1421 public SCache () {
1522 // synchronised HasMap prevents data corruption when reading while writing the Map
@@ -18,7 +25,7 @@ public SCache() {
1825 }
1926
2027 /**
21- * Add the HttpResponse into the HashMap cache with the url as the key.
28+ * Adds the HttpResponse into the cache with the url as the key.
2229 * @param key The Url
2330 * @param value The HttpResponse
2431 * @return The given value HttpResponse
@@ -29,7 +36,8 @@ public HttpResponse put(String key, HttpResponse value) {
2936 }
3037
3138 /**
32- * Retrieves the HttpResponse from HashMap cache by looking up the url
39+ * Retrieves the HttpResponse from cache by looking up the url.
40+ * The HttpResponse is checked to make sure it is fresh.
3341 * @param key The Url
3442 * @return The Cached HttpResponse
3543 */
@@ -50,9 +58,12 @@ public HttpResponse get(String key) {
5058 return value ;
5159 }
5260
53- // check if response is still valid
5461 /**
55- * Determine if the Cache HttpResonse is still fresh.
62+ * Determines if the cached HttpResonse is still fresh and updates the cache when it is not.
63+ * It checks that the response has not expired by checking the Last-Modified and the Expired header
64+ * in the response. If no expired header is present the server is asked with If-None-Match and the
65+ * If-Modified-Since headers. If the server response status code is not a 304 Not Modified response
66+ * than the new response updates the value in the cache.
5667 * @param requestURL The key in the cache
5768 * @param response The value in the cache
5869 * @return isValid? as in is the item in the cache fresh?
@@ -63,7 +74,6 @@ public boolean isValid(String requestURL, HttpResponse response) {
6374 return true ;
6475 }
6576
66- // HttpRequest check = new HttpRequest(request); // deep copy
6777 request .setHost (getHost (requestURL ));
6878 request .setPort (getPort (requestURL ));
6979 request .URL = getLocation (requestURL );
@@ -72,7 +82,6 @@ public boolean isValid(String requestURL, HttpResponse response) {
7282 if (newResponse == null ) { // if error
7383 return true ; // use the cached response if new response has an unrecoverable error.
7484 }
75- // HttpResponse r = new HttpRequest(request, response.lastModified, response.etag).send(); // ask server
7685 if (newResponse .status == 304 ) { // Not modified, so still valid
7786 System .out .println ("SCACHE: 304 Not modified" );
7887 return true ;
@@ -81,89 +90,5 @@ public boolean isValid(String requestURL, HttpResponse response) {
8190 put (requestURL , newResponse ); // save the new response
8291 return false ;
8392 }
84-
85- // return false;
86- }
87-
88- /**
89- * From a url string get the hostname
90- * @param url The Url (http://example.com/foo)
91- * @return The Hostname (example.com)
92- */
93- public static String getHost (String url ) {
94- if (url == null || url .length () <= 0 ) {
95- return "" ;
96- }
97-
98- int start = url .indexOf ("//" );
99- if (start == -1 ) {
100- return "" ;
101- }
102- start += 2 ;
103-
104- int end = url .indexOf ('/' , start );
105- if (end == -1 ) {
106- return "" ;
107- }
108-
109- return url .substring (start , end );
110- }
111-
112- /**
113- * From a utl string get the port number
114- * @param url The Url (http://example.com:50/foo)
115- * @return The Port number (50)
116- */
117- public static int getPort (String url ) {
118- if (url == null || url .length () <= 0 ) {
119- return 80 ;
120- }
121-
122- int start = url .indexOf ("//" );
123- if (start == -1 ) {
124- return 80 ;
125- }
126-
127- start += 2 ;
128-
129- int end = url .indexOf ('/' , start );
130- if (end == -1 ) {
131- return 80 ;
132- }
133-
134- int port = url .indexOf (':' , start );
135- if (port == -1 ) {
136- return 80 ;
137- }
138-
139- if (port < end ) {
140- end = port ;
141- }
142-
143- return Integer .parseInt (url .substring (port , end ));
144- }
145-
146- /**
147- * From a url string get the location
148- * @param url The Url (http://example.com/foo)
149- * @return The location (/foo)
150- */
151- public static String getLocation (String url ) {
152- if (url == null || url .length () <= 0 ) {
153- return "" ;
154- }
155-
156- int start = url .indexOf ("//" );
157- if (start == -1 ) {
158- return "" ;
159- }
160- start += 2 ;
161-
162- int end = url .indexOf ('/' , start );
163- if (end == -1 ) {
164- return "" ;
165- }
166-
167- return url .substring (end );
16893 }
16994}
0 commit comments