001: package com.opensymphony.oscache.hibernate;
002:
003: import java.util.Map;
004:
005: import org.hibernate.cache.Cache;
006: import org.hibernate.cache.CacheException;
007: import org.hibernate.cache.Timestamper;
008:
009: import com.opensymphony.oscache.base.NeedsRefreshException;
010: import com.opensymphony.oscache.general.GeneralCacheAdministrator;
011:
012: /**
013: * Cache plugin for Hibernate 3.2 and OpenSymphony OSCache 2.4.
014: * <p/>
015: * The OSCache implementation assumes that identifiers have well-behaved <tt>toString()</tt> methods.
016: * This implementation <b>must</b> be threadsafe.
017: *
018: * @version $Revision:$
019: */
020: public class OSCache implements Cache {
021:
022: /** The OSCache 2.4 cache administrator. */
023: private GeneralCacheAdministrator cache;
024: private final int refreshPeriod;
025: private final String cron;
026: private final String regionName;
027: private final String[] regionGroups;
028:
029: public OSCache(GeneralCacheAdministrator cache, int refreshPeriod,
030: String cron, String region) {
031: this .cache = cache;
032: this .refreshPeriod = refreshPeriod;
033: this .cron = cron;
034: this .regionName = region;
035: this .regionGroups = new String[] { region };
036: }
037:
038: /**
039: * @see org.hibernate.cache.Cache#get(java.lang.Object)
040: */
041: public Object get(Object key) throws CacheException {
042: try {
043: return cache.getFromCache(toString(key), refreshPeriod,
044: cron);
045: } catch (NeedsRefreshException e) {
046: cache.cancelUpdate(toString(key));
047: return null;
048: }
049: }
050:
051: /**
052: * @see org.hibernate.cache.Cache#put(java.lang.Object, java.lang.Object)
053: */
054: public void put(Object key, Object value) throws CacheException {
055: cache.putInCache(toString(key), value, regionGroups);
056: }
057:
058: /**
059: * @see org.hibernate.cache.Cache#remove(java.lang.Object)
060: */
061: public void remove(Object key) throws CacheException {
062: cache.flushEntry(toString(key));
063: }
064:
065: /**
066: * @see org.hibernate.cache.Cache#clear()
067: */
068: public void clear() throws CacheException {
069: cache.flushGroup(regionName);
070: }
071:
072: /**
073: * @see org.hibernate.cache.Cache#destroy()
074: */
075: public void destroy() throws CacheException {
076: synchronized (cache) {
077: cache.destroy();
078: }
079: }
080:
081: /**
082: * @see org.hibernate.cache.Cache#lock(java.lang.Object)
083: */
084: public void lock(Object key) throws CacheException {
085: // local cache, so we use synchronization
086: }
087:
088: /**
089: * @see org.hibernate.cache.Cache#unlock(java.lang.Object)
090: */
091: public void unlock(Object key) throws CacheException {
092: // local cache, so we use synchronization
093: }
094:
095: /**
096: * @see org.hibernate.cache.Cache#nextTimestamp()
097: */
098: public long nextTimestamp() {
099: return Timestamper.next();
100: }
101:
102: /**
103: * @see org.hibernate.cache.Cache#getTimeout()
104: */
105: public int getTimeout() {
106: return Timestamper.ONE_MS * 60000; //ie. 60 seconds
107: }
108:
109: /**
110: * @see org.hibernate.cache.Cache#toMap()
111: */
112: public Map toMap() {
113: throw new UnsupportedOperationException();
114: }
115:
116: /**
117: * @see org.hibernate.cache.Cache#getElementCountOnDisk()
118: */
119: public long getElementCountOnDisk() {
120: return -1;
121: }
122:
123: /**
124: * @see org.hibernate.cache.Cache#getElementCountInMemory()
125: */
126: public long getElementCountInMemory() {
127: return -1;
128: }
129:
130: /**
131: * @see org.hibernate.cache.Cache#getSizeInMemory()
132: */
133: public long getSizeInMemory() {
134: return -1;
135: }
136:
137: /**
138: * @see org.hibernate.cache.Cache#getRegionName()
139: */
140: public String getRegionName() {
141: return regionName;
142: }
143:
144: /**
145: * @see org.hibernate.cache.Cache#update(java.lang.Object, java.lang.Object)
146: */
147: public void update(Object key, Object value) throws CacheException {
148: put(key, value);
149: }
150:
151: /**
152: * @see org.hibernate.cache.Cache#read(java.lang.Object)
153: */
154: public Object read(Object key) throws CacheException {
155: return get(key);
156: }
157:
158: private String toString(Object key) {
159: return String.valueOf(key) + "." + regionName;
160: }
161:
162: }
|