cache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Thomas Tanghus <thomas@tanghus.net>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC;
  26. class Cache {
  27. /**
  28. * @var Cache $user_cache
  29. */
  30. static protected $user_cache;
  31. /**
  32. * @var Cache $global_cache
  33. */
  34. static protected $global_cache;
  35. /**
  36. * get the global cache
  37. * @return Cache
  38. */
  39. static public function getGlobalCache() {
  40. if (!self::$global_cache) {
  41. self::$global_cache = new Cache\FileGlobal();
  42. }
  43. return self::$global_cache;
  44. }
  45. /**
  46. * get the user cache
  47. * @return Cache
  48. */
  49. static public function getUserCache() {
  50. if (!self::$user_cache) {
  51. self::$user_cache = new Cache\File();
  52. }
  53. return self::$user_cache;
  54. }
  55. /**
  56. * get a value from the user cache
  57. * @param string $key
  58. * @return mixed
  59. */
  60. static public function get($key) {
  61. $user_cache = self::getUserCache();
  62. return $user_cache->get($key);
  63. }
  64. /**
  65. * set a value in the user cache
  66. * @param string $key
  67. * @param mixed $value
  68. * @param int $ttl
  69. * @return bool
  70. */
  71. static public function set($key, $value, $ttl=0) {
  72. if (empty($key)) {
  73. return false;
  74. }
  75. $user_cache = self::getUserCache();
  76. return $user_cache->set($key, $value, $ttl);
  77. }
  78. /**
  79. * check if a value is set in the user cache
  80. * @param string $key
  81. * @return bool
  82. */
  83. static public function hasKey($key) {
  84. $user_cache = self::getUserCache();
  85. return $user_cache->hasKey($key);
  86. }
  87. /**
  88. * remove an item from the user cache
  89. * @param string $key
  90. * @return bool
  91. */
  92. static public function remove($key) {
  93. $user_cache = self::getUserCache();
  94. return $user_cache->remove($key);
  95. }
  96. /**
  97. * clear the user cache of all entries starting with a prefix
  98. * @param string $prefix (optional)
  99. * @return bool
  100. */
  101. static public function clear($prefix='') {
  102. $user_cache = self::getUserCache();
  103. return $user_cache->clear($prefix);
  104. }
  105. /**
  106. * creates cache key based on the files given
  107. * @param string[] $files
  108. * @return string
  109. */
  110. static public function generateCacheKeyFromFiles($files) {
  111. $key = '';
  112. sort($files);
  113. foreach($files as $file) {
  114. $stat = stat($file);
  115. $key .= $file.$stat['mtime'].$stat['size'];
  116. }
  117. return md5($key);
  118. }
  119. }