KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > website > ThemeCache


1 package org.roller.presentation.website;
2
3 import org.roller.util.LRUCache2;
4
5 /**
6  * Caches the Theme files to avoid repeated reading of the files from the
7  * harddrive.
8  *
9  * @author llavandowska
10  */

11 public class ThemeCache
12 {
13     private static ThemeCache INSTANCE = new ThemeCache();
14     private static String JavaDoc cacheName = "ThemeFiles";
15     /**
16      * How many objects to store in cache.
17      * @TODO Add configuration for maxObjects in theme cache
18      */

19     private static int maxObjects = 500;
20     
21     private static LRUCache2 cache = new LRUCache2(maxObjects, 30 * 60 * 1000);
22     
23     /**
24      * How long until an object in cache expires.
25      * @TODO Add configuration for theme cache timeout
26      */

27     private long expireInterval = 1000l*60*60*24; // 1 second * 1 min * 1 hr * 24 hours
28

29     /**
30      * Should the PreviewResourceLoader cache the Template files.
31      * @TODO Add configuration for enabling theme template caching
32      */

33     private static boolean cacheTemplateFiles = false;
34         
35     /** Private constructor to prevent outside instantiation **/
36     private ThemeCache() { }
37         
38     /**
39      *
40      */

41     public static ThemeCache getInstance()
42     {
43         return INSTANCE;
44     }
45     
46     /**
47      *
48      */

49     public String JavaDoc putIntoCache(String JavaDoc themeName, String JavaDoc fileName, String JavaDoc template)
50     {
51         if (cacheTemplateFiles)
52         {
53             cache.put(themeName+":"+fileName, template);
54         }
55         return template;
56
57     }
58     
59     /**
60      * Null will be returned if there is a problem or if caching is "turned
61      * off".
62      */

63     public String JavaDoc getFromCache(String JavaDoc themeName, String JavaDoc fileName)
64     {
65         if (!cacheTemplateFiles) return null;
66         return (String JavaDoc) cache.get(themeName + ":" + fileName);
67     }
68     
69     /**
70      *
71      */

72     public void removeFromCache(String JavaDoc themeName, String JavaDoc fileName)
73     {
74         if (!cacheTemplateFiles) return;
75         cache.purge( new String JavaDoc[] { themeName+":"+fileName } );
76     }
77
78     
79     /**
80      * The list of files in a Theme is cached as a String[], the key being the
81      * Theme location itself.
82      *
83      * @param themeDir
84      * @param fileNames
85      * @return String[]
86      */

87     public String JavaDoc[] setFileList(String JavaDoc themeDir, String JavaDoc[] fileNames)
88     {
89         if (cacheTemplateFiles)
90         {
91                 cache.put(themeDir, fileNames);
92         }
93         return fileNames;
94
95     }
96     
97     /**
98      * The list of files in a Theme is cached as a String[], the key being the
99      * Theme location itself. If caching is turned off this will return null.
100      *
101      * @param theme
102      * @return String[]
103      */

104     public String JavaDoc[] getFileList(String JavaDoc themeDir)
105     {
106         if (!cacheTemplateFiles) return null;
107         return (String JavaDoc[])cache.get(themeDir);
108     }
109 }
110
Popular Tags