You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2010/07/14 19:01:52 UTC
svn commit: r964097 - in /openjpa/trunk:
openjpa-kernel/src/main/java/org/apache/openjpa/datacache/
openjpa-kernel/src/main/java/org/apache/openjpa/util/
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/
openjpa-project/s...
Author: curtisr7
Date: Wed Jul 14 17:01:51 2010
New Revision: 964097
URL: http://svn.apache.org/viewvc?rev=964097&view=rev
Log:
OPENJPA-1378: Expose Lru cache on DataCache and QueryCache.
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java (with props)
Modified:
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentQueryCache.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/PartitionedDataCache.java
openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java?rev=964097&r1=964096&r2=964097&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentDataCache.java Wed Jul 14 17:01:51 2010
@@ -40,8 +40,11 @@ public class ConcurrentDataCache
private static final Localizer _loc = Localizer.forPackage
(ConcurrentDataCache.class);
- private CacheMap _cache = newCacheMap();
-
+ private CacheMap _cache;
+ private int _cacheSize = Integer.MIN_VALUE;
+ private int _softRefs = Integer.MIN_VALUE;
+ protected boolean _lru = false;
+
/**
* Returns the underlying {@link CacheMap} that this cache is using.
* This is not an unmodifiable view on the map, so care should be taken
@@ -60,7 +63,7 @@ public class ConcurrentDataCache
* flushing old values.
*/
public void setCacheSize(int size) {
- _cache.setCacheSize(size);
+ _cacheSize = size;
}
/**
@@ -78,7 +81,7 @@ public class ConcurrentDataCache
* flushing values.
*/
public void setSoftReferenceSize(int size) {
- _cache.setSoftReferenceSize(size);
+ _softRefs = size;
}
/**
@@ -92,6 +95,7 @@ public class ConcurrentDataCache
public void initialize(DataCacheManager mgr) {
super.initialize(mgr);
conf.getRemoteCommitEventManager().addInternalListener(this);
+ _cache = newCacheMap();
}
public void unpinAll(Class<?> cls, boolean subs) {
@@ -113,12 +117,18 @@ public class ConcurrentDataCache
* invoke {@link AbstractDataCache#keyRemoved}.
*/
protected CacheMap newCacheMap() {
- return new CacheMap() {
- protected void entryRemoved(Object key, Object value,
- boolean expired) {
+ CacheMap res = new CacheMap(_lru) {
+ protected void entryRemoved(Object key, Object value, boolean expired) {
keyRemoved(key, expired);
}
};
+ if (_cacheSize != Integer.MIN_VALUE) {
+ res.setCacheSize(_cacheSize);
+ }
+ if (_softRefs != Integer.MIN_VALUE) {
+ res.setSoftReferenceSize(_softRefs);
+ }
+ return res;
}
protected DataCachePCData getInternal(Object key) {
@@ -157,4 +167,11 @@ public class ConcurrentDataCache
return true;
}
+ public void setLru(boolean l) {
+ _lru = l;
+ }
+
+ public boolean getLru() {
+ return _lru;
+ }
}
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentQueryCache.java?rev=964097&r1=964096&r2=964097&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentQueryCache.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ConcurrentQueryCache.java Wed Jul 14 17:01:51 2010
@@ -36,8 +36,11 @@ public class ConcurrentQueryCache
extends AbstractQueryCache
implements RemoteCommitListener {
- private CacheMap _cache = newCacheMap();
-
+ private CacheMap _cache;
+ protected boolean _lru = false;
+ private int _cacheSize = Integer.MIN_VALUE;
+ private int _softRefs = Integer.MIN_VALUE;
+
/**
* Returns the underlying {@link CacheMap} that this cache is using.
* This is not an unmodifiable view on the map, so care should be taken
@@ -67,7 +70,7 @@ public class ConcurrentQueryCache
* flushing old values.
*/
public void setCacheSize(int size) {
- _cache.setCacheSize(size);
+ _cacheSize = size;
}
/**
@@ -85,12 +88,13 @@ public class ConcurrentQueryCache
* flushing values.
*/
public void setSoftReferenceSize(int size) {
- _cache.setSoftReferenceSize(size);
+ _softRefs = size;
}
public void initialize(DataCacheManager mgr) {
super.initialize(mgr);
conf.getRemoteCommitEventManager().addInternalListener(this);
+ _cache = newCacheMap();
}
public void writeLock() {
@@ -107,7 +111,14 @@ public class ConcurrentQueryCache
* Return the map to use as an internal cache.
*/
protected CacheMap newCacheMap() {
- return new CacheMap();
+ CacheMap res = new CacheMap(_lru);
+ if (_cacheSize != Integer.MIN_VALUE) {
+ res.setCacheSize(_cacheSize);
+ }
+ if (_softRefs != Integer.MIN_VALUE) {
+ res.setSoftReferenceSize(_softRefs);
+ }
+ return res;
}
protected QueryResult getInternal(QueryKey qk) {
@@ -144,4 +155,12 @@ public class ConcurrentQueryCache
public EvictPolicy getEvictPolicy() {
return super.evictPolicy;
}
+
+ public void setLru(boolean l) {
+ _lru = l;
+ }
+
+ public boolean getLru() {
+ return _lru;
+ }
}
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/PartitionedDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/PartitionedDataCache.java?rev=964097&r1=964096&r2=964097&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/PartitionedDataCache.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/PartitionedDataCache.java Wed Jul 14 17:01:51 2010
@@ -68,6 +68,13 @@ public class PartitionedDataCache extend
private final List<String> _partProperties = new ArrayList<String>();
private final Map<String, DataCache> _partitions = new HashMap<String, DataCache>();
+ @Override
+ public void initialize(DataCacheManager mgr) {
+ super.initialize(mgr);
+ for(DataCache part : _partitions.values()){
+ part.initialize(mgr);
+ }
+ }
/**
* Sets the type of the partitions.
* Each partition is a DataCache itself.
Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java?rev=964097&r1=964096&r2=964097&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/CacheMap.java Wed Jul 14 17:01:51 2010
@@ -75,6 +75,15 @@ public class CacheMap
public CacheMap() {
this(false, 1000);
}
+
+ /**
+ * Create a cache map with a size of 1000.
+ *
+ * @param lru if true, create a LRU cache map otherwise a non-LRU map will be created.
+ */
+ public CacheMap(boolean lru) {
+ this(lru, 1000);
+ }
/**
* Create a cache map with the given properties.
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java?rev=964097&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java Wed Jul 14 17:01:51 2010
@@ -0,0 +1,122 @@
+/*
+ * 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.openjpa.persistence.datacache;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.openjpa.datacache.ConcurrentQueryCache;
+import org.apache.openjpa.datacache.DataCache;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.StoreCacheImpl;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+import org.apache.openjpa.util.IntId;
+
+public class TestLRUCache extends SingleEMFTestCase {
+ private final int cacheSize = 5;
+ private final String QUERY = "SELECT p FROM CachedPerson p WHERE p.id=";
+
+ @Override
+ protected void setUp(Object... props) {
+ super.setUp(CLEAR_TABLES,
+ CachedPerson.class,
+ "openjpa.RemoteCommitProvider", "sjvm",
+ "openjpa.DataCache","true(SoftReferenceSize=0,Lru=true,CacheSize="+cacheSize+")",
+ "openjpa.QueryCache","true(SoftReferenceSize=0,Lru=true,CacheSize="+cacheSize+")"
+ );
+ }
+
+ public void testQueryCacheOverFlow() {
+ OpenJPAEntityManagerSPI em = emf.createEntityManager();
+ ConcurrentQueryCache cache =
+ (ConcurrentQueryCache) emf.getConfiguration().getDataCacheManagerInstance().getSystemQueryCache();
+
+ // populate entities.
+ em.getTransaction().begin();
+ for (int i = 0; i < cacheSize + 1; i++) {
+ CachedPerson person = new CachedPerson();
+ person.setId(i);
+ em.persist(person);
+ }
+ em.getTransaction().commit();
+ // Clean up persistence context.
+ em.clear();
+
+ // Populate query cache
+ for (int i = 0; i < cacheSize + 1; i++) {
+ em.createQuery(QUERY + i, CachedPerson.class).getSingleResult();
+ }
+
+ Set<?> keys = cache.getCacheMap().keySet();
+ assertEquals(cacheSize, keys.size());
+ List<String> strKeys = new ArrayList<String>();
+ for (Object key : keys) {
+ strKeys.add(key.toString());
+ }
+
+ for (int i = 0; i < keys.size(); i++) {
+ boolean res = contains(QUERY + i, strKeys);
+ if (i == 0) {
+ assertFalse(res);
+ } else {
+ assertTrue(res);
+ }
+ }
+
+ }
+
+ public void testDataCacheOverFlow() {
+ OpenJPAEntityManagerSPI em = emf.createEntityManager();
+ StoreCacheImpl storeCache = (StoreCacheImpl) emf.getCache();
+ DataCache cache = (DataCache) storeCache.getDelegate();
+ LinkedList<CachedPerson> people = new LinkedList<CachedPerson>();
+
+ // Persist cacheSize + 1 Entites.
+ for (int i = 0; i < cacheSize + 1; i++) {
+ em.getTransaction().begin();
+ CachedPerson person = new CachedPerson();
+ person.setId(i);
+ em.persist(person);
+ people.addFirst(person);
+ em.getTransaction().commit();
+ }
+
+ // Assert that the first persisted entity isn't in the cache and everything else is.
+ for (int i = 0; i < cacheSize + 1; i++) {
+ IntId id = new IntId(CachedPerson.class, i);
+ boolean contains = cache.get(id) != null;
+ if (i == 0) {
+ assertFalse(contains);
+ } else {
+ assertTrue(contains);
+ }
+ }
+ }
+
+ private boolean contains(String needle, List<String> haystack) {
+ for (String s : haystack) {
+ if (s.contains(needle)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestLRUCache.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml?rev=964097&r1=964096&r2=964097&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml Wed Jul 14 17:01:51 2010
@@ -179,6 +179,20 @@ a little while longer. You can control t
keeps with the <literal>SoftReferenceSize</literal> property. Soft references
are unlimited by default. Set to 0 to disable soft references completely.
</para>
+ <para>
+Both the QueryCache and DataCache can be configured to use a backing <literal>Lru</literal> map rather than the default
+concurrent HashMap. Note that enabling the <literal>Lru</literal> cache can hurt performance as this map in not as
+scalable as the default map.
+ </para>
+ <example id="ref_guide_cache_conf_lru">
+ <title>
+ Data Cache Size
+ </title>
+<programlisting>
+<property name="openjpa.DataCache" value="true(Lru=true)"/>
+<property name="openjpa.QueryCache" value="true(Lru=true)"/>
+</programlisting>
+ </example>
<example id="ref_guide_cache_conf_size">
<title>
Data Cache Size