cache.file.func.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('file');
  8. define('CACHE_FILE_PATH', IA_ROOT . '/data/cache/');
  9. function cache_read($key, $dir = '', $include = true) {
  10. $key = str_replace(':', '@', $key);
  11. $key = CACHE_FILE_PATH . $key;
  12. if (!is_file($key)) {
  13. return array();
  14. }
  15. return $include ? include $key : file_get_contents($key);
  16. }
  17. function cache_write($key, $data, $dir = '') {
  18. global $_W;
  19. if (empty($key) || !isset($data)) {
  20. return false;
  21. }
  22. $key = str_replace(':', '@', $key);
  23. if (!is_string($data)) {
  24. $data = "<?php \r\ndefined('IN_IA') or exit('Access Denied');\r\nreturn " . var_export($data, true) . ';';
  25. }
  26. $key = CACHE_FILE_PATH . $key;
  27. mkdirs(dirname($key));
  28. file_put_contents($key, $data);
  29. @chmod($key, $_W['config']['setting']['filemode']);
  30. return is_file($key);
  31. }
  32. function cache_delete($key, $dir = '') {
  33. $cache_relation_keys = cache_relation_keys($key);
  34. if (is_error($cache_relation_keys)) {
  35. return $cache_relation_keys;
  36. }
  37. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  38. foreach ($cache_relation_keys as $key) {
  39. $cache_info = cache_load($key);
  40. if (!empty($cache_info)) {
  41. $key = str_replace(':', '@', $key);
  42. $key = CACHE_FILE_PATH . $key;
  43. $result = file_delete($key);
  44. if (!$result) {
  45. return error(1, '缓存: ' . $key . ' 删除失败!');
  46. }
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. function cache_clean($dir = '') {
  53. return rmdirs(CACHE_FILE_PATH, true);
  54. }