| 1 | <!----------------------------------------------------------------------- |
|---|
| 2 | ******************************************************************************** |
|---|
| 3 | Copyright 2005-2007 ColdBox Framework by Luis Majano and Ortus Solutions, Corp |
|---|
| 4 | www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com |
|---|
| 5 | ******************************************************************************** |
|---|
| 6 | |
|---|
| 7 | Author : Luis Majano |
|---|
| 8 | Date : January 18, 2007 |
|---|
| 9 | Description : |
|---|
| 10 | This is an object cache pool. |
|---|
| 11 | |
|---|
| 12 | Modification History: |
|---|
| 13 | 01/18/2007 - Created |
|---|
| 14 | -----------------------------------------------------------------------> |
|---|
| 15 | <cfcomponent name="objectPool" hint="I manage persistance for objects." output="false"> |
|---|
| 16 | |
|---|
| 17 | <!------------------------------------------- CONSTRUCTOR -------------------------------------------> |
|---|
| 18 | |
|---|
| 19 | <cffunction name="init" access="public" output="false" returntype="objectPool" hint="Constructor"> |
|---|
| 20 | <cfscript> |
|---|
| 21 | variables.instance = structnew(); |
|---|
| 22 | instance.pool = structnew(); |
|---|
| 23 | instance.pool_metadata = structnew(); |
|---|
| 24 | return this; |
|---|
| 25 | </cfscript> |
|---|
| 26 | </cffunction> |
|---|
| 27 | |
|---|
| 28 | <!------------------------------------------- PUBLIC -------------------------------------------> |
|---|
| 29 | |
|---|
| 30 | <!--- Getter/Setter For pool ---> |
|---|
| 31 | <cffunction name="getpool" access="public" returntype="struct" output="false"> |
|---|
| 32 | <cfreturn instance.pool > |
|---|
| 33 | </cffunction> |
|---|
| 34 | <cffunction name="setpool" access="public" returntype="void" output="false"> |
|---|
| 35 | <cfargument name="pool" type="struct" required="true"> |
|---|
| 36 | <cfset instance.pool = arguments.pool> |
|---|
| 37 | </cffunction> |
|---|
| 38 | |
|---|
| 39 | <!--- Getter/Setter for Pool Metdata ---> |
|---|
| 40 | <cffunction name="getpool_metadata" access="public" returntype="struct" output="false"> |
|---|
| 41 | <cfreturn instance.pool_metadata > |
|---|
| 42 | </cffunction> |
|---|
| 43 | <cffunction name="setpool_metadata" access="public" returntype="void" output="false"> |
|---|
| 44 | <cfargument name="pool_metadata" type="struct" required="true"> |
|---|
| 45 | <cfset instance.pool_metadata = arguments.pool_metadata> |
|---|
| 46 | </cffunction> |
|---|
| 47 | |
|---|
| 48 | <!--- Setter/Getter metdata property ---> |
|---|
| 49 | <cffunction name="getObjectMetadata" access="public" returntype="struct" output="false"> |
|---|
| 50 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 51 | <cfreturn instance.pool_metadata[arguments.objectKey] > |
|---|
| 52 | </cffunction> |
|---|
| 53 | <cffunction name="setObjectMetadata" access="public" returntype="void" output="false"> |
|---|
| 54 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 55 | <cfargument name="metadata" type="struct" required="true"> |
|---|
| 56 | <cfset instance.pool_metadata[arguments.objectKey] = arguments.metadata> |
|---|
| 57 | </cffunction> |
|---|
| 58 | <cffunction name="getMetadataProperty" access="public" returntype="any" output="false"> |
|---|
| 59 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 60 | <cfargument name="property" type="string" required="true"> |
|---|
| 61 | <cfreturn instance.pool_metadata[arguments.objectKey][arguments.property] > |
|---|
| 62 | </cffunction> |
|---|
| 63 | <cffunction name="setMetadataProperty" access="public" returntype="void" output="false"> |
|---|
| 64 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 65 | <cfargument name="property" type="string" required="true"> |
|---|
| 66 | <cfargument name="value" type="any" required="true"> |
|---|
| 67 | <cfset instance.pool_metadata[arguments.objectKey][arguments.property] = arguments.value > |
|---|
| 68 | </cffunction> |
|---|
| 69 | |
|---|
| 70 | <!--- Simple Object Lookup ---> |
|---|
| 71 | <cffunction name="lookup" access="public" output="false" returntype="boolean" hint="Check if an object is in cache."> |
|---|
| 72 | <!--- ************************************************************* ---> |
|---|
| 73 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 74 | <!--- ************************************************************* ---> |
|---|
| 75 | <!--- Check for Object in Cache. ---> |
|---|
| 76 | <cfreturn structKeyExists(instance.pool, arguments.objectKey) > |
|---|
| 77 | </cffunction> |
|---|
| 78 | |
|---|
| 79 | <!--- Get an object from the pool ---> |
|---|
| 80 | <cffunction name="get" access="public" output="false" returntype="any" hint="Get an object from cache. If it doesn't exist it returns a blank structure."> |
|---|
| 81 | <!--- ************************************************************* ---> |
|---|
| 82 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 83 | <!--- ************************************************************* ---> |
|---|
| 84 | <cfscript> |
|---|
| 85 | //Record Metadata |
|---|
| 86 | setMetadataProperty(arguments.objectKey,"hits", getMetaDataProperty(arguments.objectKey,"hits")+1); |
|---|
| 87 | setMetadataProperty(arguments.objectKey,"lastAccesed", now()); |
|---|
| 88 | //Return object. |
|---|
| 89 | return instance.pool[arguments.objectKey]; |
|---|
| 90 | </cfscript> |
|---|
| 91 | </cffunction> |
|---|
| 92 | |
|---|
| 93 | <!--- Set an Object in the pool ---> |
|---|
| 94 | <cffunction name="set" access="public" output="false" returntype="void" hint="sets an object in cache."> |
|---|
| 95 | <!--- ************************************************************* ---> |
|---|
| 96 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 97 | <cfargument name="MyObject" type="any" required="true"> |
|---|
| 98 | <cfargument name="Timeout" type="string" required="false" default="" hint="Timeout in minutes. If timeout = 0 then object never times out. If timeout is blank, then timeout will be inherited from framework."> |
|---|
| 99 | <!--- ************************************************************* ---> |
|---|
| 100 | <cfscript> |
|---|
| 101 | var MetaData = structnew(); |
|---|
| 102 | //Set new Object into cache. |
|---|
| 103 | instance.pool[arguments.objectKey] = arguments.MyObject; |
|---|
| 104 | //Create object's metdata |
|---|
| 105 | MetaData.hits = 1; |
|---|
| 106 | MetaData.Timeout = arguments.timeout; |
|---|
| 107 | MetaData.Created = now(); |
|---|
| 108 | MetaData.LastAccesed = now(); |
|---|
| 109 | //Set the metadata |
|---|
| 110 | setObjectMetaData(arguments.objectkey,MetaData); |
|---|
| 111 | </cfscript> |
|---|
| 112 | </cffunction> |
|---|
| 113 | |
|---|
| 114 | <!--- Clear an object from the pool ---> |
|---|
| 115 | <cffunction name="clearKey" access="public" output="false" returntype="boolean" hint="Clears a key from the cache."> |
|---|
| 116 | <!--- ************************************************************* ---> |
|---|
| 117 | <cfargument name="objectKey" type="string" required="true"> |
|---|
| 118 | <!--- ************************************************************* ---> |
|---|
| 119 | <cfscript> |
|---|
| 120 | var Results = false; |
|---|
| 121 | try{ |
|---|
| 122 | structDelete(instance.pool,arguments.objectKey); |
|---|
| 123 | structDelete(instance.pool_metadata,arguments.objectKey); |
|---|
| 124 | Results = true; |
|---|
| 125 | } |
|---|
| 126 | catch(Any e){ |
|---|
| 127 | //Nothing; |
|---|
| 128 | } |
|---|
| 129 | return Results; |
|---|
| 130 | </cfscript> |
|---|
| 131 | </cffunction> |
|---|
| 132 | |
|---|
| 133 | <!--- Get the size of the pool ---> |
|---|
| 134 | <cffunction name="getSize" access="public" output="false" returntype="numeric" hint="Get the cache's size in items"> |
|---|
| 135 | <cfscript> |
|---|
| 136 | return StructCount(instance.pool); |
|---|
| 137 | </cfscript> |
|---|
| 138 | </cffunction> |
|---|
| 139 | |
|---|
| 140 | <!--- Get the itemList ---> |
|---|
| 141 | <cffunction name="getObjectsKeyList" access="public" output="false" returntype="string" hint="Get the cache's object entries listing."> |
|---|
| 142 | <cfscript> |
|---|
| 143 | return structKeyList(instance.pool); |
|---|
| 144 | </cfscript> |
|---|
| 145 | </cffunction> |
|---|
| 146 | |
|---|
| 147 | <!------------------------------------------- PRIVATE -------------------------------------------> |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | </cfcomponent> |
|---|