cache.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_Cache {
  9. /**
  10. * @var OC_Cache $user_cache
  11. */
  12. static protected $user_cache;
  13. /**
  14. * @var OC_Cache $global_cache
  15. */
  16. static protected $global_cache;
  17. /**
  18. * @var OC_Cache $global_cache_fast
  19. */
  20. static protected $global_cache_fast;
  21. /**
  22. * @var OC_Cache $user_cache_fast
  23. */
  24. static protected $user_cache_fast;
  25. static protected $isFast=null;
  26. /**
  27. * get the global cache
  28. * @return OC_Cache
  29. */
  30. static public function getGlobalCache($fast=false) {
  31. if (!self::$global_cache) {
  32. self::$global_cache_fast = null;
  33. if (!self::$global_cache_fast && function_exists('xcache_set')) {
  34. self::$global_cache_fast = new OC_Cache_XCache(true);
  35. }
  36. if (!self::$global_cache_fast && function_exists('apc_store')) {
  37. self::$global_cache_fast = new OC_Cache_APC(true);
  38. }
  39. self::$global_cache = new OC_Cache_FileGlobal();
  40. if (self::$global_cache_fast) {
  41. self::$global_cache = new OC_Cache_Broker(self::$global_cache_fast, self::$global_cache);
  42. }
  43. }
  44. if($fast) {
  45. if(self::$global_cache_fast) {
  46. return self::$global_cache_fast;
  47. }else{
  48. return false;
  49. }
  50. }
  51. return self::$global_cache;
  52. }
  53. /**
  54. * get the user cache
  55. * @return OC_Cache
  56. */
  57. static public function getUserCache($fast=false) {
  58. if (!self::$user_cache) {
  59. self::$user_cache_fast = null;
  60. if (!self::$user_cache_fast && function_exists('xcache_set')) {
  61. self::$user_cache_fast = new OC_Cache_XCache();
  62. }
  63. if (!self::$user_cache_fast && function_exists('apc_store')) {
  64. self::$user_cache_fast = new OC_Cache_APC();
  65. }
  66. self::$user_cache = new OC_Cache_File();
  67. if (self::$user_cache_fast) {
  68. self::$user_cache = new OC_Cache_Broker(self::$user_cache_fast, self::$user_cache);
  69. }
  70. }
  71. if($fast) {
  72. if(self::$user_cache_fast) {
  73. return self::$user_cache_fast;
  74. }else{
  75. return false;
  76. }
  77. }
  78. return self::$user_cache;
  79. }
  80. /**
  81. * get a value from the user cache
  82. * @return mixed
  83. */
  84. static public function get($key) {
  85. $user_cache = self::getUserCache();
  86. return $user_cache->get($key);
  87. }
  88. /**
  89. * set a value in the user cache
  90. * @return bool
  91. */
  92. static public function set($key, $value, $ttl=0) {
  93. if (empty($key)) {
  94. return false;
  95. }
  96. $user_cache = self::getUserCache();
  97. return $user_cache->set($key, $value, $ttl);
  98. }
  99. /**
  100. * check if a value is set in the user cache
  101. * @return bool
  102. */
  103. static public function hasKey($key) {
  104. $user_cache = self::getUserCache();
  105. return $user_cache->hasKey($key);
  106. }
  107. /**
  108. * remove an item from the user cache
  109. * @return bool
  110. */
  111. static public function remove($key) {
  112. $user_cache = self::getUserCache();
  113. return $user_cache->remove($key);
  114. }
  115. /**
  116. * clear the user cache of all entries starting with a prefix
  117. * @param string prefix (optional)
  118. * @return bool
  119. */
  120. static public function clear($prefix='') {
  121. $user_cache = self::getUserCache();
  122. return $user_cache->clear($prefix);
  123. }
  124. /**
  125. * check if a fast memory based cache is available
  126. * @return true
  127. */
  128. static public function isFast() {
  129. if(is_null(self::$isFast)) {
  130. self::$isFast=function_exists('xcache_set') || function_exists('apc_store');
  131. }
  132. return self::$isFast;
  133. }
  134. static public function generateCacheKeyFromFiles($files) {
  135. $key = '';
  136. sort($files);
  137. foreach($files as $file) {
  138. $stat = stat($file);
  139. $key .= $file.$stat['mtime'].$stat['size'];
  140. }
  141. return md5($key);
  142. }
  143. }