Chapter 3. Using Caching Advice with GemFire

An implementation of Spring.NET's ICache interface is provided. You can read about the features of Spring.NET Caching advice here. In this documentation we will show a basic configuration of the caching advice.

3.1. Caching Advice

The caching advice allows you to use string values as the keys and a GemFire .NET serialized objects as the values. To apply the Gemfire Caching advice you need to create an object definition for Spring.Data.GemFire.Caching.GemFireCache and pass a reference to a GemFire region in the constructor. In this example, the caching advice is being applied to a data access repository for Inventors. The inventors The repository is identified as a candidate to apply caching advice through the use of [Repository] attribute.

<objects xmlns="http://www.springframework.net" 
         xmlns:gfe="http://www.springframework.net/gemfire">

  <object id="inventorstore" type="Spring.Data.GemFire.Tests.InventorRepository, Spring.Data.GemFire.Tests"/>

  <!-- the ID matches the name used in the [Cache] attribute -->
  <object id="inventors" type="Spring.Data.GemFire.Caching.GemFireCache, Spring.Data.GemFire">
    <constructor-arg ref="exampleregion"/>
  </object>

  <!-- Defines multiple Advisors for Caching - the CacheResultAdvisor, CacheParameterAdvisor, and InvalidateCacheAdvisor-->
  <object id="CacheAspect" type="Spring.Aspects.Cache.CacheAspect, Spring.Aop"/>


  <!-- The AutoProxy based on attributes to apply the advisors defined in the CacheAspect -->
  <object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
    <!-- AttributeTypes selects the classes that have the RepositoryAttribute at the class level-->
    <property name="AttributeTypes" value="Spring.Stereotype.RepositoryAttribute"/>
    <!-- Interceptor names can be either of the type IAdvice, IAdvisor, or IAdvisors -->
    <!-- The CacheAspect is of the type IAdvisors -->
    <property name="InterceptorNames" value="CacheAspect"/>
  </object>
  
  
  <!-- default name is gemfire-cache -->
  <gfe:cache/>

  <gfe:pool id="gemfire-pool" subscription-enabled="true">
    <gfe:server host="localhost" port="40404"/>    
  </gfe:pool>

  <gfe:client-region id="exampleregion" pool-name="gemfire-pool"  />
  
</objects>

Spring's CacheAspect class implements the IAdvisors interface, so that it can define multiple IAdvisors, one for each of the attributes used in the caching advice: [CacheResult], [CacheParameter] and [InvalidateCache]. The InventorRepository class is shown below, it is backed by a simple dictionary, but in a real world scenario it would be backed by a database.

    [Serializable]
    [Repository]
    public sealed class InventorRepository : IInventorRepository
    {
        private IDictionary inventors = new ListDictionary();

        public InventorRepository()
        {
            Inventor tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), null);
            Inventor pupin = new Inventor("Mihajlo Pupin", new DateTime(1854, 10, 9), null);
            inventors.Add("Nikola Tesla", tesla);
            inventors.Add("Mihajlo Pupin", pupin);
        }

        [CacheResultItems("inventors", "Name")]
        public IList GetAll()
        {
            return new ArrayList(inventors.Values);
        }


        [CacheResult(CacheName = "inventors")]
        public IList GetAllNoCacheKey()
        {
            return new ArrayList(inventors.Values);
        }

        [CacheResult("inventors", "#name")]
        public Inventor Load(string name)
        {
            return (Inventor)inventors[name];
        }

        public void Save([CacheParameter("inventors", "Name")] Inventor inventor)
        {
            inventor.Nationality = "Serbian";
        }

        [InvalidateCache("inventors", Keys = "#inventor.Name")]
        public void Delete(Inventor inventor)
        {
        }

        [InvalidateCache("inventors")]
        public void DeleteAll()
        {
        }
    }

The IInventorRepository interface is shown below. Inventors have the [Serializable] attribute applies to them.

    public interface IInventorRepository
    {
        IList GetAll();
        IList GetAllNoCacheKey();
        Inventor Load(string name);
        void Save(Inventor inventor);
        void Delete(Inventor inventor);
        void DeleteAll();
    }