Anonymous avatar Anonymous committed a66fe40

Hibernate 3.2 integration support

Comments (0)

Files changed (3)

src/java/com/opensymphony/oscache/hibernate/OSCache.java

+package com.opensymphony.oscache.hibernate;
+
+import java.util.Map;
+
+import org.hibernate.cache.Cache;
+import org.hibernate.cache.CacheException;
+import org.hibernate.cache.Timestamper;
+
+import com.opensymphony.oscache.base.NeedsRefreshException;
+import com.opensymphony.oscache.general.GeneralCacheAdministrator;
+
+/**
+ * Cache plugin for Hibernate 3.2 and OpenSymphony OSCache 2.4.
+ * <p/>
+ * The OSCache implementation assumes that identifiers have well-behaved <tt>toString()</tt> methods.
+ * This implementation <b>must</b> be threadsafe.
+ * 
+ * @version $Revision$
+ */
+public class OSCache implements Cache {
+    
+    /** The OSCache 2.4 cache administrator. */
+    private GeneralCacheAdministrator cache;
+    private final int refreshPeriod;
+    private final String cron;
+    private final String regionName;
+    private final String[] regionGroups;
+    
+    public OSCache(GeneralCacheAdministrator cache, int refreshPeriod, String cron, String region) {
+        this.cache = cache;
+        this.refreshPeriod = refreshPeriod;
+        this.cron = cron;
+        this.regionName = region;
+        this.regionGroups = new String[] {region};
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#get(java.lang.Object)
+     */
+    public Object get(Object key) throws CacheException {
+        try {
+            return cache.getFromCache( toString(key), refreshPeriod, cron );
+        }
+        catch (NeedsRefreshException e) {
+            cache.cancelUpdate( toString(key) );
+            return null;
+        }
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#put(java.lang.Object, java.lang.Object)
+     */
+    public void put(Object key, Object value) throws CacheException {
+        cache.putInCache( toString(key), value, regionGroups );
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#remove(java.lang.Object)
+     */
+    public void remove(Object key) throws CacheException {
+        cache.flushEntry( toString(key) );
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#clear()
+     */
+    public void clear() throws CacheException {
+        cache.flushGroup(regionName);
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#destroy()
+     */
+    public void destroy() throws CacheException {
+        synchronized (cache) {
+            cache.destroy();
+        }
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#lock(java.lang.Object)
+     */
+    public void lock(Object key) throws CacheException {
+        // local cache, so we use synchronization
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#unlock(java.lang.Object)
+     */
+    public void unlock(Object key) throws CacheException {
+        // local cache, so we use synchronization
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#nextTimestamp()
+     */
+    public long nextTimestamp() {
+        return Timestamper.next();
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#getTimeout()
+     */
+    public int getTimeout() {
+        return Timestamper.ONE_MS * 60000; //ie. 60 seconds
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#toMap()
+     */
+    public Map toMap() {
+        throw new UnsupportedOperationException();
+    }    
+
+    /**
+     * @see org.hibernate.cache.Cache#getElementCountOnDisk()
+     */
+    public long getElementCountOnDisk() {
+        return -1;
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#getElementCountInMemory()
+     */
+    public long getElementCountInMemory() {
+        return -1;
+    }
+    
+    /**
+     * @see org.hibernate.cache.Cache#getSizeInMemory()
+     */
+    public long getSizeInMemory() {
+        return -1;
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#getRegionName()
+     */
+    public String getRegionName() {
+        return regionName;
+    }
+
+    /**
+     * @see org.hibernate.cache.Cache#update(java.lang.Object, java.lang.Object)
+     */
+    public void update(Object key, Object value) throws CacheException {
+        put(key, value);
+    }    
+
+    /**
+     * @see org.hibernate.cache.Cache#read(java.lang.Object)
+     */
+    public Object read(Object key) throws CacheException {
+        return get(key);
+    }
+    
+    private String toString(Object key) {
+        return String.valueOf(key) + "." + regionName;
+    }
+
+}

src/java/com/opensymphony/oscache/hibernate/OSCacheProvider.java

+package com.opensymphony.oscache.hibernate;
+
+import java.util.Properties;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.hibernate.cache.Cache;
+import org.hibernate.cache.CacheException;
+import org.hibernate.cache.CacheProvider;
+import org.hibernate.cache.Timestamper;
+import org.hibernate.util.StringHelper;
+
+import com.opensymphony.oscache.base.CacheEntry;
+import com.opensymphony.oscache.base.Config;
+import com.opensymphony.oscache.general.GeneralCacheAdministrator;
+import com.opensymphony.oscache.util.StringUtil;
+
+/**
+ * Cache provider plugin for Hibernate 3.2 and OpenSymphony OSCache 2.4.
+ * <p/>
+ * This implementation assumes that identifiers have well-behaved <tt>toString()</tt> methods.
+ * <p/>
+ * To enable OSCache for Hibernate's second level cache add the following line to Hibernate's configuration e.g. <code>hibernate.cfg.xml</code>):
+ * <code>hibernate.cache.provider_class=com.opensymphony.oscache.hibernate.OSCacheProvider</code>
+ * To configure a different configuration file use the following parameter in the Hibernate's configuration:
+ * <code>com.opensymphony.oscache.configurationResourceName=[path to oscache-hibernate.properties]</code>
+ * 
+ * @version $Revision$
+ */
+public class OSCacheProvider implements CacheProvider {
+
+    private static final Log LOG = LogFactory.getLog(OSCacheProvider.class);
+
+    /** In the Hibernate system property you can specify the location of the oscache configuration file name. */
+    public static final String OSCACHE_CONFIGURATION_RESOURCE_NAME = "com.opensymphony.oscache.configurationResourceName";
+
+    /** The <tt>OSCache</tt> refresh period property suffix. */
+    public static final String OSCACHE_REFRESH_PERIOD = "refresh.period";
+
+    /** The <tt>OSCache</tt> CRON expression property suffix. */
+    public static final String OSCACHE_CRON = "cron";
+    
+    private static GeneralCacheAdministrator cache;
+
+    /**
+     * Builds a new {@link Cache} instance, and gets it's properties from the 
+     * GeneralCacheAdministrator {@link GeneralCacheAdministrator}
+     * which reads the properties file (<code>oscache.properties</code>) in the start method: 
+     * @see com.opensymphony.oscache.hibernate.OSCacheProvider#start(java.util.Properties)
+     *
+     * @param region the region of the cache
+     * @param properties not used
+     * @return the hibernate 2nd level cache
+     * @throws CacheException
+     * 
+     * @see org.hibernate.cache.CacheProvider#buildCache(java.lang.String, java.util.Properties)
+     */
+    public Cache buildCache(String region, Properties properties) throws CacheException {
+        if (cache != null) {
+            LOG.debug("building cache in OSCacheProvider...");
+            
+            String refreshPeriodString = cache.getProperty( StringHelper.qualify(region, OSCACHE_REFRESH_PERIOD) );
+            int refreshPeriod = refreshPeriodString==null ? CacheEntry.INDEFINITE_EXPIRY : Integer.parseInt( refreshPeriodString.trim() );
+            
+            String cron = cache.getProperty( StringHelper.qualify(region, OSCACHE_CRON) );
+            
+            return new OSCache(cache, refreshPeriod, cron, region);
+        }
+        throw new CacheException("OSCache was stopped or wasn't configured via method start.");
+    }
+
+    /**
+     * @see org.hibernate.cache.CacheProvider#nextTimestamp()
+     */
+    public long nextTimestamp() {
+        return Timestamper.next();
+    }
+
+    /**
+     * This method isn't documented in Hibernate:
+     * @see org.hibernate.cache.CacheProvider#isMinimalPutsEnabledByDefault()
+     */
+    public boolean isMinimalPutsEnabledByDefault() {
+        return false;
+    }
+
+    /**
+     * @see org.hibernate.cache.CacheProvider#stop()
+     */
+    public void stop() {
+        if (cache != null) {
+            LOG.debug("Stopping OSCacheProvider...");
+            cache.destroy();
+            cache = null;
+            LOG.debug("OSCacheProvider stopped.");
+        }
+    }
+
+    /**
+     * @see org.hibernate.cache.CacheProvider#start(java.util.Properties)
+     */
+    public void start(Properties hibernateSystemProperties) throws CacheException {
+        if (cache == null) {
+            // construct the cache
+            LOG.debug("Starting OSCacheProvider...");
+            String configResourceName = null;
+            if (hibernateSystemProperties != null) {
+                configResourceName = (String) hibernateSystemProperties.get(OSCACHE_CONFIGURATION_RESOURCE_NAME);
+            }
+            if (StringUtil.isEmpty(configResourceName)) {
+                cache = new GeneralCacheAdministrator();
+            } else {
+                Properties propertiesOSCache = Config.loadProperties(configResourceName, this.getClass().getName());
+                cache = new GeneralCacheAdministrator(propertiesOSCache);
+            }
+            LOG.debug("OSCacheProvider started.");
+        } else {
+            LOG.warn("Tried to restart OSCacheProvider, which is already running.");
+        }
+    }
+    
+}

src/java/com/opensymphony/oscache/hibernate/package.html

+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<!--
+
+  @(#)package.html
+
+  Copyright (c) 2002-2007 by OpenSymphony
+  All rights reserved.
+
+-->
+</head>
+<body bgcolor="white">
+
+Provides Hibernate 3.2 classes for OSCache.
+
+
+<h2>Package Specification</h2>
+
+<h2>Related Documentation</h2>
+
+
+For overviews, tutorials, examples, guides, and tool documentation, please see:
+<ul>
+  <li><a href="http://www.opensymphony.com/oscache">The OSCache Homepage</a>
+</ul>
+
+<!-- Put @see and @since tags down here. -->
+
+</body>
+</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.