Skip to content

Commit 9949b66

Browse files
committed
Change cache policy
1 parent 4858cdf commit 9949b66

File tree

8 files changed

+97
-127
lines changed

8 files changed

+97
-127
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ port:
1616

1717
# Cache
1818
cache:
19-
default_browser_cache_expiration: 0
20-
default_ttl: 60
19+
default_expires: 60
2120

2221
# Cluster
2322
cluster:
@@ -99,9 +98,8 @@ http://example.com:{port}/{key}
9998
http://example.com:55200/example_key
10099
{
101100
"value": "Test value",
102-
"content_type": "plain/text",
103-
"browser_cache_expiration": 200,
104-
"ttl": 200
101+
"content_type": "text/plain",
102+
"expires": 200
105103
}
106104
```
107105

src/main/java/org/code13k/heets/business/ClusteredCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public void mapCleared(MapEvent event) {
7171
/**
7272
* Set
7373
*/
74-
public void set(String key, CacheData value, long ttl, Consumer<Boolean> consumer) {
74+
public void set(String key, CacheData value, Consumer<Boolean> consumer) {
7575
if (StringUtils.isEmpty(key) == false) {
76-
ICompletableFuture<CacheData> future = mData.putAsync(key, value, ttl, TimeUnit.SECONDS);
76+
ICompletableFuture<CacheData> future = mData.putAsync(key, value, value.getExpires(), TimeUnit.SECONDS);
7777
future.andThen(new ExecutionCallback<CacheData>() {
7878
@Override
7979
public void onResponse(CacheData response) {

src/main/java/org/code13k/heets/config/AppConfig.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,12 @@ protected boolean loadConfig(final String content, final String filePath) {
8181

8282
// CacheInfo
8383
LinkedHashMap cacheObject = (LinkedHashMap) yamlObject.get("cache");
84-
Integer cacheDefaultBrowserExpiration = (Integer) cacheObject.get("default_browser_cache_expiration");
85-
if (cacheDefaultBrowserExpiration < 0) {
86-
mLogger.error("Invalid default_browser_cache_expiration of cache : " + cacheDefaultBrowserExpiration);
84+
Integer cacheDefaultExpires = (Integer) cacheObject.get("default_expires");
85+
if (cacheDefaultExpires <= 0) {
86+
mLogger.error("Invalid default_expires of cache : " + cacheDefaultExpires);
8787
return false;
8888
}
89-
Integer cacheDefaultTtl = (Integer) cacheObject.get("default_ttl");
90-
if (cacheDefaultTtl <= 0) {
91-
mLogger.error("Invalid default_ttl of cache : " + cacheDefaultTtl);
92-
return false;
93-
}
94-
mCacheInfo.setDefaultBrowserCacheExpiration(cacheDefaultBrowserExpiration);
95-
mCacheInfo.setDefaultTtl(cacheDefaultTtl);
89+
mCacheInfo.setDefaultExpires(cacheDefaultExpires);
9690

9791
// ClusterInfo
9892
LinkedHashMap clusterObject = (LinkedHashMap) yamlObject.get("cluster");
@@ -131,8 +125,7 @@ public void logging() {
131125
mLogger.info("api_http of PortInfo = " + mPortInfo.getApiHttp());
132126

133127
// CacheInfo
134-
mLogger.info("default_browser_cache_expiration of CacheInfo = " + mCacheInfo.getDefaultBrowserCacheExpiration());
135-
mLogger.info("default_ttl of CacheInfo = " + mCacheInfo.getDefaultTtl());
128+
mLogger.info("default_expires of CacheInfo = " + mCacheInfo.getDefaultExpires());
136129

137130
// ClusterInfo
138131
mLogger.info("port of ClusterInfo = " + mClusterInfo.getPort());

src/main/java/org/code13k/heets/model/CacheData.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
public class CacheData extends BasicModel {
44
private String value;
55
private String contentType;
6-
private int browserCacheExpiration;
6+
private int expires;
77
private long modified;
88

99

10-
public CacheData(String value, String contentType, int browserCacheExpiration){
10+
public CacheData(String value, String contentType, int expires){
1111
setValue(value);
1212
setContentType(contentType);
13-
setBrowserCacheExpiration(browserCacheExpiration);
13+
setExpires(expires);
1414
updateModified();
1515
}
1616

@@ -30,12 +30,12 @@ public void setContentType(String contentType) {
3030
this.contentType = contentType;
3131
}
3232

33-
public int getBrowserCacheExpiration() {
34-
return browserCacheExpiration;
33+
public int getExpires() {
34+
return expires;
3535
}
3636

37-
public void setBrowserCacheExpiration(int browserCacheExpiration) {
38-
this.browserCacheExpiration = browserCacheExpiration;
37+
public void setExpires(int expires) {
38+
this.expires = expires;
3939
}
4040

4141
public void updateModified(){
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
package org.code13k.heets.model.config.app;
22

33
public class CacheInfo {
4-
private int defaultBrowserCacheExpiration;
5-
private int defaultTtl;
4+
private int defaultExpires;
65

7-
public int getDefaultBrowserCacheExpiration() {
8-
return defaultBrowserCacheExpiration;
6+
public int getDefaultExpires() {
7+
return defaultExpires;
98
}
109

11-
public void setDefaultBrowserCacheExpiration(int defaultBrowserCacheExpiration) {
12-
this.defaultBrowserCacheExpiration = defaultBrowserCacheExpiration;
13-
}
14-
15-
public int getDefaultTtl() {
16-
return defaultTtl;
17-
}
18-
19-
public void setDefaultTtl(int defaultTtl) {
20-
this.defaultTtl = defaultTtl;
10+
public void setDefaultExpires(int defaultExpires) {
11+
this.defaultExpires = defaultExpires;
2112
}
2213
}

src/main/java/org/code13k/heets/service/get/GetHttpServer.java

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ private void logging(HttpServerOptions httpServerOptions, Router router) {
9191
private void setRouter(Router router) {
9292
// GET /*
9393
router.route().method(HttpMethod.GET).path("/*").handler(routingContext -> {
94-
routingContext.request().bodyHandler(new Handler<Buffer>() {
94+
routingContext.request().endHandler(new Handler<Void>() {
9595
@Override
96-
public void handle(Buffer event) {
96+
public void handle(Void event) {
9797
// Key
9898
final String path = routingContext.request().uri();
9999
if (StringUtils.isEmpty(path) == true) {
@@ -137,16 +137,22 @@ private void response(RoutingContext routingContext, int statusCode, String mess
137137
* Response HTTP data
138138
*/
139139
private void sendData(RoutingContext routingContext, CacheData cacheData) {
140-
/**
141-
* Content Type
142-
*/
143-
if (StringUtils.isEmpty(cacheData.getContentType()) == false) {
144-
routingContext.response().putHeader(HttpHeaderNames.CONTENT_TYPE, cacheData.getContentType());
145-
}
146-
147-
/**
148-
* Check to modify
149-
*/
140+
int currentTime = (int) (System.currentTimeMillis() / 1000);
141+
int lastModifiedTime = (int) (cacheData.getModified() / 1000);
142+
int expiresSeconds = cacheData.getExpires();
143+
int pastSeconds = currentTime - lastModifiedTime;
144+
int maxAgeSeconds = Math.max(0, expiresSeconds - pastSeconds);
145+
int expiresTime = currentTime + maxAgeSeconds;
146+
147+
// Log
148+
mLogger.trace("currentTime = " + currentTime);
149+
mLogger.trace("lastModifiedTime = " + lastModifiedTime);
150+
mLogger.trace("expiresSeconds = " + expiresSeconds);
151+
mLogger.trace("pastSeconds = " + pastSeconds);
152+
mLogger.trace("maxAgeSeconds = " + maxAgeSeconds);
153+
mLogger.trace("expiresTime = " + expiresTime);
154+
155+
// Check to modify
150156
boolean isModified = false;
151157
final String headerIfModifiedSince = routingContext.request().headers().get(HttpHeaderNames.IF_MODIFIED_SINCE);
152158
if (StringUtils.isEmpty(headerIfModifiedSince)) {
@@ -163,34 +169,45 @@ private void sendData(RoutingContext routingContext, CacheData cacheData) {
163169
}
164170
}
165171

166-
/**
167-
* Not Modified (304)
168-
*/
169-
if (isModified == false) {
170-
routingContext.response().setStatusCode(304).end();
171-
return;
172-
}
172+
// Cache-Control
173+
routingContext.response().putHeader(HttpHeaderNames.CACHE_CONTROL, "public, max-age=" + maxAgeSeconds);
173174

174-
/**
175-
* Modified
176-
*/
175+
// Expires
177176
SimpleDateFormat expiresFormat = new SimpleDateFormat(DATE_FORMAT);
178-
SimpleDateFormat lastModifiedFormat = new SimpleDateFormat(DATE_FORMAT);
179177
expiresFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
180-
lastModifiedFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
181-
Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
182-
calendar.add(Calendar.SECOND, cacheData.getBrowserCacheExpiration());
183-
String expires = expiresFormat.format(calendar.getTime());
184-
String lastModified = lastModifiedFormat.format(cacheData.getModified());
185-
186-
// Put Header
187-
routingContext.response().putHeader(HttpHeaderNames.CACHE_CONTROL, "public, max-age=" + cacheData.getBrowserCacheExpiration());
188-
routingContext.response().putHeader(HttpHeaderNames.EXPIRES, expires);
189-
routingContext.response().putHeader(HttpHeaderNames.LAST_MODIFIED, lastModified);
190-
191-
/**
192-
* END
193-
*/
194-
routingContext.response().end(cacheData.getValue());
178+
String expiresString = expiresFormat.format(expiresTime * 1000L);
179+
routingContext.response().putHeader(HttpHeaderNames.EXPIRES, expiresString);
180+
181+
// Last-Modified
182+
if (isModified == true) {
183+
SimpleDateFormat lastModifiedFormat = new SimpleDateFormat(DATE_FORMAT);
184+
lastModifiedFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
185+
String lastModifiedString = lastModifiedFormat.format(lastModifiedTime * 1000L);
186+
routingContext.response().putHeader(HttpHeaderNames.LAST_MODIFIED, lastModifiedString);
187+
}
188+
189+
// Content-Type
190+
if (StringUtils.isEmpty(cacheData.getContentType()) == false) {
191+
routingContext.response().putHeader(HttpHeaderNames.CONTENT_TYPE, cacheData.getContentType());
192+
}
193+
194+
// Date
195+
SimpleDateFormat currentDateFormat = new SimpleDateFormat(DATE_FORMAT);
196+
currentDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
197+
String currentDateString = currentDateFormat.format(currentTime * 1000L);
198+
routingContext.response().putHeader(HttpHeaderNames.DATE, currentDateString);
199+
200+
// Not Modified (304)
201+
if (isModified == false) {
202+
String statusMessage = "Not Modified";
203+
routingContext.response().setStatusCode(304).setStatusMessage(statusMessage).end(statusMessage);
204+
}
205+
206+
// OK (200)
207+
else {
208+
routingContext.response().putHeader(HttpHeaderNames.ACCEPT_RANGES, "bytes");
209+
routingContext.response().putHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
210+
routingContext.response().end(cacheData.getValue());
211+
}
195212
}
196213
}

src/main/java/org/code13k/heets/service/set/SetHttpServer.java

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public class SetHttpServer extends AbstractVerticle {
2828

2929
// Const
3030
public static final int PORT = AppConfig.getInstance().getPort().getSetHttp();
31-
public static final int DEFAULT_BROWSER_CACHE_EXPIRATION = AppConfig.getInstance().getCache().getDefaultBrowserCacheExpiration();
32-
public static final int DEFAULT_TTL_SECONDS = AppConfig.getInstance().getCache().getDefaultTtl();
31+
public static final int DEFAULT_EXPIRES = AppConfig.getInstance().getCache().getDefaultExpires();
3332

3433

3534
/**
@@ -137,24 +136,16 @@ public void handle(Buffer event) {
137136
// Nothing
138137
}
139138

140-
// BrowserCacheExpiration
141-
int browserCacheExpiration = DEFAULT_BROWSER_CACHE_EXPIRATION;
139+
// Expires
140+
int expires = DEFAULT_EXPIRES;
142141
try {
143-
browserCacheExpiration = jsonObject.get("browser_cache_expiration").getAsInt();
144-
} catch (Exception e) {
145-
// Nothing
146-
}
147-
148-
// TTL
149-
long ttl = DEFAULT_TTL_SECONDS;
150-
try {
151-
ttl = jsonObject.get("ttl").getAsLong();
142+
expires = jsonObject.get("expires").getAsInt();
152143
} catch (Exception e) {
153144
// Nothing
154145
}
155146

156147
// Handle
157-
handleRequest(routingContext, key, value, contentType, browserCacheExpiration, ttl);
148+
handleRequest(routingContext, key, value, contentType, expires);
158149
}
159150
});
160151
}
@@ -199,26 +190,17 @@ public void handle(Buffer event) {
199190
// Nothing
200191
}
201192

202-
// BrowserCacheExpiration
203-
int browserCacheExpiration = DEFAULT_BROWSER_CACHE_EXPIRATION;
193+
// Expires
194+
int expires = DEFAULT_EXPIRES;
204195
try {
205-
String temp = param.get("browser_cache_expiration");
206-
browserCacheExpiration = Integer.parseInt(temp);
207-
} catch (Exception e) {
208-
// Nothing
209-
}
210-
211-
// TTL
212-
long ttl = DEFAULT_TTL_SECONDS;
213-
try {
214-
String temp = param.get("ttl");
215-
ttl = Long.parseLong(temp);
196+
String temp = param.get("expires");
197+
expires = Integer.parseInt(temp);
216198
} catch (Exception e) {
217199
// Nothing
218200
}
219201

220202
// Handle
221-
handleRequest(routingContext, key, value, contentType, browserCacheExpiration, ttl);
203+
handleRequest(routingContext, key, value, contentType, expires);
222204
}
223205
});
224206
}
@@ -267,26 +249,17 @@ public void handle(Void event) {
267249
// Nothing
268250
}
269251

270-
// BrowserCacheExpiration
271-
int browserCacheExpiration = DEFAULT_BROWSER_CACHE_EXPIRATION;
272-
try {
273-
String temp = form.get("browser_cache_expiration");
274-
browserCacheExpiration = Integer.parseInt(temp);
275-
} catch (Exception e) {
276-
// Nothing
277-
}
278-
279-
// TTL
280-
long ttl = DEFAULT_TTL_SECONDS;
252+
// Expires
253+
int expires = DEFAULT_EXPIRES;
281254
try {
282-
String temp = form.get("ttl");
283-
ttl = Long.parseLong(temp);
255+
String temp = form.get("expires");
256+
expires = Integer.parseInt(temp);
284257
} catch (Exception e) {
285258
// Nothing
286259
}
287260

288261
// Handle
289-
handleRequest(routingContext, key, value, contentType, browserCacheExpiration, ttl);
262+
handleRequest(routingContext, key, value, contentType, expires);
290263
}
291264
});
292265
} else {
@@ -302,17 +275,16 @@ public void handle(Void event) {
302275
/**
303276
* Handle request
304277
*/
305-
private void handleRequest(RoutingContext routingContext, String key, String value, String contentType, int browserCacheExpiration, long ttl) {
278+
private void handleRequest(RoutingContext routingContext, String key, String value, String contentType, int expires) {
306279
// Log
307280
mLogger.trace("key = " + key);
308281
mLogger.trace("value = " + value);
309282
mLogger.trace("contentType = " + contentType);
310-
mLogger.trace("browserCacheExpiration = " + browserCacheExpiration);
311-
mLogger.trace("ttl = " + ttl);
283+
mLogger.trace("expires = " + expires);
312284

313285
// Process
314-
CacheData cacheData = new CacheData(value, contentType, browserCacheExpiration);
315-
ClusteredCache.getInstance().set(key, cacheData, ttl, new Consumer<Boolean>() {
286+
CacheData cacheData = new CacheData(value, contentType, expires);
287+
ClusteredCache.getInstance().set(key, cacheData, new Consumer<Boolean>() {
316288
@Override
317289
public void accept(Boolean result) {
318290
if (result == true) {

src/main/resources/config/default_app_config.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ port:
66

77
# Cache
88
cache:
9-
default_browser_cache_expiration: 0
10-
default_ttl: 60
9+
default_expires: 60
1110

1211
# Cluster
1312
cluster:

0 commit comments

Comments
 (0)