PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/system/library/cache.php

https://bitbucket.org/jjasko/opencart_serbian
PHP | 57 lines | 44 code | 13 blank | 0 comment | 6 complexity | 33ec467866d12e63edab3e0ef062c7c6 MD5 | raw file
  1. <?php
  2. final class Cache {
  3. private $expire = 3600;
  4. public function __construct() {
  5. $files = glob(DIR_CACHE . 'cache.*');
  6. if ($files) {
  7. foreach ($files as $file) {
  8. $time = substr(strrchr($file, '.'), 1);
  9. if ($time < time()) {
  10. if (file_exists($file)) {
  11. unlink($file);
  12. clearstatcache();
  13. }
  14. }
  15. }
  16. }
  17. }
  18. public function get($key) {
  19. $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');
  20. if ($files) {
  21. $cache = file_get_contents($files[0]);
  22. return unserialize($cache);
  23. }
  24. }
  25. public function set($key, $value) {
  26. $this->delete($key);
  27. $file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . (time() + $this->expire);
  28. $handle = fopen($file, 'w');
  29. fwrite($handle, serialize($value));
  30. fclose($handle);
  31. }
  32. public function delete($key) {
  33. $files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');
  34. if ($files) {
  35. foreach ($files as $file) {
  36. if (file_exists($file)) {
  37. unlink($file);
  38. clearstatcache();
  39. }
  40. }
  41. }
  42. }
  43. }
  44. ?>