api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TempFileCache.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Cache;
  3. class TempFileCache implements Cache
  4. {
  5. const SUFFIX = 'cache';
  6. private $path;
  7. private $segments;
  8. public function __construct(String $prefix, String $config)
  9. {
  10. $this->segments = [];
  11. $s = DIRECTORY_SEPARATOR;
  12. $ps = PATH_SEPARATOR;
  13. if ($config == '') {
  14. $id = substr(md5(__FILE__), 0, 8);
  15. $this->path = sys_get_temp_dir() . $s . $prefix . self::SUFFIX;
  16. } elseif (strpos($config, $ps) === false) {
  17. $this->path = $config;
  18. } else {
  19. list($path, $segments) = explode($ps, $config);
  20. $this->path = $path;
  21. $this->segments = explode(',', $segments);
  22. }
  23. if (file_exists($this->path) && is_dir($this->path)) {
  24. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), false);
  25. }
  26. }
  27. private function getFileName(String $key): String
  28. {
  29. $s = DIRECTORY_SEPARATOR;
  30. $md5 = md5($key);
  31. $filename = rtrim($this->path, $s) . $s;
  32. $i = 0;
  33. foreach ($this->segments as $segment) {
  34. $filename .= substr($md5, $i, $segment) . $s;
  35. $i += $segment;
  36. }
  37. $filename .= substr($md5, $i);
  38. return $filename;
  39. }
  40. public function set(String $key, String $value, int $ttl = 0): bool
  41. {
  42. $filename = $this->getFileName($key);
  43. $dirname = dirname($filename);
  44. if (!file_exists($dirname)) {
  45. if (!mkdir($dirname, 0755, true)) {
  46. return false;
  47. }
  48. }
  49. $string = $ttl . '|' . $value;
  50. return file_put_contents($filename, $string, LOCK_EX) !== false;
  51. }
  52. private function getString($filename): String
  53. {
  54. $data = file_get_contents($filename);
  55. if ($data === false) {
  56. return '';
  57. }
  58. list($ttl, $string) = explode('|', $data, 2);
  59. if ($ttl > 0 && time() - filemtime($filename) > $ttl) {
  60. return '';
  61. }
  62. return $string;
  63. }
  64. public function get(String $key): String
  65. {
  66. $filename = $this->getFileName($key);
  67. if (!file_exists($filename)) {
  68. return '';
  69. }
  70. $string = $this->getString($filename);
  71. if ($string == null) {
  72. return '';
  73. }
  74. return $string;
  75. }
  76. private function clean(String $path, array $segments, int $len, bool $all)/*: void*/
  77. {
  78. $entries = scandir($path);
  79. foreach ($entries as $entry) {
  80. if ($entry === '.' || $entry === '..') {
  81. continue;
  82. }
  83. $filename = $path . DIRECTORY_SEPARATOR . $entry;
  84. if (count($segments) == 0) {
  85. if (strlen($entry) != $len) {
  86. continue;
  87. }
  88. if (is_file($filename)) {
  89. if ($all || $this->getString($filename) == null) {
  90. unlink($filename);
  91. }
  92. }
  93. } else {
  94. if (strlen($entry) != $segments[0]) {
  95. continue;
  96. }
  97. if (is_dir($filename)) {
  98. $this->clean($filename, array_slice($segments, 1), $len - $segments[0], $all);
  99. rmdir($filename);
  100. }
  101. }
  102. }
  103. }
  104. public function clear(): bool
  105. {
  106. if (!file_exists($this->path) || !is_dir($this->path)) {
  107. return false;
  108. }
  109. $this->clean($this->path, array_filter($this->segments), strlen(md5('')), true);
  110. return true;
  111. }
  112. }