KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cache > OSCache


1 //$Id: OSCache.java,v 1.9 2005/04/21 07:57:19 oneovthafew Exp $
2
package org.hibernate.cache;
3
4 import java.util.Map JavaDoc;
5
6 import com.opensymphony.oscache.base.NeedsRefreshException;
7 import com.opensymphony.oscache.general.GeneralCacheAdministrator;
8
9 /**
10  * @author <a HREF="mailto:m.bogaert@intrasoft.be">Mathias Bogaert</a>
11  */

12 public class OSCache implements Cache {
13
14     /**
15      * The OSCache 2.0 cache administrator.
16      */

17     private GeneralCacheAdministrator cache = new GeneralCacheAdministrator();
18
19     private final int refreshPeriod;
20     private final String JavaDoc cron;
21     private final String JavaDoc regionName;
22     
23     private String JavaDoc toString(Object JavaDoc key) {
24         return String.valueOf(key) + '.' + regionName;
25     }
26
27     public OSCache(int refreshPeriod, String JavaDoc cron, String JavaDoc region) {
28         this.refreshPeriod = refreshPeriod;
29         this.cron = cron;
30         this.regionName = region;
31     }
32
33     public void setCacheCapacity(int cacheCapacity) {
34         cache.setCacheCapacity(cacheCapacity);
35     }
36
37     public Object JavaDoc get(Object JavaDoc key) throws CacheException {
38         try {
39             return cache.getFromCache( toString(key), refreshPeriod, cron );
40         }
41         catch (NeedsRefreshException e) {
42             cache.cancelUpdate( toString(key) );
43             return null;
44         }
45     }
46
47     public Object JavaDoc read(Object JavaDoc key) throws CacheException {
48         return get(key);
49     }
50     
51     public void update(Object JavaDoc key, Object JavaDoc value) throws CacheException {
52         put(key, value);
53     }
54     
55     public void put(Object JavaDoc key, Object JavaDoc value) throws CacheException {
56         cache.putInCache( toString(key), value );
57     }
58
59     public void remove(Object JavaDoc key) throws CacheException {
60         cache.flushEntry( toString(key) );
61     }
62
63     public void clear() throws CacheException {
64         cache.flushAll();
65     }
66
67     public void destroy() throws CacheException {
68         cache.destroy();
69     }
70
71     public void lock(Object JavaDoc key) throws CacheException {
72         // local cache, so we use synchronization
73
}
74
75     public void unlock(Object JavaDoc key) throws CacheException {
76         // local cache, so we use synchronization
77
}
78
79     public long nextTimestamp() {
80         return Timestamper.next();
81     }
82
83     public int getTimeout() {
84         return Timestamper.ONE_MS * 60000; //ie. 60 seconds
85
}
86
87     public String JavaDoc getRegionName() {
88         return regionName;
89     }
90
91     public long getSizeInMemory() {
92         return -1;
93     }
94
95     public long getElementCountInMemory() {
96         return -1;
97     }
98
99     public long getElementCountOnDisk() {
100         return -1;
101     }
102
103     public Map JavaDoc toMap() {
104         throw new UnsupportedOperationException JavaDoc();
105     }
106
107     public String JavaDoc toString() {
108         return "OSCache(" + regionName + ')';
109     }
110
111 }
112
Popular Tags