file.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 File {
  10. protected $storage;
  11. protected function getStorage() {
  12. if (isset($this->storage)) {
  13. return $this->storage;
  14. }
  15. if(\OC_User::isLoggedIn()) {
  16. \OC\Files\Filesystem::initMountPoints(\OC_User::getUser());
  17. $subdir = 'cache';
  18. $view = new \OC\Files\View('/' . \OC_User::getUser());
  19. if(!$view->file_exists($subdir)) {
  20. $view->mkdir($subdir);
  21. }
  22. $this->storage = new \OC\Files\View('/' . \OC_User::getUser().'/'.$subdir);
  23. return $this->storage;
  24. }else{
  25. \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
  26. return false;
  27. }
  28. }
  29. public function get($key) {
  30. $result = null;
  31. $proxyStatus = \OC_FileProxy::$enabled;
  32. \OC_FileProxy::$enabled = false;
  33. if ($this->hasKey($key)) {
  34. $storage = $this->getStorage();
  35. $result = $storage->file_get_contents($key);
  36. }
  37. \OC_FileProxy::$enabled = $proxyStatus;
  38. return $result;
  39. }
  40. /**
  41. * Returns the size of the stored/cached data
  42. *
  43. * @param $key
  44. * @return int
  45. */
  46. public function size($key) {
  47. $result = 0;
  48. $proxyStatus = \OC_FileProxy::$enabled;
  49. \OC_FileProxy::$enabled = false;
  50. if ($this->hasKey($key)) {
  51. $storage = $this->getStorage();
  52. $result = $storage->filesize($key);
  53. }
  54. \OC_FileProxy::$enabled = $proxyStatus;
  55. return $result;
  56. }
  57. public function set($key, $value, $ttl=0) {
  58. $storage = $this->getStorage();
  59. $result = false;
  60. $proxyStatus = \OC_FileProxy::$enabled;
  61. \OC_FileProxy::$enabled = false;
  62. if ($storage and $storage->file_put_contents($key, $value)) {
  63. if ($ttl === 0) {
  64. $ttl = 86400; // 60*60*24
  65. }
  66. $result = $storage->touch($key, time() + $ttl);
  67. }
  68. \OC_FileProxy::$enabled = $proxyStatus;
  69. return $result;
  70. }
  71. public function hasKey($key) {
  72. $storage = $this->getStorage();
  73. if ($storage && $storage->is_file($key)) {
  74. $mtime = $storage->filemtime($key);
  75. if ($mtime < time()) {
  76. $storage->unlink($key);
  77. return false;
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. public function remove($key) {
  84. $storage = $this->getStorage();
  85. if(!$storage) {
  86. return false;
  87. }
  88. return $storage->unlink($key);
  89. }
  90. public function clear($prefix='') {
  91. $storage = $this->getStorage();
  92. if($storage and $storage->is_dir('/')) {
  93. $dh=$storage->opendir('/');
  94. if(is_resource($dh)) {
  95. while (($file = readdir($dh)) !== false) {
  96. if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
  97. $storage->unlink('/'.$file);
  98. }
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. public function gc() {
  105. $storage = $this->getStorage();
  106. if($storage and $storage->is_dir('/')) {
  107. $now = time();
  108. $dh=$storage->opendir('/');
  109. if(!is_resource($dh)) {
  110. return null;
  111. }
  112. while (($file = readdir($dh)) !== false) {
  113. if($file!='.' and $file!='..') {
  114. $mtime = $storage->filemtime('/'.$file);
  115. if ($mtime < $now) {
  116. $storage->unlink('/'.$file);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. public static function loginListener() {
  123. $c = new self();
  124. $c->gc();
  125. }
  126. }