How to use a (performance) cache with Castor

# How to use a (performance) cache with Castor

## Intended Audience

Anyone who wants to enable caching for classes already mapped with Castor JDO.

This document addresses the basics to get people familiar with the basic concepts and discusses some implementation details.

The example given describes the addition of a `<cache-type>` element to an existing class mapping.

## Prerequisites

You should have a valid class mapping for a Java class, similar to the following one:

    <mapping>
        <class name="com.xyz.MyOtherObject" identity="id">
            <field name="id" type="integer">
                   ...
            </field>
            <field name="description" type="string">
                   ...
            </field>
        </class>
    </mapping>

## Steps

Here is how to proceed.

### Add <cache-type> element to class mapping

Add a <cache-type> element as shown below, specifying the cache provider to use in the 'type' attribute.

    <mapping>
        <class name="com.xyz.MyOtherObject" identity="id">
            <cache-type type="time-limited"/>
            <field name="id" type="integer">
                   ...
            </field>
            <field name="description" type="string">
                   ...
            </field>
        </class>
    </mapping>

This, for example, defines the 'time-limited' cache provider to be used for the com.xyz.MyOtherObject. This cache provider applies internally a time-limited least-recently-used algorithm for com.xyz.MyObject instances.

## Tips

- With the current release, performance caches also serve a dual purpose as dirty checking caches for long-transactions. This limitation implies that the object's duration in the performance cache determines the allowed time span of a long transaction. This might become an issue when performance caches of type '`count-limited`' or '`time-limited`' are being used, as objects will eventually be disposed. If an application tries to update an object that has been disposed from the dirty checking cache, an ObjectModifedException will be thrown.

## References

- Long transactions

- Caching

- Caching and clustered environments