Skip to content
Snippets Groups Projects
Commit 70223186 authored by Eike Cochu's avatar Eike Cochu
Browse files

added topic to object caching

parent 39a82023
No related branches found
No related tags found
No related merge requests found
......@@ -475,6 +475,7 @@
],
"file_history":
[
"/home/eike/Downloads/FRITZ.Box 7490 113.06.30_17.01.16_2147.export",
"/home/eike/repos/master/ma-impl/vm/data/test-1.json",
"/home/eike/repos/master/ma-impl/vm/data/test-2.json",
"/home/eike/.local/share/vipra/jgibb/jgibb.twords",
......@@ -601,8 +602,7 @@
"/home/eike/Repositories/fu/ss15/ma/impl/vm/config/initd-tomcat",
"/home/eike/Repositories/fu/ss15/ma/impl/vm/config/environment",
"/home/eike/Repositories/fu/ss15/ma/impl/tmbs-frontend/app/templates/articles.hbs",
"/home/eike/Repositories/fu/ss15/ma/impl/tmbs-frontend/app/templates/application.hbs",
"/home/eike/Repositories/fu/ss15/ma/impl/vm/config/disable-transparent-hugepages"
"/home/eike/Repositories/fu/ss15/ma/impl/tmbs-frontend/app/templates/application.hbs"
],
"find":
{
......
......@@ -78,15 +78,8 @@ public class ArticleResource {
return Response.status(Response.Status.BAD_REQUEST).entity(res).build();
}
// caching
Article article = articleCache.get(id);
if (article == null) {
article = service.getSingle(id);
if (article != null)
articleCache.put(id, article);
}
Article article = getSingle(id);
// checking
if (article != null) {
res.setData(article);
return Response.ok().entity(res).tag(res.tag()).build();
......@@ -163,9 +156,20 @@ public class ArticleResource {
@Produces(APIMediaType.APPLICATION_JSONAPI)
@Path("{id}")
public Response updateArticle(@PathParam("id") String id, Wrapper<Article> wrapper) {
Article article = wrapper.getData();
Article newArticle = wrapper.getData();
Article article = getSingle(id);
// TODO implement
return null;
}
private Article getSingle(String id) {
Article article = articleCache.get(id);
if (article == null) {
article = service.getSingle(id);
if (article != null)
articleCache.put(id, article);
}
return article;
}
}
......@@ -16,6 +16,10 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.CacheConfigurationBuilder;
import de.vipra.rest.APIMediaType;
import de.vipra.rest.Messages;
import de.vipra.rest.PATCH;
......@@ -26,6 +30,7 @@ import de.vipra.util.Config;
import de.vipra.util.Mongo;
import de.vipra.util.ex.ConfigException;
import de.vipra.util.ex.DatabaseException;
import de.vipra.util.model.Article;
import de.vipra.util.model.Topic;
@Path("topics")
......@@ -34,12 +39,20 @@ public class TopicResource {
@Context
UriInfo uri;
Cache<String, Topic> topicCache;
TopicService service;
public TopicResource(@Context ServletContext servletContext) throws ConfigException, IOException {
Config config = Config.getConfig();
Mongo mongo = Mongo.getInstance(config);
service = new TopicService(mongo);
CacheManager manager = (CacheManager) servletContext.getAttribute("cachemanager");
topicCache = manager.getCache("topiccache", String.class, Topic.class);
if (topicCache == null)
topicCache = manager.createCache("topiccache",
CacheConfigurationBuilder.newCacheConfigurationBuilder().buildConfig(String.class, Topic.class));
}
@GET
......@@ -62,7 +75,9 @@ public class TopicResource {
String.format(Messages.BAD_REQUEST, "id cannot be empty")));
return Response.status(Response.Status.BAD_REQUEST).entity(res).build();
}
Topic topic = service.getSingle(id);
Topic topic = getSingle(id);
if (topic != null) {
res.setData(topic);
return Response.ok().entity(res).tag(res.tag()).build();
......@@ -82,6 +97,7 @@ public class TopicResource {
Wrapper<Topic> res = new Wrapper<>();
try {
service.updateSingle(topic);
topicCache.put(id, topic);
res.setData(topic);
return Response.ok().entity(res).tag(res.tag()).build();
} catch (DatabaseException e) {
......@@ -96,9 +112,20 @@ public class TopicResource {
@Produces(APIMediaType.APPLICATION_JSONAPI)
@Path("{id}")
public Response updateTopic(@PathParam("id") String id, Wrapper<Topic> wrapper) {
Topic topic = wrapper.getData();
Topic newTopic = wrapper.getData();
Topic topic = getSingle(id);
// TODO implement
return null;
}
private Topic getSingle(String id) {
Topic topic = topicCache.get(id);
if (topic == null) {
topic = service.getSingle(id);
if (topic != null)
topicCache.put(id, topic);
}
return topic;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment