Extensions : Query Cache

Plugin

DataNucleus is developed as a plugin-driven framework and one of the components that is pluggable is the caching of query compilations. DataNucleus provides some inbuilt cache options, but also allows you to provide your own.

DataNucleus is able to support third party Query Cache products. You can extend DataNucleuss capabilities using the plugin extension org.datanucleus.cache_query.

Plugin extension-point Key Description Location
org.datanucleus.cache_query weak Weak Query Cache (default) datanucleus-core
org.datanucleus.cache_query soft Soft Query Cache datanucleus-core
org.datanucleus.cache_query strong Strong Query Cache datanucleus-core
org.datanucleus.cache_query javax.cache javax.cache Query Cache datanucleus-core
org.datanucleus.cache_query cacheonix Cacheonix Query Cache datanucleus-cache

The following sections describe how to create your own Query cache plugin for DataNucleus.

Interface

If you have your own Query cache you can easily use it with DataNucleus. DataNucleus defines a QueryCache interface and you need to implement this. Javadoc.

package org.datanucleus.store.query.cache;
public interface QueryCache
{
    void close();
    void evict(String queryKey);
    void clear();
    boolean isEmpty();
    int size();
    CachedQuery get(String queryKey);
    CachedQuery put(String queryKey, CachedQuery cachedQuery);
    boolean contains(String queryKey);
}

Implementation

Lets suppose your want to implement your own Level 2 cache MyLevel2Cache

package mydomain;

import org.datanucleus.NucleusContext;
import org.datanucleus.store.query.cache.QueryCache;

public class MyQueryCache implements QueryCache
{
    public MyQueryCache(NucleusContext nucCtx)
    {
        ...
    }

    ... (implement the interface)
}

Plugin Specification

Once you have this implementation you then need to make the class available as a DataNucleus plugin. You do this by putting a file plugin.xml in your JAR at the root of the CLASSPATH. The file plugin.xml will look like this

<?xml version="1.0"?>
<plugin id="mydomain" name="DataNucleus plug-ins" provider-name="My Company">
    <extension point="org.datanucleus.cache_query">
        <cache name="MyCache" class-name="mydomain.MyQueryCache"/>
    </extension>
</plugin>

Note that you also require a MANIFEST.MF file as per the Extensions Guide.

Plugin Usage

The only thing remaining is to use your Query Cache plugin. To do this you specify the persistence property datanucleus.cache.query.type as MyCache (the name in plugin.xml).