Using Caching Outside Of Mach II - Mach-II/Mach-II-Framework GitHub Wiki

Table of Contents

  1. Overview
  2. Setting Up a Cache Strategy Using ColdSpring
  3. Setting Up a Cache Strategy Without ColdSpring

Overview

If you are not familiar with the Mach-II caching package, be sure to read Introduction to Caching before reading this wiki entry.

The following sample demonstrates Using the MachII TimeSpanCache Caching outside of MachII using it with and without ColdSpring. This can be used in the scenario where you want to use Mach-II caching package in your model layer but you don't want to take advantage of the MachII's MVC functionality.

If you want to use Mach-II caching in your model layer of your application and want to utilize your caching setup in a Mach-II application, check out using the Utility Connector component to pull in the right objects into your ColdSpring bean factory.

Setting Up a Cache Strategy Using ColdSpring

Here is a ColdSpring configuration file i.e. services.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans>
        <bean id="CfCacheOutSide" class="MachII.caching.strategies.TimeSpanCache" init-method="configure">
            <constructor-arg name="parameters">
                <map>
                    <entry key="scope"><value>application</value></entry>
                    <entry key="timespan"><value>0,1,0,0</value></entry>
                    <entry key="cleanupIntervalInMinutes"><value>3</value></entry>
                </map>
            </constructor-arg>
        </bean>
    </beans>

ColdSpring will automatically call out init method for us. So you might be noticing the init-method attribute of the bean node. This is telling ColdSpring to call a method named configure after calling init and running any setter injection (which there is none in this case). We have to do this because Mach-II usually takes care of calling both init and configure for us automatically at the right moments.

Here is my index.cfm loading up ColdSpring and using my configured time span cache:

    <!--- Load up my CS bean factory --->
    <cfset factory = createObject("component","coldspring.beans.DefaultXmlBeanFactory").init() />
    <cfset factory.loadBeansFromXmlFile(expandPath("/caching/config/services.xml"), true) />

    <!--- Each cache element needs a key so I'm creating one here manually --->
    <cfset testKey = "productID=1" />

    <!--- Grab the time span cache, put a cache element data under our test key
            and get the data from the cache --->
    <cfset CfCache = factory.getBean("CfCacheOutSide") />
    <cfset CfCache.put(testkey, "my test value for cache") />
    <cfset CfCache.keyExists(testkey) />

    <!--- Let's dump the data we got back from the cache --->
    <cfdump var="#CfCache.get(testkey)#" />

Setting Up a Cache Strategy Without ColdSpring

This is just an example file. Ultimately we would want to put the cache strategy into the application or server scope so it does not get recreated on each request.

    <!--- Build our configuration parameters (this is done in XML when using Mach-II) --->
    <cfset parameters = StructNew() />
    <cfset parameters.timespan = "0,1,0,0" />
    <cfset parameters.scope = "application" />

    <!--- Create an instance of the time span cache and pass in the parameters --->
    <cfset CfCache = CreateObject("component", "MachII.caching.strategies.TimeSpanCache").init(parameters) />
        <!--- Mach-II automatically calls the configure method for us at the right time,
                but since we're without Mach-II MVC right now, we have to do it manually --->
    <cfset CfCache.configure() />

    <!--- Each cache element needs a key so I'm creating one here manually --->
    <cfset testKey = "productID=1" />

    <cfset CfCache.put(testkey, "my test value for cache") />
    <cfset CfCache.keyExists(testkey) />

    <!--- Let's dump the data we got back from the cache --->
    <cfdump var="#CfCache.get(testkey)#" />
⚠️ **GitHub.com Fallback** ⚠️