So far we've been looking at things in the abstract; let's see an example of how this comes together. In this example, imagine we have a Hibernate application with the following characteristics.
Query caching is enabled.
There is a region prefix set as part of the Hibernate
configuration: hibernate.cache.region_prefix==appA
Some cachable entities and collections have a region name of "reference" set in their Hibernate mapping.
Some cachable queries have the "reference" region name set when they are created.
Other cachable entities and collections in the
org.example.hibernate
package don't have a
region name set in their Hibernate mapping.
Other cachable queries don't have a region name set when they are created.
Let's see a possible eviction configuration for this scenario:
<attribute name="EvictionPolicyConfig"> <config> <attribute name="wakeUpIntervalSeconds">5</attribute> <attribute name="policyClass">org.jboss.cache.eviction.LRUPolicy</attribute> <!-- Default region to pick up anything we miss in the more specific regions below. --> <region name="/_default_"> <attribute name="maxNodes">500</attribute> <attribute name="timeToLiveSeconds">300</attribute> <attribute name="minTimeToLiveSeconds">120</attribute> </region> <!-- Don't ever evict modification timestamps --> <region name="/TS" policyClass="org.jboss.cache.eviction.NullEvictionPolicy"/> <!-- Reference data --> <region name="/appA/reference"> <!-- Keep all reference data if it's being used --> <attribute name="maxNodes">0</attribute> <!-- Keep it around a long time (4 hours) --> <attribute name="timeToLiveSeconds">14400</attribute> <attribute name="minTimeToLiveSeconds">120</attribute> </region> <!-- Be more aggressive about queries on reference data --> <region name="/appA/reference/QUERY"> <attribute name="maxNodes">200</attribute> <attribute name="timeToLiveSeconds">1000</attribute> <attribute name="minTimeToLiveSeconds">120</attribute> </region> <!-- Lots of entity instances from this package, but different users are unlikely to share them. So, we can cache a lot, but evict unused ones pretty quickly. --> <region name="/appA/org/example/hibernate"> <attribute name="maxNodes">50000</attribute> <attribute name="timeToLiveSeconds">1200</attribute> <attribute name="minTimeToLiveSeconds">120</attribute> </region> <!-- Clean up misc queries very promptly --> <region name="/appA/org/hibernate/cache/StandardQueryCache"> <attribute name="maxNodes">200</attribute> <attribute name="timeToLiveSeconds">240</attribute> <attribute name="minTimeToLiveSeconds">120</attribute> </region> </config> </attribute>
Notes on the above:
The wakeUpIntervalSeconds
configuration
controls how often the background eviction process kicks
in to evict nodes.
The first policyClass
configuration
sets the default eviction policy class to use for each region.
Here we want to use the standard LRUPolicy
This can be overridden on a per-region basis, as is done
here for the /TS
region.
We set up a /_default_
region. Having
such a region is a requirement if eviction is used. Here we
don't expect any data to end up in this default region, but
if by mistake someone adds a new entity type that doesn't fall
into one of our other regions, we may not have a large memory
budget for it so we evict fairly agressively.
Evicting timestamps is forbidden, so we add a
/TS
region that disables it. Here we
see how to override the default eviction policy.
The /appA/reference
region covers our
reference data entities and collections. This is our most
likely to be reused data, so we configure the cache to be
very slow to evict it.
The queries related to our reference data are less likely to
be reused, and may take up a lot of memory, so we override the
/appA/reference
region with a
/appA/reference/QUERY
region that is more
agressive about eviction.
The org.example.hibernate
package includes a
lot of entity classes like Order
, where
there are hundreds of thousands of records in the database.
These are unlikely to be reused across users, but we have a lot
of users and want to be able to cache many of them so a user
can have fast access to his or her data during the
course of their interaction with the system. So we create a
/appA/org/example/hibernate
region
with a high maxNodes
value but a fairly
low timeToLiveSeconds
. The low time-to-live
ensures an Order
is evicted quickly once a
user is done with it.
Finally, cacheable queries that aren't assigned to
to the reference
region will end up in
/appA/org/hibernate/cache/StandardQueryCache
.
We've elected not to keep these around long at all.