Using Your Own Policy
CacheBox is incredibly flexible and if you would like to create your own eviction policy, you can! Below are a set of easy steps on how to do this:
  1. 1.
    Create a simple CFC that implements the following class cachebox.system.cache.policies.IEvictionPolicy or use our convenience abstract class and inherit from cachebox.system.cache.policies.AbstractEvictionPolicy
  2. 2.
    Create your own execute() method that will evict items (We recommend looking at existing policies to get an insight on how to do this)
  3. 3.
    Use the policy instantiation path in your cachebox provider properties

Sample Policy

1
/**
2
* FIFO Eviction Policy Command
3
*/
4
component extends="cachebox.system.cache.policies.AbstractEvictionPolicy"{
5
6
/**
7
* Constructor
8
* @cacheProvider The associated cache provider of type: cachebox.system.cache.ICacheProvider
9
*/
10
FIFO function init( required cacheProvider ){
11
super.init( arguments.cacheProvider );
12
13
return this;
14
}
15
16
/**
17
* Execute the policy
18
*/
19
function execute(){
20
var index = "";
21
22
// Get searchable index
23
try{
24
index = getAssociatedCache()
25
.getObjectStore()
26
.getIndexer()
27
.getSortedKeys( "hits", "numeric", "asc" );
28
29
// process evictions via the abstract class
30
processEvictions( index );
31
}
32
catch(Any e){
33
getLogger().error("Error sorting via store indexer #e.message# #e.detail# #e.stackTrace#.");
34
}
35
}
36
37
}
Copied!

Process Evictions:

The below code is the code used to evict objects from cache
1
<--- processEvictions --->
2
<cffunction name="processEvictions" output="false" access="private" returntype="void" hint="Abstract processing of evictions">
3
<cfargument name="index" type="array" required="true" hint="The array of metadata keys used for processing evictions"/>
4
<cfscript>
5
var oCacheManager = getAssociatedCache();
6
var indexer = oCacheManager.getObjectStore().getIndexer();
7
var indexLength = arrayLen(arguments.index);
8
var x = 1;
9
var md = "";
10
var evictCount = oCacheManager.getConfiguration().evictCount;
11
var evictedCounter = 0;
12
13
//Loop Through Metadata
14
for (x=1; x lte indexLength; x=x+1){
15
16
// verify object in indexer
17
if( NOT indexer.objectExists( arguments.index[x] ) ){
18
continue;
19
}
20
md = indexer.getObjectMetadata( arguments.index[x] );
21
22
// Evict if not already marked for eviction or an eternal object.
23
if( md.timeout gt 0 AND NOT md.isExpired ){
24
25
// Expire Object
26
oCacheManager.expireKey( arguments.index[x] );
27
28
// Record Eviction
29
oCacheManager.getStats().evictionHit();
30
evictedCounter++;
31
32
// Can we break or keep on evicting
33
if( evictedCounter GTE evictCount ){
34
break;
35
}
36
}
37
}//end for loop
38
</cfscript>
39
</cffunction>
Copied!

Configuration File

1
defaultCache = {
2
objectDefaultTimeout = 120,
3
objectDefaultLastAccessTimeout = 30,
4
useLastAccessTimeouts = true,
5
reapFrequency = 2,
6
freeMemoryPercentageThreshold = 0,
7
8
evictionPolicy = "myPath.policies.MyPolicy",
9
10
evictCount = 1,
11
maxObjects = 300,
12
objectStore = "ConcurrentSoftReferenceStore",
13
coldboxEnabled = false
14
}
Copied!
That's it folks! Very easily you can create your own eviction policies and use our built in indexers to just sort the elements in whatever way you like. If not, you can always do it yourself :)