memory_driver_file.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: memory_driver_yac.php 27635 2017-02-02 17:02:46Z NaiXiaoxIN $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class memory_driver_file {
  12. public $cacheName = 'File';
  13. public $enable;
  14. public $path;
  15. public function env() {
  16. return true;
  17. }
  18. public function init($config) {
  19. $this->path = $config['server'].'/';
  20. if($config['server']) {
  21. $this->enable = is_dir(DISCUZ_ROOT.$this->path);
  22. if(!$this->enable) {
  23. dmkdir(DISCUZ_ROOT.$this->path);
  24. $this->enable = is_dir(DISCUZ_ROOT.$this->path);
  25. }
  26. } else {
  27. $this->enable = false;
  28. }
  29. }
  30. private function cachefile($key) {
  31. return str_replace('_', '/', $key).'/'.$key;
  32. }
  33. public function get($key) {
  34. $file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
  35. if(!file_exists($file)) {
  36. return false;
  37. }
  38. $data = null;
  39. @include $file;
  40. if($data !== null) {
  41. if($data['exp'] && $data['exp'] < TIMESTAMP) {
  42. return false;
  43. } else {
  44. return $data['data'];
  45. }
  46. } else {
  47. return false;
  48. }
  49. }
  50. public function set($key, $value, $ttl = 0) {
  51. $file = DISCUZ_ROOT.$this->path.$this->cachefile($key).'.php';
  52. dmkdir(dirname($file));
  53. $data = array(
  54. 'exp' => $ttl ? TIMESTAMP + $ttl : 0,
  55. 'data' => $value,
  56. );
  57. file_put_contents($file, "<?php\n\$data = ".var_export($data, 1).";\n");
  58. return true;
  59. }
  60. public function rm($key) {
  61. return @unlink(DISCUZ_ROOT.$this->path.$this->cachefile($key));
  62. }
  63. private function dir_clear($dir) {
  64. if($directory = @dir($dir)) {
  65. while($entry = $directory->read()) {
  66. $filename = $dir.'/'.$entry;
  67. if($entry != '.' && $entry != '..') {
  68. if(is_file($filename)) {
  69. @unlink($filename);
  70. } else {
  71. $this->dir_clear($filename);
  72. @rmdir($filename);
  73. }
  74. }
  75. }
  76. $directory->close();
  77. }
  78. }
  79. public function clear() {
  80. return $this->dir_clear(DISCUZ_ROOT.$this->path);
  81. }
  82. public function inc($key, $step = 1) {
  83. $old = $this->get($key);
  84. if (!$old) {
  85. return false;
  86. }
  87. return $this->set($key, $old + $step);
  88. }
  89. public function dec($key, $step = 1) {
  90. $old = $this->get($key);
  91. if (!$old) {
  92. return false;
  93. }
  94. return $this->set($key, $old - $step);
  95. }
  96. }