Cache configuration "ehcache.xml".

More specific settings are defined and assigned via ehcache.xml. In detail, the following settings can be made:

  • Expiration times of entries in the cache

  • Size of the cache

  • Defining cache strategies which can then be assigned to different entities.

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

    <service>
        <jsr107:defaults enable-management="true"
            enable-statistics="true" default-template="defaultCacheTemplate" />
    </service>

    <cache-template name="defaultCacheTemplate">
        <expiry>
            <!-- time to idle, the maximum time for an entry to remain untouched Entries
                to the Cache can be made to expire after a given time other options are:
                * <ttl>, time to live; * <class>, for a custom Expiry implementation; or
                * <none>, for no expiry -->
            <!-- <tti unit="minutes">10</tti> -->
            <!-- <tti unit="seconds">10</tti> -->
            <!-- <ttl unit="seconds">1200</ttl> -->
            <none />
        </expiry>
        <resources>
            <!-- Maximum entries in memory -->
            <heap unit="entries">100000</heap>
            <!-- Maximum size in memory -->
            <!-- <heap unit="MB">10000</heap> -->
        </resources>
    </cache-template>

    <cache alias="org.hibernate.cache.spi.QueryResultsRegion">
        <expiry>
            <!-- time to idle, the maximum time for an entry to remain untouched Entries
                to the Cache can be made to expire after a given time other options are:
                * <ttl>, time to live; * <class>, for a custom Expiry implementation; or
                * <none>, for no expiry -->
            <!-- <tti unit="minutes">10</tti> -->
            <!-- <tti unit="seconds">10</tti> -->
            <!-- <ttl unit="seconds">1200</ttl> -->
            <none />
        </expiry>
        <resources>
            <!-- Maximum entries in memory -->
            <heap unit="entries">100000</heap>
            <!-- Maximum size in memory -->
            <!-- <heap unit="MB">10000</heap> -->
        </resources>
    </cache>

    <!-- Because of this line the TimestampsRegionCache inherits its preferences
        from the defaultCacheTemplate. If it is necessary to define different preferences
        remove this line and use it like the lines above -->
    <cache alias="org.hibernate.cache.spi.TimestampsRegion"
        uses-template="defaultCacheTemplate" />

    <cache alias="com.company.charttest.entities.Employee"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Order"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Product"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Customer"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Category"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Customerdemographic"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Orderdetail"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.OrderdetailId"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Region"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Shipper"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Supplier"
        uses-template="defaultCacheTemplate" />
    <cache alias="com.company.charttest.entities.Territory"
        uses-template="defaultCacheTemplate" />
</config>

Define expiration times

<expiry>
    <!-- time to idle, the maximum time for an entry to remain untouched Entries to the Cache -->
    <tti unit="minutes">10</tti>
    <!-- oder -->
    <tti unit="seconds">10</tti>
</expiry>

Defines after what time the entity should be discarded. The time always starts again after each access.

Time to live

<expiry>
    <!-- time to live, the maximum time for an entry to live in the Cache -->
    <ttl unit="minutes">10</ttl>
    <!-- oder -->
    <ttl unit="seconds">10</ttl>
</expiry>

Specifies after what time the entity should be discarded. Regardless of how many times the entity has been referenced.

Size of the cache

<resources>
    <!-- Maximum entries -->
    <heap unit="entries">100000</heap>
    <!-- oder -->
    <!-- Maximum size in memory -->
    <!-- <heap unit="MB">10000</heap> -->
</resources>

Defines the maximum size of the cache until elements are moved out of the cache.

Definition of cache strategies and their assignments

<cache-template name="defaultCacheTemplate">
        <expiry
            <tti unit="seconds">10</tti>
            <none />
        </expiry>
        <resources>
            <heap unit="MB">10000</heap>
        </resources>
</cache-template>


<cache-template name="advancedCacheTemplate">
        <expiry
            <tti unit="seconds">100</tti>
            <none />
        </expiry>
        <resources>
            <heap unit="MB">1000000</heap>
        </resources>
</cache-template>

<cache alias="com.company.charttest.entities.Supplier"
    uses-template="defaultCacheTemplate" />
<cache alias="com.company.charttest.entities.Territory"
    uses-template="advancedCacheTemplate" />

Sets different cache behavior for specific entities.