Setting up the Proxy Factory

Definition of the Beans to Advise

We need to define the beans we want to be advised or proxied, i.e. the bean definitions affected by the caching services. For this example, we will use org.wanghy.cache.integration.CacheableImpl, which is the one used in the integration tests.

<bean id="cacheableTarget"
    class="org.wanghy.cache.integration.CacheableImpl">
    <property name="names">
        <list>
            <value>James Gosling</value>
        </list>
    </property>
</bean>

Definition of the Proxy Factory

The proxy factory should be an instance of org.wanghy.cache.interceptor.proxy.CacheProxyFactoryBean.

Properties:

  1. cacheProviderFacade (required).

    The facade for the cache implementation to use. It must be an implementation of org.wanghy.cache.provider.CacheProviderFacade.

    For more information about setting up cache providers, click here.

  2. cachingAttributes (required).

    Specifies:

    • Which methods will have their return values stored in the cache.
    • The id of the cache profile to use. For more information about cache profiles please read "Setting up the Cache Provider."

  3. cacheFlushAttributes (required).

    Specifies:

    • Which methods will trigger the invalidation/flush of the cache.
    • The ids of the cache profiles to use. For more information about cache profiles please read "Setting up the Cache Provider."

  4. entryStoredListener (optional).

    Listener that is notified each time an entry is added to the cache. It should be an implementation of org.wanghy.cache.interceptor.caching.EntryStoredListener. There are not any implementations of this interface.

  5. target (required).

    The bean that will be proxied.

Example:

<bean id="cacheable"
    class="org.wanghy.cache.interceptor.proxy.CacheProxyFactoryBean">
    <property name="cacheProviderFacade">
        <ref local="cacheProvider" />
    </property>
    <property name="cachingAttributes">
        <props>
            <!-- We are going to cache the return value of the methods which names start with "get" -->
            <prop key="get*">[cacheProfileId=test]</prop>
        </props>
    </property>
    <property name="cacheFlushAttributes">
        <props>
            <!-- We are going to flush the cache when the methods which names start with "update" are executed -->
            <prop key="update*">[cacheProfileIds=test,main]</prop>
        </props>
    </property>
    <property name="target">
        <ref local="cacheableTarget" />
    </property>
</bean>

Notice that the ids of the cache profiles to use are highlighted in red and blue. Cache profiles with the same ids should exist in the cache provider facade:

<bean id="cacheProvider"
    class="...">
    <property name="cacheManager">
        <ref local="cacheManager"/>
    </property>

    <!-- Cache profiles specified as a java.util.Map -->
    <property name="cacheProfiles">
        <map>
            <entry key="test">
                <!-- Profiles ommited.  They depend on the implementation of the Cache Provider. -->
            </entry>
            <entry key="main">
                <!-- Profiles ommited.  They depend on the implementation of the Cache Provider. -->
            </entry>
        </map>
    </property>

    <!-- Cache profiles specified as a java.util.Properties -->
    <property name="cacheProfiles">
        <props>
            <prop key="test">...</prop>
            <prop key="main">...</prop>
        </props>
    </property>
</bean>