PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/cache/storage/Memcached.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 97 lines | 82 code | 15 blank | 0 comment | 5 complexity | edf31c88e04f1d43c4c0a529ec78a319 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cfcomponent implements="AbstractStorage" output="false">
  2. <cffunction name="init" access="public" output="false" returntype="any">
  3. <cfargument name="servers" type="string" required="false" default="127.0.0.1:11211" />
  4. <cfargument name="timeout" type="numeric" required="false" default="60" />
  5. <cfargument name="timeunit" type="string" required="false" default="SECONDS" />
  6. <cfscript>
  7. variables.$instance = {};
  8. variables.$instance.cache = false;
  9. variables.$instance.available = false;
  10. StructAppend(variables.$instance, arguments);
  11. variables.$instance.cache = variables.$connect();
  12. </cfscript>
  13. <cfreturn this>
  14. </cffunction>
  15. <cffunction name="isAvailable" access="public" output="false" returntype="boolean">
  16. <cfreturn IsObject(variables.$instance.cache)>
  17. </cffunction>
  18. <cffunction name="set" access="public" output="false" returntype="void">
  19. <cfargument name="key" type="string" required="true">
  20. <cfargument name="value" type="any" required="true">
  21. <cfscript>
  22. variables.$instance.cache.set(arguments.key, arguments.value);
  23. </cfscript>
  24. </cffunction>
  25. <cffunction name="get" access="public" output="false" returntype="any">
  26. <cfargument name="key" type="string" required="true">
  27. <cfscript>
  28. var loc = {};
  29. loc.value = variables.$instance.cache.get(arguments.key);
  30. if (!StructKeyExists(loc, "value") || (IsSimpleValue(loc.value) && !Len(Trim(loc.value))))
  31. loc.value = false;
  32. </cfscript>
  33. <cfreturn loc.value>
  34. </cffunction>
  35. <cffunction name="evict" access="public" output="false" returntype="numeric">
  36. <cfargument name="keys" type="array" required="false" default="#ArrayNew(1)#">
  37. <cfargument name="strategy" type="any" required="true">
  38. <cfargument name="currentTime" type="date" required="true">
  39. <!--- don't do anything as memcached decides when to evict content from the cache with it's internal lru logic --->
  40. <cfreturn 0>
  41. </cffunction>
  42. <cffunction name="delete" access="public" output="false" returntype="void">
  43. <cfargument name="key" type="string" required="true">
  44. <cfscript>
  45. variables.$instance.cache.delete(arguments.key);
  46. </cfscript>
  47. </cffunction>
  48. <cffunction name="count" access="public" output="false" returntype="numeric">
  49. <cfscript>
  50. var loc = {};
  51. loc.stats = variables.$instance.cache.getStats();
  52. loc.totalItems = 0;
  53. for (loc.item in loc.stats)
  54. loc.totalItems = loc.totalItems + (loc.stats[loc.item]["curr_items"] - 2); // for some reason memcached always shows two items in the cache over what there really is
  55. </cfscript>
  56. <cfreturn loc.totalItems>
  57. </cffunction>
  58. <cffunction name="flush" access="public" output="false" returntype="void">
  59. <cfscript>
  60. variables.$instance.cache.flush();
  61. </cfscript>
  62. </cffunction>
  63. <cffunction name="$connect" access="private" output="false" returntype="any">
  64. <cfargument name="servers" type="string" required="false" default="#Trim(REReplace(variables.$instance.servers, '\s+', ' ', 'all'))#">
  65. <cfargument name="defaultTimeout" type="string" required="false" default="#variables.$instance.timeout#">
  66. <cfargument name="defaultUnit" type="string" required="false" default="#variables.$instance.timeunit#">
  67. <cfscript>
  68. var loc = {};
  69. try
  70. {
  71. loc.factory = CreateObject("component", "wheelsMapping.vendor.memcached.MemcachedFactory").init(argumentCollection=arguments);
  72. loc.memcached = loc.factory.getMemcached();
  73. if (!StructIsEmpty(loc.memcached.getVersions()))
  74. return loc.memcached;
  75. }
  76. catch (Any e)
  77. {
  78. }
  79. </cfscript>
  80. <cfreturn false>
  81. </cffunction>
  82. </cfcomponent>