fileglobal.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Cache;
  9. class FileGlobal {
  10. static protected function getCacheDir() {
  11. $cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
  12. if (!is_dir($cache_dir)) {
  13. mkdir($cache_dir);
  14. }
  15. return $cache_dir;
  16. }
  17. protected function fixKey($key) {
  18. return str_replace('/', '_', $key);
  19. }
  20. public function get($key) {
  21. $key = $this->fixKey($key);
  22. if ($this->hasKey($key)) {
  23. $cache_dir = self::getCacheDir();
  24. return file_get_contents($cache_dir.$key);
  25. }
  26. return null;
  27. }
  28. public function set($key, $value, $ttl=0) {
  29. $key = $this->fixKey($key);
  30. $cache_dir = self::getCacheDir();
  31. if ($cache_dir and file_put_contents($cache_dir.$key, $value)) {
  32. if ($ttl === 0) {
  33. $ttl = 86400; // 60*60*24
  34. }
  35. return touch($cache_dir.$key, time() + $ttl);
  36. }
  37. return false;
  38. }
  39. public function hasKey($key) {
  40. $key = $this->fixKey($key);
  41. $cache_dir = self::getCacheDir();
  42. if ($cache_dir && is_file($cache_dir.$key)) {
  43. $mtime = filemtime($cache_dir.$key);
  44. if ($mtime < time()) {
  45. unlink($cache_dir.$key);
  46. return false;
  47. }
  48. return true;
  49. }
  50. return false;
  51. }
  52. public function remove($key) {
  53. $cache_dir = self::getCacheDir();
  54. if(!$cache_dir) {
  55. return false;
  56. }
  57. $key = $this->fixKey($key);
  58. return unlink($cache_dir.$key);
  59. }
  60. public function clear($prefix='') {
  61. $cache_dir = self::getCacheDir();
  62. $prefix = $this->fixKey($prefix);
  63. if($cache_dir and is_dir($cache_dir)) {
  64. $dh=opendir($cache_dir);
  65. if(is_resource($dh)) {
  66. while (($file = readdir($dh)) !== false) {
  67. if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
  68. unlink($cache_dir.$file);
  69. }
  70. }
  71. }
  72. }
  73. }
  74. static public function gc() {
  75. $last_run = \OC_AppConfig::getValue('core', 'global_cache_gc_lastrun', 0);
  76. $now = time();
  77. if (($now - $last_run) < 300) {
  78. // only do cleanup every 5 minutes
  79. return;
  80. }
  81. \OC_AppConfig::setValue('core', 'global_cache_gc_lastrun', $now);
  82. $cache_dir = self::getCacheDir();
  83. if($cache_dir and is_dir($cache_dir)) {
  84. $dh=opendir($cache_dir);
  85. if(is_resource($dh)) {
  86. while (($file = readdir($dh)) !== false) {
  87. if($file!='.' and $file!='..') {
  88. $mtime = filemtime($cache_dir.$file);
  89. if ($mtime < $now) {
  90. unlink($cache_dir.$file);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }