diff --git a/ma-impl.sublime-workspace b/ma-impl.sublime-workspace index 44cda50b3a9b778e0a38b047e8bcb0839aa39c1e..0a9c9a9b0229e6cdc8a4c459bc0da93f5ffd99b5 100644 --- a/ma-impl.sublime-workspace +++ b/ma-impl.sublime-workspace @@ -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": { diff --git a/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java b/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java index 26cfc36e8a67f5924793743ad04323b7d0f78d31..e3c5d06aa49607cc2c3cf7990dc7f7e33b0e88dd 100644 --- a/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java +++ b/vipra-rest/src/main/java/de/vipra/rest/resource/ArticleResource.java @@ -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; + } + } diff --git a/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java b/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java index 3d4a05979986abdfee303f960914f9c3ca1db928..df56b8c3b2273caaf7eb884ba7c8bee123998b3a 100644 --- a/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java +++ b/vipra-rest/src/main/java/de/vipra/rest/resource/TopicResource.java @@ -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; + } + }