]> source.dussan.org Git - gitblit.git/commitdiff
Cache and re-use project markdown content (issue-172)
authorJames Moger <james.moger@gitblit.com>
Thu, 3 Jan 2013 22:22:21 +0000 (17:22 -0500)
committerJames Moger <james.moger@gitblit.com>
Thu, 3 Jan 2013 22:22:21 +0000 (17:22 -0500)
src/com/gitblit/GitBlit.java
src/com/gitblit/models/ProjectModel.java
src/com/gitblit/wicket/pages/ProjectPage.java

index 96333a07e242c01f62ca4a4b194972f25fd37386..30071bb680148e186b0b7cfaefb3e6a943812a70 100644 (file)
@@ -154,6 +154,10 @@ public class GitBlit implements ServletContextListener {
        private final Map<String, ProjectModel> projectCache = new ConcurrentHashMap<String, ProjectModel>();\r
        \r
        private final AtomicReference<String> repositoryListSettingsChecksum = new AtomicReference<String>("");\r
+       \r
+       private final ObjectCache<String> projectMarkdownCache = new ObjectCache<String>();\r
+       \r
+       private final ObjectCache<String> projectRepositoriesMarkdownCache = new ObjectCache<String>();\r
 \r
        private ServletContext servletContext;\r
 \r
@@ -1402,7 +1406,30 @@ public class GitBlit implements ServletContextListener {
                                }\r
                                project.title = projectConfigs.getString("project", name, "title");\r
                                project.description = projectConfigs.getString("project", name, "description");\r
-                               configs.put(name.toLowerCase(), project);                               \r
+                               \r
+                               // project markdown\r
+                               File pmkd = new File(getRepositoriesFolder(), (project.isRoot ? "" : name) + "/project.mkd");\r
+                               if (pmkd.exists()) {\r
+                                       Date lm = new Date(pmkd.lastModified());\r
+                                       if (!projectMarkdownCache.hasCurrent(name, lm)) {\r
+                                               String mkd = com.gitblit.utils.FileUtils.readContent(pmkd,  "\n");\r
+                                               projectMarkdownCache.updateObject(name, lm, mkd);\r
+                                       }\r
+                                       project.projectMarkdown = projectMarkdownCache.getObject(name);\r
+                               }\r
+                               \r
+                               // project repositories markdown\r
+                               File rmkd = new File(getRepositoriesFolder(), (project.isRoot ? "" : name) + "/repositories.mkd");\r
+                               if (rmkd.exists()) {\r
+                                       Date lm = new Date(rmkd.lastModified());\r
+                                       if (!projectRepositoriesMarkdownCache.hasCurrent(name, lm)) {\r
+                                               String mkd = com.gitblit.utils.FileUtils.readContent(rmkd,  "\n");\r
+                                               projectRepositoriesMarkdownCache.updateObject(name, lm, mkd);\r
+                                       }\r
+                                       project.repositoriesMarkdown = projectRepositoriesMarkdownCache.getObject(name);\r
+                               }\r
+                               \r
+                               configs.put(name.toLowerCase(), project);\r
                        }\r
                        projectCache.clear();\r
                        projectCache.putAll(configs);\r
index 189a409b5b5c531ea2eab2aaf5eeabaa75326f48..9e5d523378876f2a9e9369b23f7654e17ba2f706 100644 (file)
@@ -39,6 +39,8 @@ public class ProjectModel implements Serializable, Comparable<ProjectModel> {
        public String description;\r
        public final Set<String> repositories = new HashSet<String>();\r
        \r
+       public String projectMarkdown;\r
+       public String repositoriesMarkdown;\r
        public Date lastChange;\r
        public final boolean isRoot;\r
 \r
index e10ca900374aea4cd4af51d37ebcae29caefb82b..7eba03315b467c9817d36063f61314875535c8d9 100644 (file)
@@ -15,9 +15,6 @@
  */\r
 package com.gitblit.wicket.pages;\r
 \r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.InputStreamReader;\r
 import java.text.MessageFormat;\r
 import java.text.SimpleDateFormat;\r
 import java.util.ArrayList;\r
@@ -37,7 +34,6 @@ import org.apache.wicket.markup.html.link.ExternalLink;
 import org.apache.wicket.markup.repeater.Item;\r
 import org.apache.wicket.markup.repeater.data.DataView;\r
 import org.apache.wicket.markup.repeater.data.ListDataProvider;\r
-import org.eclipse.jgit.lib.Constants;\r
 \r
 import com.gitblit.GitBlit;\r
 import com.gitblit.Keys;\r
@@ -46,7 +42,6 @@ import com.gitblit.models.Activity;
 import com.gitblit.models.Metric;\r
 import com.gitblit.models.ProjectModel;\r
 import com.gitblit.models.RepositoryModel;\r
-import com.gitblit.models.UserModel;\r
 import com.gitblit.utils.ActivityUtils;\r
 import com.gitblit.utils.MarkdownUtils;\r
 import com.gitblit.utils.StringUtils;\r
@@ -111,23 +106,14 @@ public class ProjectPage extends RootPage {
                add(WicketUtils.syndicationDiscoveryLink(SyndicationServlet.getTitle(project.getDisplayName(),\r
                                null), feedLink));\r
                \r
-               final String projectPath;\r
-               if (project.isRoot) {\r
-                       projectPath = "";\r
-               } else {\r
-                       projectPath = projectName + "/";\r
-               }\r
-               \r
                // project markdown message\r
-               File pmkd = new File(GitBlit.getRepositoriesFolder(),  projectPath + "project.mkd");\r
-               String pmessage = readMarkdown(projectName, pmkd);\r
+               String pmessage = transformMarkdown(project.projectMarkdown);\r
                Component projectMessage = new Label("projectMessage", pmessage)\r
                                .setEscapeModelStrings(false).setVisible(pmessage.length() > 0);\r
                add(projectMessage);\r
 \r
                // markdown message above repositories list\r
-               File rmkd = new File(GitBlit.getRepositoriesFolder(),  projectPath + "repositories.mkd");\r
-               String rmessage = readMarkdown(projectName, rmkd);\r
+               String rmessage = transformMarkdown(project.repositoriesMarkdown);\r
                Component repositoriesMessage = new Label("repositoriesMessage", rmessage)\r
                                .setEscapeModelStrings(false).setVisible(rmessage.length() > 0);\r
                add(repositoriesMessage);\r
@@ -352,20 +338,15 @@ public class ProjectPage extends RootPage {
                }\r
                return menu;\r
        }\r
-\r
-\r
-       private String readMarkdown(String projectName, File projectMessage) {\r
+       \r
+       private String transformMarkdown(String markdown) {\r
                String message = "";\r
-               if (projectMessage.exists()) {\r
+               if (!StringUtils.isEmpty(markdown)) {\r
                        // Read user-supplied message\r
                        try {\r
-                               FileInputStream fis = new FileInputStream(projectMessage);\r
-                               InputStreamReader reader = new InputStreamReader(fis,\r
-                                               Constants.CHARACTER_ENCODING);\r
-                               message = MarkdownUtils.transformMarkdown(reader);\r
-                               reader.close();\r
+                               message = MarkdownUtils.transformMarkdown(markdown);\r
                        } catch (Throwable t) {\r
-                               message = getString("gb.failedToRead") + " " + projectMessage;\r
+                               message = getString("gb.failedToRead") + " " + markdown;\r
                                warn(message, t);\r
                        }\r
                }\r