PageRenderTime 24ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/cache/functions.cfm

http://cfwheels.googlecode.com/
ColdFusion | 99 lines | 89 code | 10 blank | 0 comment | 5 complexity | 3c4b46aa145019d56daa9f271dfed570 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cffunction name="init" returntype="any" access="public" output="false">
  2. <cfargument name="storage" type="string" required="false" default="#application.wheels.cacheStorage#">
  3. <cfargument name="strategy" type="string" required="false" default="#application.wheels.cacheStrategy#">
  4. <cfargument name="defaultCacheTime" type="numeric" required="false" default="#application.wheels.defaultCacheTime#">
  5. <cfargument name="cacheCullPercentage" type="numeric" required="false" default="#application.wheels.cacheCullPercentage#">
  6. <cfargument name="cacheCullInterval" type="numeric" required="false" default="#application.wheels.cacheCullInterval#">
  7. <cfargument name="maximumItemsToCache" type="numeric" required="false" default="#application.wheels.maximumItemsToCache#">
  8. <cfargument name="cacheDatePart" type="string" required="false" default="#application.wheels.cacheDatePart#">
  9. <cfscript>
  10. var loc = {};
  11. // setup our instance scope
  12. variables.$instance = {};
  13. StructAppend(variables.$instance, arguments);
  14. // the actual cache of items is stored where ever the storage component is supposed to
  15. variables.$instance.cache = CreateObject("component", "storage.#arguments.storage#").init(argumentCollection=arguments);
  16. variables.$instance.strategy = CreateObject("component", "strategy.#arguments.strategy#").init(argumentCollection=arguments);
  17. </cfscript>
  18. <cfreturn this>
  19. </cffunction>
  20. <cffunction name="add" returntype="void" access="public" output="false">
  21. <cfargument name="key" type="string" required="true">
  22. <cfargument name="value" type="any" required="true">
  23. <cfargument name="time" type="numeric" required="false" default="#variables.$instance.defaultCacheTime#">
  24. <cfargument name="currentTime" type="date" required="false" default="#Now()#">
  25. <cfscript>
  26. var loc = {};
  27. // evict any
  28. if (variables.$instance.strategy.evictOnSet())
  29. variables.$instance.cache.evict(strategy=variables.$instance.strategy, currentTime=arguments.currentTime);
  30. if (count() < variables.$instance.maximumItemsToCache)
  31. {
  32. // set our stats in the cache object
  33. loc.cacheItem = {};
  34. loc.cacheItem.addedAt = arguments.currentTime;
  35. loc.cacheItem.expiresAt = DateAdd(variables.$instance.cacheDatePart, arguments.time, arguments.currentTime);
  36. loc.cacheItem.hitCount = 0;
  37. if (IsSimpleValue(arguments.value))
  38. loc.cacheItem.value = arguments.value;
  39. else
  40. loc.cacheItem.value = Duplicate(arguments.value);
  41. // always cache our value along with our metadata
  42. variables.$instance.cache.set(arguments.key, loc.cacheItem);
  43. }
  44. </cfscript>
  45. </cffunction>
  46. <cffunction name="get" returntype="any" access="public" output="false">
  47. <cfargument name="key" type="string" required="true">
  48. <cfargument name="currentTime" type="date" required="false" default="#Now()#">
  49. <cfscript>
  50. var loc = {};
  51. // we rely on the storage object to pass back the struct that we passed in, otherwise it is a miss
  52. loc.evicted = false;
  53. loc.cacheItem = variables.$instance.cache.get(arguments.key);
  54. if (IsStruct(loc.cacheItem))
  55. {
  56. // if we are going to do an eviction here, we should have each storage object handle it with a strategy object
  57. // so the strategy object needs to be able to apply it's strategy to a single key / value
  58. // do eviction
  59. if (variables.$instance.strategy.evictOnGet())
  60. {
  61. loc.keys = [ arguments.key ];
  62. loc.evicted = variables.$instance.cache.evict(keys=loc.keys, strategy=variables.$instance.strategy, currentTime=arguments.currentTime);
  63. }
  64. if (!loc.evicted)
  65. {
  66. loc.cacheItem.hitCount++;
  67. variables.$instance.cache.set(arguments.key, loc.cacheItem);
  68. return loc.cacheItem.value;
  69. }
  70. }
  71. </cfscript>
  72. <cfreturn false>
  73. </cffunction>
  74. <cffunction name="remove" returntype="void" access="public" output="false">
  75. <cfargument name="key" type="string" required="true">
  76. <cfset variables.$instance.cache.delete(arguments.key)>
  77. </cffunction>
  78. <cffunction name="count" returntype="numeric" access="public" output="false">
  79. <cfreturn variables.$instance.cache.count()>
  80. </cffunction>
  81. <cffunction name="clear" returntype="void" access="public" output="false">
  82. <cfreturn variables.$instance.cache.flush()>
  83. </cffunction>