diff --git a/src/main/java/io/ebean/CacheMode.java b/src/main/java/io/ebean/CacheMode.java index 6fcaedb00..364f06e3f 100644 --- a/src/main/java/io/ebean/CacheMode.java +++ b/src/main/java/io/ebean/CacheMode.java @@ -1,41 +1,71 @@ package io.ebean; /** - * Enum to control the different cache modes for queryCache (and maybe later) beanCache. + * Enum to control the different cache modes for queryCache and beanCache. + *

Bean cache

*

- * If cache is enabled, you must be careful, what you do with the returned collection. + * The bean cache is automatically used by default on @Cache beans for + * the following queries: + *

+ * + *

+ * Bean caching needs to be explicitly turned on for queries that are findList() by natural keys. + *

+ *

Query cache

+ *

+ * For query cache use note that you must be careful, what you do with the returned collection. * By default the returned collections are read only and you will get an exception if you try * to change them. * If you add ".setReadOnly(false)" to your query, you'll get a collection that is a clone from the - * one in the cache. That means, changing does not affect the cache + * one in the cache. That means, changing does not affect the cache. + *

* * @author Roland Praml, FOCONIS AG */ public enum CacheMode { + /** * Do not use cache. */ OFF(false, false), /** - * Use the cache (query & store the result). + * Use the cache and store a result when needed. */ ON(true, true), /** - * Only used for bean caching. We automatically use the cache for findOne() but not findList(). + * Only used for bean caching. + *

+ * The bean cache is automatically used by default on @Cache beans for + * the following queries: + *

+ * + *

+ * Bean caching needs to be explicitly turned on for queries that are findList() by natural keys. + *

*/ AUTO(true, true), /** - * Do not read from cache, but write retrieved value to cache. - * Use this, if you want to get the fresh value from database and a CacheMode.ON query will follow. + * Do not read from cache, but load retrieved beans into the cache. + *

+ * Use this, if you want to get the fresh value from database into the cache. Typically a CacheMode.ON query + * will follow. */ RECACHE(false, true), /** - * Query the cache for value. If it is there, use it, otherwise hit database but do NOT put the value - * into the cache. (this mode is for completeness. There's probably no use case for this) + * Query the cache for value. If it is there, use it and otherwise hit database but do NOT put the value + * into the cache. Note that there are not many use case for this mode. */ QUERY_ONLY(true, false); @@ -48,14 +78,14 @@ public enum CacheMode { } /** - * Retruns true if value is read from cache. + * Return true if value is read from cache. */ public boolean isGet() { return get; } /** - * Returns true if value (from database) is written to cache. + * Return true if a newly loaded value (from database) is put into the cache. */ public boolean isPut() { return put; diff --git a/src/main/java/io/ebean/ExpressionList.java b/src/main/java/io/ebean/ExpressionList.java index 50518df80..1a5db3a21 100644 --- a/src/main/java/io/ebean/ExpressionList.java +++ b/src/main/java/io/ebean/ExpressionList.java @@ -457,14 +457,24 @@ public interface ExpressionList { Query setMapKey(String mapKey); /** - * Set to true to use the query for executing this query. + * Set to true when this query should use the bean cache. + *

+ * This is now the same as setUseBeanCache(CacheMode.ON) and will be deprecated. + *

* * @see Query#setUseCache(boolean) */ Query setUseCache(boolean useCache); /** - * Set the {@link CacheMode} to use the query for executing this query. + * Set the mode to use the bean cache when executing this query. + * + * @see Query#setBeanCacheMode(CacheMode) + */ + Query setBeanCacheMode(CacheMode beanCacheMode); + + /** + * Set the {@link CacheMode} to use the query cache for executing this query. * * @see Query#setUseQueryCache(boolean) */ diff --git a/src/main/java/io/ebean/Query.java b/src/main/java/io/ebean/Query.java index b92067a46..0ffb4503f 100644 --- a/src/main/java/io/ebean/Query.java +++ b/src/main/java/io/ebean/Query.java @@ -1305,7 +1305,7 @@ public interface Query { * * // Assuming sku is unique for products... * - * Map productMap = + * Map productMap = * ebeanServer.find(Product.class) * // use sku for keys... * .setMapKey("sku") @@ -1320,22 +1320,36 @@ public interface Query { /** * Set this to false to not use the bean cache. *

+ * This method is now superseded by {@link #setBeanCacheMode(CacheMode)} + * which provides more explicit options controlled bean cache use. + *

+ *

+ * This method is likely to be deprecated in the future with migration + * over to setUseBeanCache(). + *

+ */ + default Query setUseCache(boolean useCache) { + return setBeanCacheMode(useCache ? CacheMode.ON : CacheMode.OFF); + } + + /** + * Set the mode to use the bean cache when executing this query. + *

* By default "find by id" and "find by natural key" will use the bean cache * when bean caching is enabled. Setting this to false means that the query * will not use the bean cache and instead hit the database. *

*

- * In the case of other queries (findList(), findEach() etc) then setting this to - * false beans that the if lazy loading is invoked that lazy loading will not try - * to use the bean cache. + * By default findList() with natural keys will not use the bean cache. In that + * case we need to explicitly use the bean cache. *

*/ - Query setUseCache(boolean useCache); + Query setBeanCacheMode(CacheMode beanCacheMode); /** * Set the {@link CacheMode} to use the query for executing this query. */ - Query setUseQueryCache(CacheMode useQueryCache); + Query setUseQueryCache(CacheMode queryCacheMode); /** * Calls {@link #setUseQueryCache(CacheMode)} with ON or OFF. @@ -1369,8 +1383,9 @@ public interface Query { Query setReadOnly(boolean readOnly); /** - * When set to true all the beans from this query are loaded into the bean - * cache. + * Will be deprecated - migrate to use setBeanCacheMode(CacheMode.RECACHE). + *

+ * When set to true all the beans from this query are loaded into the bean cache. */ Query setLoadBeanCache(boolean loadBeanCache); diff --git a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java index 50c477c2a..ba342e559 100644 --- a/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java +++ b/src/main/java/io/ebeaninternal/server/expression/DefaultExpressionList.java @@ -483,6 +483,11 @@ public class DefaultExpressionList implements SpiExpressionList { return query.setUseCache(useCache); } + @Override + public Query setBeanCacheMode(CacheMode useCache) { + return query.setBeanCacheMode(useCache); + } + @Override public Query setUseQueryCache(CacheMode useCache) { return query.setUseQueryCache(useCache); diff --git a/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java b/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java index a2360f9c3..5d16121ec 100644 --- a/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java +++ b/src/main/java/io/ebeaninternal/server/expression/JunctionExpression.java @@ -767,6 +767,11 @@ class JunctionExpression implements SpiJunction, SpiExpression, Expression return exprList.setUseCache(useCache); } + @Override + public Query setBeanCacheMode(CacheMode useCache) { + return exprList.setBeanCacheMode(useCache); + } + @Override public Query setUseQueryCache(CacheMode useCache) { return exprList.setUseQueryCache(useCache); diff --git a/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java b/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java index 0ff7b3b29..89765da9e 100644 --- a/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java +++ b/src/main/java/io/ebeaninternal/server/querydefn/DefaultOrmQuery.java @@ -1135,8 +1135,8 @@ public class DefaultOrmQuery implements SpiQuery { } @Override - public DefaultOrmQuery setUseCache(boolean useCache) { - this.useBeanCache = (useCache) ? CacheMode.ON: CacheMode.OFF; + public Query setBeanCacheMode(CacheMode beanCacheMode) { + this.useBeanCache = beanCacheMode; return this; } diff --git a/src/test/java/io/ebean/BaseTestCase.java b/src/test/java/io/ebean/BaseTestCase.java index 09b2100bf..7f98b2941 100644 --- a/src/test/java/io/ebean/BaseTestCase.java +++ b/src/test/java/io/ebean/BaseTestCase.java @@ -129,7 +129,7 @@ public abstract class BaseTestCase { protected void loadCountryCache() { Ebean.find(Country.class) - .setLoadBeanCache(true) + .setBeanCacheMode(CacheMode.RECACHE) .findList(); } diff --git a/src/test/java/org/tests/basic/TestLazyLoadInCache.java b/src/test/java/org/tests/basic/TestLazyLoadInCache.java index 7fe521d19..cb8bd61e2 100644 --- a/src/test/java/org/tests/basic/TestLazyLoadInCache.java +++ b/src/test/java/org/tests/basic/TestLazyLoadInCache.java @@ -2,11 +2,12 @@ package org.tests.basic; import io.ebean.BaseTestCase; import io.ebean.BeanState; +import io.ebean.CacheMode; import io.ebean.Ebean; +import org.junit.Test; import org.tests.model.basic.Address; import org.tests.model.basic.Customer; import org.tests.model.basic.ResetBasicData; -import org.junit.Test; import java.util.Map; import java.util.Set; @@ -23,7 +24,7 @@ public class TestLazyLoadInCache extends BaseTestCase { Map map = Ebean.find(Customer.class) .select("id, name") - .setLoadBeanCache(true) + .setBeanCacheMode(CacheMode.RECACHE) .setReadOnly(true) .orderBy().asc("id") .findMap(); diff --git a/src/test/java/org/tests/basic/TestReadOnlyPropagation.java b/src/test/java/org/tests/basic/TestReadOnlyPropagation.java index 97ffde7fe..4417e5451 100644 --- a/src/test/java/org/tests/basic/TestReadOnlyPropagation.java +++ b/src/test/java/org/tests/basic/TestReadOnlyPropagation.java @@ -1,15 +1,16 @@ package org.tests.basic; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; import io.ebean.bean.BeanCollection; +import org.junit.Assert; +import org.junit.Test; import org.tests.model.basic.Address; import org.tests.model.basic.Customer; import org.tests.model.basic.Order; import org.tests.model.basic.OrderDetail; import org.tests.model.basic.ResetBasicData; -import org.junit.Assert; -import org.junit.Test; import java.util.Iterator; import java.util.List; @@ -24,7 +25,7 @@ public class TestReadOnlyPropagation extends BaseTestCase { Order order = Ebean.find(Order.class) .setAutoTune(false) - .setUseCache(false) + .setBeanCacheMode(CacheMode.OFF) .setReadOnly(true) .setId(1) .findOne(); diff --git a/src/test/java/org/tests/cache/TestCacheCollectionIds.java b/src/test/java/org/tests/cache/TestCacheCollectionIds.java index ae59a1257..0311b71e4 100644 --- a/src/test/java/org/tests/cache/TestCacheCollectionIds.java +++ b/src/test/java/org/tests/cache/TestCacheCollectionIds.java @@ -1,15 +1,22 @@ package org.tests.cache; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; import io.ebean.SqlUpdate; import io.ebean.Update; import io.ebean.cache.ServerCache; import io.ebean.cache.ServerCacheManager; import io.ebeaninternal.server.cache.CachedManyIds; -import org.tests.model.basic.*; import org.junit.Assert; import org.junit.Test; +import org.tests.model.basic.Contact; +import org.tests.model.basic.Country; +import org.tests.model.basic.Customer; +import org.tests.model.basic.OCachedBean; +import org.tests.model.basic.Order; +import org.tests.model.basic.OrderDetail; +import org.tests.model.basic.ResetBasicData; import java.util.ArrayList; import java.util.List; @@ -33,7 +40,7 @@ public class TestCacheCollectionIds extends BaseTestCase { custCache.clear(); custManyIdsCache.clear(); - List list = Ebean.find(Customer.class).setAutoTune(false).setLoadBeanCache(true) + List list = Ebean.find(Customer.class).setAutoTune(false).setBeanCacheMode(CacheMode.RECACHE) .order().asc("id").findList(); Assert.assertTrue(list.size() > 1); diff --git a/src/test/java/org/tests/cache/TestCacheCustomer.java b/src/test/java/org/tests/cache/TestCacheCustomer.java index cb3f38022..cbd18e7c8 100644 --- a/src/test/java/org/tests/cache/TestCacheCustomer.java +++ b/src/test/java/org/tests/cache/TestCacheCustomer.java @@ -1,14 +1,15 @@ package org.tests.cache; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; +import org.junit.Assert; +import org.junit.Test; import org.tests.model.basic.Address; import org.tests.model.basic.Contact; import org.tests.model.basic.Country; import org.tests.model.basic.Customer; import org.tests.model.basic.ResetBasicData; -import org.junit.Assert; -import org.junit.Test; import java.util.List; @@ -38,7 +39,7 @@ public class TestCacheCustomer extends BaseTestCase { ResetBasicData.reset(); - List list = Ebean.find(Customer.class).setAutoTune(false).setLoadBeanCache(true) + List list = Ebean.find(Customer.class).setAutoTune(false).setBeanCacheMode(CacheMode.RECACHE) .findList(); Assert.assertTrue(list.size() > 1); diff --git a/src/test/java/org/tests/cache/TestCacheNaturalId.java b/src/test/java/org/tests/cache/TestCacheNaturalId.java index dd31366df..183302c90 100644 --- a/src/test/java/org/tests/cache/TestCacheNaturalId.java +++ b/src/test/java/org/tests/cache/TestCacheNaturalId.java @@ -1,16 +1,19 @@ package org.tests.cache; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; import io.ebean.cache.ServerCache; import io.ebean.cache.ServerCacheStatistics; +import org.junit.Test; import org.tests.model.basic.Contact; import org.tests.model.basic.ResetBasicData; -import org.junit.Test; import java.util.List; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class TestCacheNaturalId extends BaseTestCase { @@ -21,7 +24,7 @@ public class TestCacheNaturalId extends BaseTestCase { ServerCache contactCache = Ebean.getServerCacheManager().getBeanCache(Contact.class); - List list = Ebean.find(Contact.class).setLoadBeanCache(true).findList(); + List list = Ebean.find(Contact.class).setBeanCacheMode(CacheMode.RECACHE).findList(); assertTrue(contactCache.size() > 0); diff --git a/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey.java b/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey.java index 45dab5f70..90b87151c 100644 --- a/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey.java +++ b/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey.java @@ -1,6 +1,7 @@ package org.tests.model.basic.cache; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; import io.ebean.cache.ServerCache; import io.ebean.cache.ServerCacheManager; @@ -77,7 +78,7 @@ public class TestCacheViaComplexNaturalKey extends BaseTestCase { private void loadSomeIntoCache() { Ebean.find(OCachedNatKeyBean.class) - .setLoadBeanCache(true) + .setBeanCacheMode(CacheMode.RECACHE) .where().le("sku", "2") .findList(); diff --git a/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey3.java b/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey3.java index 3aa3919fb..ec20eed7b 100644 --- a/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey3.java +++ b/src/test/java/org/tests/model/basic/cache/TestCacheViaComplexNaturalKey3.java @@ -1,6 +1,7 @@ package org.tests.model.basic.cache; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; import io.ebean.Pairs; import io.ebean.cache.ServerCache; @@ -81,7 +82,7 @@ public class TestCacheViaComplexNaturalKey3 extends BaseTestCase { private void loadSomeIntoCache() { Ebean.find(OCachedNatKeyBean3.class) - .setLoadBeanCache(true) + .setBeanCacheMode(CacheMode.RECACHE) .where() .ge("sku", "2") .eq("store", "def") @@ -302,7 +303,7 @@ public class TestCacheViaComplexNaturalKey3 extends BaseTestCase { .where() .eq("store", "def") .inPairs(pairs) - .setUseCache(true) + .setBeanCacheMode(CacheMode.ON) .orderBy("sku desc") .findList(); diff --git a/src/test/java/org/tests/query/TestQueryFindReadOnly.java b/src/test/java/org/tests/query/TestQueryFindReadOnly.java index 59347a008..ea1c56bc4 100644 --- a/src/test/java/org/tests/query/TestQueryFindReadOnly.java +++ b/src/test/java/org/tests/query/TestQueryFindReadOnly.java @@ -1,11 +1,12 @@ package org.tests.query; import io.ebean.BaseTestCase; +import io.ebean.CacheMode; import io.ebean.Ebean; -import org.tests.model.basic.Article; -import org.tests.model.basic.Section; import org.junit.Assert; import org.junit.Test; +import org.tests.model.basic.Article; +import org.tests.model.basic.Section; import java.util.List; @@ -31,7 +32,7 @@ public class TestQueryFindReadOnly extends BaseTestCase { Section s2 = ar1sections.get(0); Assert.assertTrue("readonly cascading", Ebean.getBeanState(s2).isReadOnly()); - Ebean.find(Article.class).setLoadBeanCache(true).findList(); + Ebean.find(Article.class).setBeanCacheMode(CacheMode.RECACHE).findList(); Article ar0 = Ebean.find(Article.class, a0.getId());