Skip Headers
Oracle® Coherence Developer's Guide
Release 3.5

Part Number E14509-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

10 Specifying a Custom Eviction Policy

The LocalCache class is used for size-limited caches. It is used both for caching on-heap objects (as in a local cache or the front portion of a near cache) and as the backing map for a partitioned cache. Applications can provide custom eviction policies for use with a LocalCache.

Note that Coherence's default eviction policy is very effective for most workloads; the majority of applications will not need to provide a custom policy. Generally, it is best to restrict the use of eviction policies to scenarios where the evicted data is present in a backing system (that is, the back portion of a near cache or a database). Eviction should be treated as a physical operation (freeing memory) and not a logical operation (deleting an entity).

Example 10-1 shows the implementation of a simple custom eviction policy:

Example 10-1 Implementing a Custom Eviction Policy

import com.tangosol.net.cache.CacheEvent;
import com.tangosol.net.cache.ConfigurableCacheMap;
import com.tangosol.net.cache.LocalCache;
import com.tangosol.util.AbstractMapListener;
import com.tangosol.util.MapEvent;

import java.util.Iterator;

public class MyEvictionPolicy extends AbstractMapListener implements ConfigurableCacheMap.EvictionPolicy
    {
    LocalCache m_cache = null;

    public void entryInserted(MapEvent evt)
        {
        System.out.println("entryInserted:" + isSynthetic(evt) + evt);
        if (m_cache == null)
            {
            m_cache = (LocalCache) evt.getMap();
            }
        }

    public void entryUpdated(MapEvent evt)
        {
        System.out.println("entryUpdated:" + isSynthetic(evt) + evt);
        }

    public void entryDeleted(MapEvent evt)
        {
        System.out.println("entryDeleted:" + isSynthetic(evt) + evt);
        }

    String isSynthetic(MapEvent evt)
        {
        // synthetic events are caused by internal processing - eviction or loading
        return ((CacheEvent) evt).isSynthetic() ? " SYNTHETIC " : " ";
        }

    public void entryTouched(OldCache.Entry entry)
        {
        System.out.println("entryTouched:" + entry.getKey());
        }

    public void requestEviction(int cMaximum)
        {
        int cCurrent = m_cache.getUnits();
        System.out.println("requestEviction: current:" + cCurrent + " to:" + cMaximum);

        //
        // ... eviction policy calculations ...
        //
        for (Iterator iter = m_cache.entrySet().iterator(); iter.hasNext();)
            {
            ConfigurableCacheMap.Entry entry = (ConfigurableCacheMap.Entry)
               iter.next();
            if (m_cache.getUnits() > cMaximum)
                {
                m_cache.evict(entry.getKey());
                }
            else
                {
                break;
                }
            }
        }

    public MyEvictionPolicy()
        {
        }

    }

Example 10-2 illustrates a Coherence cache configuration file (coherence-cache-config.xml) with an eviction policy:

Example 10-2 Custom Eviction Policy in a coherence-cache-config.xml File

<?xml version="1.0"?>

<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

<cache-config>
    <caching-scheme-mapping>
        <cache-mapping>
            <cache-name>test</cache-name>
            <scheme-name>example-near</scheme-name>
        </cache-mapping>
    </caching-scheme-mapping>

    <caching-schemes>
        <distributed-scheme>
            <scheme-name>example-distributed</scheme-name>
            <service-name>DistributedCache</service-name>

            <backing-map-scheme>
                <local-scheme>
                    <scheme-ref>example-backing-map</scheme-ref>
                </local-scheme>
            </backing-map-scheme>

            <autostart>true</autostart>
        </distributed-scheme>

        <near-scheme>
            <scheme-name>example-near</scheme-name>

            <front-scheme>
                <local-scheme>
                    <eviction-policy>
                        <class-scheme>
                            <class-name>MyEvictionPolicy</class-name>
                        </class-scheme>
                    </eviction-policy>
                    <high-units>10</high-units>
                </local-scheme>
            </front-scheme>

            <back-scheme>
                <distributed-scheme>
                    <scheme-ref>example-distributed</scheme-ref>
                </distributed-scheme>
            </back-scheme>

            <invalidation-strategy>all</invalidation-strategy>
            <autostart>true</autostart>
        </near-scheme>

        <local-scheme>
            <scheme-name>example-backing-map</scheme-name>
            <eviction-policy>HYBRID</eviction-policy>
            <high-units>{back-size-limit 0}</high-units>
            <expiry-delay>{back-expiry 1h}</expiry-delay>
            <flush-delay>1m</flush-delay>
            <cachestore-scheme></cachestore-scheme>
        </local-scheme>
    </caching-schemes>
</cache-config>