PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/components/libraries/cmslib/libraries/cache.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 214 lines | 129 code | 57 blank | 28 comment | 19 complexity | f4e614a20e9da82bb7e52a34eee9ce79 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. (defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. define('CMSCACHE_TIMEOUT', 1800);
  4. if(!defined('CMS_CACHE_PATH'))
  5. define('CMS_CACHE_PATH',(dirname(dirname(dirname(dirname(dirname(__FILE__)))))). '/components/libraries/cmslib/cache');
  6. class CMSCache {
  7. var $cachetype;
  8. var $cms;
  9. var $cache;
  10. function CMSCache(){
  11. // If APC is available, use it
  12. if(function_exists('apc_store')){
  13. $this->cache = new CMSCache_APC();
  14. } else {
  15. $this->cache = new CMSCache_Filesystem();
  16. }
  17. }
  18. // do a function call
  19. function call(){
  20. $numargs = func_num_args();
  21. $fname = '';
  22. $arglist = array();
  23. $arglist = func_get_args();
  24. // Get function name
  25. $fname = array_shift($arglist);
  26. // Get language so we can properly name the cached file
  27. // We know the first arg should be language since the first parameter has been shifted out in the fname above
  28. //$language = array_shift($arglist);
  29. // Get cache group
  30. //$fname = array_shift($arglist);
  31. // Fix for IIS webservers that doesn't have REQUEST_URI
  32. if(!isset($_SERVER['REQUEST_URI'])) {
  33. $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'],1 );
  34. if (isset($_SERVER['QUERY_STRING'])) {
  35. $_SERVER['REQUEST_URI'].='?'.$_SERVER['QUERY_STRING'];
  36. }
  37. }
  38. $fkey = $fname. serialize($arglist) . serialize($_SERVER['REQUEST_URI']);
  39. if(!$data = $this->cache->fetch($fkey)){
  40. $data = call_user_func_array($fname, $arglist);
  41. $this->cache->store($fkey, $data, CMSCACHE_TIMEOUT);
  42. }
  43. return $data;
  44. }
  45. function fetch($key){
  46. return $this->cache->fetch($key);
  47. }
  48. function store($key, $data, $ttl=CMSCACHE_TIMEOUT){
  49. $this->cache->store($key, $data, $ttl);
  50. }
  51. function delete($key){
  52. $this->cache->delete($key);
  53. }
  54. // clear all cache
  55. function clear(){
  56. $this->cache->clear();
  57. }
  58. }
  59. class CMSCacheBase {
  60. function fetch($key){
  61. }
  62. function store($key, $data, $ttl=CMSCACHE_TIMEOUT){
  63. }
  64. function delete($key){
  65. }
  66. // clear all cache
  67. function clear(){
  68. }
  69. }
  70. class CMSCache_Filesystem extends CMSCacheBase {
  71. // This is the function you store information with
  72. function store($key,$data,$ttl = CMSCACHE_TIMEOUT) {
  73. // Opening the file in read/write mode
  74. $h = @fopen($this->getFileName($key),'w');
  75. if (!$h) {
  76. return;
  77. //throw new Exception('Could not write to cache');
  78. }
  79. // Serializing along with the TTL
  80. $data = serialize(array(time()+$ttl,$data));
  81. if (@fwrite($h,$data)===false) {
  82. return;
  83. }
  84. fclose($h);
  85. }
  86. // The function to fetch data returns false on failure
  87. function fetch($key) {
  88. $filename = $this->getFileName($key);
  89. if (!file_exists($filename)) return false;
  90. $h = fopen($filename,'r');
  91. if (!$h) return false;
  92. // Getting a shared lock
  93. flock($h,LOCK_SH);
  94. $data = file_get_contents($filename);
  95. fclose($h);
  96. $data = @unserialize($data);
  97. if (!$data) {
  98. // If unserializing somehow didn't work out, we'll delete the file
  99. unlink($filename);
  100. return false;
  101. }
  102. if (time() > $data[0]) {
  103. // Unlinking when the file was expired
  104. unlink($filename);
  105. return false;
  106. }
  107. return $data[1];
  108. }
  109. function delete( $key ) {
  110. $filename = $this->getFileName($key);
  111. if (file_exists($filename)) {
  112. return unlink($filename);
  113. } else {
  114. return false;
  115. }
  116. }
  117. function getFileName($key) {
  118. // Use CMS_CACHE_PATH instead of mosConfig_cachepath
  119. return CMS_CACHE_PATH . '/cache_' . md5($key);
  120. //return ini_get('session.save_path') . '/cache' . md5($key);
  121. //return dirname(dirname(__FILE__)). '/cache/cache_' . md5($key);
  122. }
  123. // clear all cache
  124. function clear(){
  125. // Define the full path to your folder from root
  126. global $mosConfig_cachepath;
  127. $path = $mosConfig_cachepath;
  128. // Open the folder
  129. $dir_handle = @opendir($path);
  130. // Check if the directory is opened correctly.
  131. if($dir_handle){
  132. while (false !== ($file = readdir($dir_handle))) {
  133. if(is_file($path . $file)){
  134. if(substr($file, 0, 5) == 'cache')
  135. @unlink($path . $file);
  136. }
  137. }
  138. // Close
  139. closedir($dir_handle);
  140. }
  141. }
  142. }
  143. class CMSCache_APC extends CMSCacheBase {
  144. function fetch($key) {
  145. return apc_fetch($key);
  146. }
  147. function store($key,$data,$ttl) {
  148. return apc_store($key,$data,$ttl);
  149. }
  150. function delete($key) {
  151. return apc_delete($key);
  152. }
  153. // clear all cache
  154. function clear(){
  155. apc_clear_cache("user");
  156. }
  157. }