cache.php 2.2 KB

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