cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrus Adamchik <and...@objectstyle.org>
Subject Re: Further on modeler problems and a Question..
Date Tue, 21 May 2019 05:42:43 GMT
>>  Is there a best practice for this use case?

Using query cache is the best practice. "local cache" is the fastest, but it is local to a
given ObjectContext. "shared cache" is a bit slower, but works across contexts and still avoids
hitting the DB. 

  // this query can be rerun any number of times without hitting the DB
  SelectQuery.query(MyEntity.class).sharedCache().select(context);

You will need to do a bit of extra setup to enable manage cache in your runtime, but it is
usually worth it. In a Bootique app you would additionally include these dependencies to hook
up Ehcache framework:

<dependency>
    <groupId>io.bootique.cayenne</groupId>
    <artifactId>bootique-cayenne-jcache</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

You can leave it at that, or can configure it further to setup cache size and expiration policies.
For this you can create an ehcache file somewhere on classpath, where you can control expiration
times, etc.:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">

    <!-- Used by Cayenne for queries without explicit cache groups. -->
    <cache alias="cayenne.default.cache">
        <expiry>
            <ttl unit="minutes">1</ttl>
        </expiry>
        <heap>100</heap>
    </cache>
</config>


And link it in your bootique config:

jcache:
  configs:
    - "classpath:com/foo/ehcache.xml"


HTH,
Andrus

[1] https://cayenne.apache.org/docs/4.0/cayenne-guide/#ext-jcache



> On May 20, 2019, at 7:25 PM, John Huss <johnthuss@gmail.com> wrote:
> 
> Just by querying the table at startup you can load it into the Object cache
> (if your shared cache is enabled, which it is by default). However if you
> exceed the size of the shared cache it will eventually evict those objects
> and re-fetch them one at a time as needed (which is bad).
> 
> On Mon, May 20, 2019 at 11:09 AM Tony Giaccone <tony@giaccone.org> wrote:
> 
>> I also have Cayenne Modeler at version 4.01 and that starts up with out a
>> problem. So I am even more confused.  The 4.0 release version seems to work
>> fine and keeps the same version number in the model files so I'll be using
>> that.
>> 
>> Now the question that started this whole process.  I have a table in the
>> database that's essentially a look up table, it has several thousand rows,
>> and I'd like to just read it in once and then use it across different
>> incoming requests.  Is there a best practice for this use case?
>> 
>> 
>> Tony
>> 


Mime
View raw message