You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by dk...@apache.org on 2011/07/28 16:55:28 UTC
svn commit: r1151882 - in /cayenne/main/trunk: ./
framework/cayenne-jdk1.5-unpublished/
framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/
framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/
Author: dkazimirchyk
Date: Thu Jul 28 14:55:26 2011
New Revision: 1151882
URL: http://svn.apache.org/viewvc?rev=1151882&view=rev
Log:
CAY-1595 EHCache implementation of Provider<QueryCache>
basic implementation of EhCacheQueryCache + some unit tests modification for it
Added:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCacheProvider.java
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextQueryCachingEhCacheTest.java
Modified:
cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml
cayenne/main/trunk/pom.xml
Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml?rev=1151882&r1=1151881&r2=1151882&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/pom.xml Thu Jul 28 14:55:26 2011
@@ -121,6 +121,10 @@
<groupId>mockrunner</groupId>
<artifactId>mockrunner</artifactId>
</dependency>
+ <dependency>
+ <groupId>net.sf.ehcache</groupId>
+ <artifactId>ehcache-core</artifactId>
+ </dependency>
</dependencies>
<build>
<plugins>
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java?rev=1151882&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCache.java Thu Jul 28 14:55:26 2011
@@ -0,0 +1,187 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.cache;
+
+import java.util.List;
+
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Ehcache;
+import net.sf.ehcache.Element;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.di.BeforeScopeEnd;
+import org.apache.cayenne.query.QueryMetadata;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+public class EhCacheQueryCache implements QueryCache {
+
+ /**
+ * Default cache group name.
+ */
+ static final String DEFAULT_CACHE_NAME = "cayenne.default.cachegroup";
+
+ private static Log logger = LogFactory.getLog(EhCacheQueryCache.class);
+
+ protected CacheManager cacheManager;
+
+ public EhCacheQueryCache() {
+ cacheManager = new CacheManager();
+ init();
+ }
+
+ public EhCacheQueryCache(String configFile) {
+ cacheManager = new CacheManager(configFile);
+ init();
+ }
+
+ public EhCacheQueryCache(CacheManager cacheManager) {
+ if (cacheManager != null) {
+ this.cacheManager = cacheManager;
+ init();
+ }
+ else {
+ throw new CayenneRuntimeException("CacheManager cannot be null.");
+ }
+ }
+
+ private void init() {
+ cacheManager.addCacheIfAbsent(DEFAULT_CACHE_NAME);
+ }
+
+ public List get(QueryMetadata metadata) {
+ String key = metadata.getCacheKey();
+ if (key == null) {
+ return null;
+ }
+
+ Element result = null;
+ String[] groupNames = metadata.getCacheGroups();
+ if (groupNames != null && groupNames.length > 0) {
+ Ehcache cache = cacheManager.getCache(groupNames[0]);
+ if (cache == null) {
+ return null;
+ }
+ else {
+ result = cache.get(key);
+ if (result != null) {
+ return (List)result.getObjectValue();
+ }
+ }
+ if (groupNames.length > 1) {
+ logger.warn("multiple cache groups per key: " + key);
+ }
+ }
+ else {
+ result = getDefaultCache().get(key);
+ if (result != null) {
+ return (List)getDefaultCache().get(key).getObjectValue();
+ }
+ }
+ return null;
+ }
+
+ public List get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
+ String key = metadata.getCacheKey();
+ if (key == null) {
+ return null;
+ }
+
+ List result = get(metadata);
+ if (result != null) {
+ return result;
+ }
+ else {
+ Object noResult = factory.createObject();
+ if (!(noResult instanceof List)) {
+ if (noResult == null) {
+ throw new CayenneRuntimeException("Null object created: "
+ + metadata.getCacheKey());
+ }
+ else {
+ throw new CayenneRuntimeException(
+ "Invalid query result, expected List, got "
+ + noResult.getClass().getName());
+ }
+ }
+
+ List list = (List)noResult;
+ put(metadata, list);
+ return list;
+ }
+ }
+
+ public void put(QueryMetadata metadata, List results) {
+ String key = metadata.getCacheKey();
+ if (key != null) {
+ String[] groupNames = metadata.getCacheGroups();
+ if (groupNames != null && groupNames.length > 0) {
+ Ehcache cache = cacheManager.addCacheIfAbsent(groupNames[0]);
+ cache.put(new Element(key, results));
+ if (groupNames.length > 1) {
+ logger.warn("multiple groups per key: " + key);
+ }
+ }
+ else {
+ getDefaultCache().put(new Element(key,results));
+ }
+ }
+ }
+
+ public void remove(String key) {
+ if (key != null) {
+ for (String cache : cacheManager.getCacheNames()) {
+ cacheManager.getCache(cache).remove(key);
+ }
+ }
+
+ }
+
+ public void removeGroup(String groupKey) {
+ cacheManager.removeCache(groupKey);
+ }
+
+ public void clear() {
+ cacheManager.removalAll();
+ }
+
+ public int size() {
+ int size = 0;
+ for (String cache : cacheManager.getCacheNames()) {
+ size += cacheManager.getCache(cache).getSize();
+ }
+ return size;
+ }
+
+ /**
+ * Returns default cache group.
+ */
+ public Ehcache getDefaultCache() {
+ return cacheManager.getCache(DEFAULT_CACHE_NAME);
+ }
+
+ /**
+ * Shuts down EhCache CacheManager
+ */
+ @BeforeScopeEnd
+ public void shutdown() {
+ cacheManager.shutdown();
+ }
+}
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCacheProvider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCacheProvider.java?rev=1151882&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCacheProvider.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/cache/EhCacheQueryCacheProvider.java Thu Jul 28 14:55:26 2011
@@ -0,0 +1,36 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.cache;
+
+import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.di.Provider;
+
+
+public class EhCacheQueryCacheProvider implements Provider<QueryCache> {
+
+ public QueryCache get() throws ConfigurationException {
+ return new QueryCacheLazyInitializationProxy(new Provider<QueryCache>() {
+
+ public QueryCache get() throws ConfigurationException {
+ return new EhCacheQueryCache();
+ }
+ });
+ }
+
+}
Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextQueryCachingEhCacheTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextQueryCachingEhCacheTest.java?rev=1151882&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextQueryCachingEhCacheTest.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextQueryCachingEhCacheTest.java Thu Jul 28 14:55:26 2011
@@ -0,0 +1,61 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+package org.apache.cayenne.access;
+
+import org.apache.cayenne.cache.EhCacheQueryCache;
+import org.apache.cayenne.test.jdbc.TableHelper;
+import org.apache.cayenne.unit.di.server.ServerCase;
+import org.apache.cayenne.unit.di.server.UseServerRuntime;
+
+@UseServerRuntime(ServerCase.TESTMAP_PROJECT)
+public class DataContextQueryCachingEhCacheTest extends DataContextQueryCachingTest {
+
+ protected EhCacheQueryCache domainCache;
+ protected EhCacheQueryCache contextCache;
+
+ @Override
+ protected void setUpAfterInjection() throws Exception {
+ dbHelper.deleteAll("PAINTING");
+ dbHelper.deleteAll("ARTIST");
+
+ tArtist = new TableHelper(dbHelper, "ARTIST");
+ tArtist.setColumns("ARTIST_ID", "ARTIST_NAME");
+
+ tPainting = new TableHelper(dbHelper, "PAINTING");
+ tPainting.setColumns(
+ "PAINTING_ID",
+ "PAINTING_TITLE",
+ "ARTIST_ID",
+ "ESTIMATED_PRICE");
+
+ domain = context.getParentDataDomain();
+ oldCache = domain.getQueryCache();
+
+ domainCache = new EhCacheQueryCache();
+ contextCache = new EhCacheQueryCache();
+ domain.setQueryCache(domainCache);
+ context.setQueryCache(contextCache);
+ }
+
+ @Override
+ protected void tearDownBeforeInjection() throws Exception {
+ domainCache.shutdown();
+ contextCache.shutdown();
+ }
+}
Modified: cayenne/main/trunk/pom.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/pom.xml?rev=1151882&r1=1151881&r2=1151882&view=diff
==============================================================================
--- cayenne/main/trunk/pom.xml (original)
+++ cayenne/main/trunk/pom.xml Thu Jul 28 14:55:26 2011
@@ -455,6 +455,12 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>net.sf.ehcache</groupId>
+ <artifactId>ehcache-core</artifactId>
+ <version>2.4.3</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
</dependencyManagement>