cache.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * get the global cache
  19. * @return OC_Cache
  20. */
  21. static public function getGlobalCache() {
  22. if (!self::$global_cache) {
  23. self::$global_cache = new OC_Cache_FileGlobal();
  24. }
  25. return self::$global_cache;
  26. }
  27. /**
  28. * get the user cache
  29. * @return OC_Cache
  30. */
  31. static public function getUserCache() {
  32. if (!self::$user_cache) {
  33. self::$user_cache = new OC_Cache_File();
  34. }
  35. return self::$user_cache;
  36. }
  37. /**
  38. * get a value from the user cache
  39. * @param string $key
  40. * @return mixed
  41. */
  42. static public function get($key) {
  43. $user_cache = self::getUserCache();
  44. return $user_cache->get($key);
  45. }
  46. /**
  47. * set a value in the user cache
  48. * @param string $key
  49. * @param mixed $value
  50. * @param int $ttl
  51. * @return bool
  52. */
  53. static public function set($key, $value, $ttl=0) {
  54. if (empty($key)) {
  55. return false;
  56. }
  57. $user_cache = self::getUserCache();
  58. return $user_cache->set($key, $value, $ttl);
  59. }
  60. /**
  61. * check if a value is set in the user cache
  62. * @param string $key
  63. * @return bool
  64. */
  65. static public function hasKey($key) {
  66. $user_cache = self::getUserCache();
  67. return $user_cache->hasKey($key);
  68. }
  69. /**
  70. * remove an item from the user cache
  71. * @param string $key
  72. * @return bool
  73. */
  74. static public function remove($key) {
  75. $user_cache = self::getUserCache();
  76. return $user_cache->remove($key);
  77. }
  78. /**
  79. * clear the user cache of all entries starting with a prefix
  80. * @param string prefix (optional)
  81. * @return bool
  82. */
  83. static public function clear($prefix='') {
  84. $user_cache = self::getUserCache();
  85. return $user_cache->clear($prefix);
  86. }
  87. static public function generateCacheKeyFromFiles($files) {
  88. $key = '';
  89. sort($files);
  90. foreach($files as $file) {
  91. $stat = stat($file);
  92. $key .= $file.$stat['mtime'].$stat['size'];
  93. }
  94. return md5($key);
  95. }
  96. }