import com.gitblit.models.FederationProposal;\r
import com.gitblit.models.FederationSet;\r
import com.gitblit.models.Metric;\r
-import com.gitblit.models.ObjectCache;\r
import com.gitblit.models.RepositoryModel;\r
import com.gitblit.models.ServerSettings;\r
import com.gitblit.models.ServerStatus;\r
import com.gitblit.utils.JGitUtils;\r
import com.gitblit.utils.JsonUtils;\r
import com.gitblit.utils.MetricUtils;\r
+import com.gitblit.utils.ObjectCache;\r
import com.gitblit.utils.StringUtils;\r
\r
/**\r
+++ /dev/null
-/*\r
- * Copyright 2011 gitblit.com.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package com.gitblit.models;\r
-\r
-import java.io.Serializable;\r
-import java.util.Date;\r
-import java.util.Map;\r
-import java.util.concurrent.ConcurrentHashMap;\r
-\r
-/**\r
- * Reusable object cache.\r
- * \r
- * @author James Moger\r
- * \r
- */\r
-public class ObjectCache<X> implements Serializable {\r
-\r
- private static final long serialVersionUID = 1L;\r
-\r
- private final Map<String, CachedObject<X>> cache = new ConcurrentHashMap<String, CachedObject<X>>();\r
-\r
- private class CachedObject<Y> {\r
-\r
- public final String name;\r
-\r
- private volatile Date date;\r
-\r
- private volatile Y object;\r
-\r
- CachedObject(String name) {\r
- this.name = name;\r
- date = new Date(0);\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return getClass().getSimpleName() + ": " + name;\r
- }\r
- }\r
-\r
- public boolean hasCurrent(String name, Date date) {\r
- return cache.containsKey(name) && cache.get(name).date.compareTo(date) == 0;\r
- }\r
-\r
- public Date getDate(String name) {\r
- return cache.get(name).date;\r
- }\r
-\r
- public X getObject(String name) {\r
- return cache.get(name).object;\r
- }\r
-\r
- public void updateObject(String name, X object) {\r
- this.updateObject(name, new Date(), object);\r
- }\r
-\r
- public void updateObject(String name, Date date, X object) {\r
- CachedObject<X> obj;\r
- if (cache.containsKey(name)) {\r
- obj = cache.get(name);\r
- } else {\r
- obj = new CachedObject<X>(name);\r
- cache.put(name, obj);\r
- }\r
- obj.date = date;\r
- obj.object = object;\r
- }\r
-\r
- public Object remove(String name) {\r
- if (cache.containsKey(name)) {\r
- return cache.remove(name).object;\r
- }\r
- return null;\r
- }\r
-}\r
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.utils;\r
+\r
+import java.io.Serializable;\r
+import java.util.Date;\r
+import java.util.Map;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+/**\r
+ * Reusable coarse date-based object cache. The date precision is in\r
+ * milliseconds and in fast, concurrent systems this cache is too simplistic.\r
+ * However, for the cases where its being used in Gitblit this cache technique\r
+ * is just fine.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class ObjectCache<X> implements Serializable {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ private final Map<String, CachedObject<X>> cache = new ConcurrentHashMap<String, CachedObject<X>>();\r
+\r
+ private class CachedObject<Y> {\r
+\r
+ public final String name;\r
+\r
+ private volatile Date date;\r
+\r
+ private volatile Y object;\r
+\r
+ CachedObject(String name) {\r
+ this.name = name;\r
+ date = new Date(0);\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return getClass().getSimpleName() + ": " + name;\r
+ }\r
+ }\r
+\r
+ public boolean hasCurrent(String name, Date date) {\r
+ return cache.containsKey(name) && cache.get(name).date.compareTo(date) == 0;\r
+ }\r
+\r
+ public Date getDate(String name) {\r
+ return cache.get(name).date;\r
+ }\r
+\r
+ public X getObject(String name) {\r
+ if (cache.containsKey(name)) {\r
+ return cache.get(name).object;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ public void updateObject(String name, X object) {\r
+ this.updateObject(name, new Date(), object);\r
+ }\r
+\r
+ public void updateObject(String name, Date date, X object) {\r
+ CachedObject<X> obj;\r
+ if (cache.containsKey(name)) {\r
+ obj = cache.get(name);\r
+ } else {\r
+ obj = new CachedObject<X>(name);\r
+ cache.put(name, obj);\r
+ }\r
+ obj.date = date;\r
+ obj.object = object;\r
+ }\r
+\r
+ public Object remove(String name) {\r
+ if (cache.containsKey(name)) {\r
+ return cache.remove(name).object;\r
+ }\r
+ return null;\r
+ }\r
+}\r