| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble; |
| 33 | |
|
| 34 | |
import org.apache.commons.logging.Log; |
| 35 | |
import org.apache.commons.logging.LogFactory; |
| 36 | |
|
| 37 | |
import java.util.Date; |
| 38 | |
import java.util.Properties; |
| 39 | |
import java.io.InputStream; |
| 40 | |
import java.io.IOException; |
| 41 | |
import java.net.URL; |
| 42 | |
|
| 43 | |
import net.sourceforge.pebble.util.RelativeDate; |
| 44 | |
import net.sourceforge.pebble.domain.*; |
| 45 | |
import net.sf.ehcache.Element; |
| 46 | |
import net.sf.ehcache.Cache; |
| 47 | |
import net.sf.ehcache.CacheManager; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public class ContentCache { |
| 55 | |
|
| 56 | 4 | private static final ContentCache instance = new ContentCache(); |
| 57 | |
|
| 58 | |
|
| 59 | 4 | private static Log log = LogFactory.getLog(ContentCache.class); |
| 60 | |
|
| 61 | |
private Cache cache; |
| 62 | |
|
| 63 | 4 | private ContentCache() { |
| 64 | 4 | URL url = BlogService.class.getResource("/ehcache.xml"); |
| 65 | 4 | CacheManager cacheManager = new CacheManager(url); |
| 66 | 4 | cache = cacheManager.getCache("contentCache"); |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | 4 | cache.getCacheConfiguration().setMaxElementsInMemory(cache.getCacheConfiguration().getMaxElementsInMemory() * BlogManager.getInstance().getNumberOfBlogs()); |
| 73 | 4 | } |
| 74 | |
|
| 75 | |
public static ContentCache getInstance() { |
| 76 | 3200 | return instance; |
| 77 | |
} |
| 78 | |
|
| 79 | |
public synchronized void putBlogEntry(BlogEntry blogEntry) { |
| 80 | 608 | Element element = new Element(getCompositeKeyForBlogEntry(blogEntry), blogEntry); |
| 81 | 608 | cache.put(element); |
| 82 | 608 | } |
| 83 | |
|
| 84 | |
public synchronized BlogEntry getBlogEntry(Blog blog, String blogEntryId) { |
| 85 | 2132 | BlogEntry blogEntry = null; |
| 86 | 2132 | Element element = cache.get(getCompositeKeyForBlogEntry(blog, blogEntryId)); |
| 87 | 2132 | if (element != null) { |
| 88 | 852 | blogEntry = (BlogEntry)element.getValue(); |
| 89 | |
} |
| 90 | |
|
| 91 | 2132 | return blogEntry; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public synchronized void removeBlogEntry(BlogEntry blogEntry) { |
| 95 | 784 | cache.remove(getCompositeKeyForBlogEntry(blogEntry)); |
| 96 | 784 | } |
| 97 | |
|
| 98 | |
private String getCompositeKeyForBlogEntry(BlogEntry blogEntry) { |
| 99 | 1392 | return getCompositeKeyForBlogEntry(blogEntry.getBlog(), blogEntry.getId()); |
| 100 | |
} |
| 101 | |
|
| 102 | |
private String getCompositeKeyForBlogEntry(Blog blog, String blogEntryId) { |
| 103 | 3524 | return blog.getId() + "/blogEntry/" + blogEntryId; |
| 104 | |
} |
| 105 | |
|
| 106 | |
public synchronized void putStaticPage(StaticPage staticPage) { |
| 107 | 0 | Element element = new Element(getCompositeKeyForStaticPage(staticPage), staticPage); |
| 108 | 0 | cache.put(element); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public synchronized StaticPage getStaticPage(Blog blog, String staticPageId) { |
| 112 | 0 | StaticPage staticPage = null; |
| 113 | 0 | Element element = cache.get(getCompositeKeyForStaticPage(blog, staticPageId)); |
| 114 | 0 | if (element != null) { |
| 115 | 0 | staticPage = (StaticPage)element.getValue(); |
| 116 | |
} |
| 117 | |
|
| 118 | 0 | return staticPage; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public synchronized void removeStaticPage(StaticPage staticPage) { |
| 122 | 0 | cache.remove(getCompositeKeyForStaticPage(staticPage)); |
| 123 | 0 | } |
| 124 | |
|
| 125 | |
private String getCompositeKeyForStaticPage(StaticPage staticPage) { |
| 126 | 0 | return getCompositeKeyForStaticPage(staticPage.getBlog(), staticPage.getId()); |
| 127 | |
} |
| 128 | |
|
| 129 | |
private String getCompositeKeyForStaticPage(Blog blog, String staticPageId) { |
| 130 | 0 | return blog.getId() + "/staticPage/" + staticPageId; |
| 131 | |
} |
| 132 | |
|
| 133 | |
} |