Skip to content

Commit 7a6af4e

Browse files
committed
added simple LRU-based in memory cache
1 parent f321d56 commit 7a6af4e

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.ioexception.www.server.cache;
2+
3+
public interface EntityCacheEntry
4+
{
5+
byte[] getEntity();
6+
String getETag();
7+
String getContentType();
8+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package de.ioexception.www.server.cache.impl;
2+
3+
import de.ioexception.www.server.cache.EntityCacheEntry;
4+
5+
public class EntityCacheEntryImpl implements EntityCacheEntry
6+
{
7+
private final byte[] entity;
8+
private final String eTag;
9+
private final String contentType;
10+
11+
public EntityCacheEntryImpl(byte[] entity, String eTag, String contentType)
12+
{
13+
super();
14+
this.entity = entity;
15+
this.eTag = eTag;
16+
this.contentType = contentType;
17+
}
18+
19+
@Override
20+
public byte[] getEntity()
21+
{
22+
return entity;
23+
}
24+
25+
@Override
26+
public String getETag()
27+
{
28+
return eTag;
29+
}
30+
31+
@Override
32+
public String getContentType()
33+
{
34+
return contentType;
35+
}
36+
37+
}

src/de/ioexception/www/server/impl/BasicHttpServer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.util.concurrent.Executors;
99

1010
import de.ioexception.www.server.HttpServer;
11+
import de.ioexception.www.server.cache.Cache;
12+
import de.ioexception.www.server.cache.EntityCacheEntry;
13+
import de.ioexception.www.server.cache.impl.LRUCache;
1114

1215
/**
1316
* A simple HTTP server implementation.
@@ -27,6 +30,8 @@ public class BasicHttpServer implements HttpServer
2730
private final ExecutorService workerPool;
2831
private final ExecutorService dispatcherService;
2932
private final ServerSocket serverSocket;
33+
34+
private final Cache<String, EntityCacheEntry> cache = new LRUCache<String, EntityCacheEntry>(100);
3035

3136

3237
/**
@@ -62,7 +67,8 @@ public BasicHttpServer(int port)
6267
@Override
6368
public void dispatchRequest(Socket socket)
6469
{
65-
workerPool.submit(new BasicHttpWorker(socket, this));
70+
// workerPool.submit(new BasicHttpWorker(socket, this));
71+
workerPool.submit(new CachingHttpWorker(socket, this,cache));
6672
}
6773

6874
@Override
Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
11
package de.ioexception.www.server.impl;
22

33
import java.net.Socket;
4+
import java.util.HashMap;
45

6+
import de.ioexception.www.Http;
57
import de.ioexception.www.http.HttpRequest;
68
import de.ioexception.www.http.HttpResponse;
9+
import de.ioexception.www.http.HttpStatusCode;
10+
import de.ioexception.www.http.impl.BasicHttpResponse;
11+
import de.ioexception.www.server.cache.Cache;
12+
import de.ioexception.www.server.cache.EntityCacheEntry;
13+
import de.ioexception.www.server.cache.impl.EntityCacheEntryImpl;
714

815
public class CachingHttpWorker extends BasicHttpWorker
916
{
17+
private final Cache<String, EntityCacheEntry> cache;
1018

11-
public CachingHttpWorker(Socket socket, BasicHttpServer server)
19+
public CachingHttpWorker(Socket socket, BasicHttpServer server, Cache<String, EntityCacheEntry> cache)
1220
{
1321
super(socket, server);
22+
this.cache = cache;
1423
}
15-
24+
1625
@Override
1726
protected HttpResponse handleRequest(HttpRequest request)
1827
{
19-
// TODO: add caching support
20-
return super.handleRequest(request);
28+
EntityCacheEntry cacheEntry = cache.get(request.getRequestUri());
29+
30+
if(null == cacheEntry)
31+
{
32+
HttpResponse response = super.handleRequest(request);
33+
if(response.getStatusCode().equals(HttpStatusCode.OK) && response.getEntity() != null && response.getEntity().length > 0)
34+
{
35+
EntityCacheEntry entry = new EntityCacheEntryImpl(response.getEntity(), response.getHeaders().get(Http.ETAG), response.getHeaders().get(Http.CONTENT_TYPE));
36+
cache.put(request.getRequestUri(), entry);
37+
}
38+
return response;
39+
}
40+
else
41+
{
42+
//TODO check for conditional request
43+
BasicHttpResponse response = new BasicHttpResponse();
44+
response.setHeaders(new HashMap<String, String>());
45+
response.getHeaders().put(Http.SERVER, server.getServerSignature());
46+
response.setVersion(request.getHttpVersion());
47+
response.getHeaders().put(Http.CONTENT_LENGTH, ""+cacheEntry.getEntity().length);
48+
if(null != cacheEntry.getETag())
49+
{
50+
response.getHeaders().put(Http.ETAG, cacheEntry.getETag());
51+
}
52+
if(null != cacheEntry.getContentType())
53+
{
54+
response.getHeaders().put(Http.CONTENT_TYPE, cacheEntry.getContentType());
55+
}
56+
response.setEntity(cacheEntry.getEntity());
57+
response.setStatusCode(HttpStatusCode.OK);
58+
return response;
59+
}
2160
}
2261

2362
}

0 commit comments

Comments
 (0)