File.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @author Jinhui Zhu<jinhui.zhu@live.cn> 2015-10-26 21:44
  4. *
  5. * Useage:
  6. * 在control.php中使用
  7. * $this->setCache('file', array('path' => 'tmp'));
  8. * $this->cache->set(id, data, policy);
  9. * $this->cache->get(id);
  10. * $this->cache->remove(id);
  11. */
  12. class Qii_Cache_File implements Qii_Cache_Intf
  13. {
  14. const VERSION = '1.2';
  15. public $policy = array('path' => 'tmp', 'life_time' => 3600, 'prefix' => 'file');//设置目录、过期时间、文件前缀
  16. public $exclude = array();
  17. public function __construct(array $policy = null)
  18. {
  19. if (!empty($policy)) {
  20. $this->policy = array_merge($this->policy, $policy);
  21. }
  22. $this->exclude = array();
  23. $this->exclude[] = Qii_DIR;
  24. }
  25. /**
  26. * 初始化 将规则合并
  27. *
  28. * @param array|null $policy
  29. */
  30. public function initialization(array $policy = null)
  31. {
  32. if (!empty($policy)) {
  33. $this->policy = array_merge($this->policy, $policy);
  34. }
  35. }
  36. /**
  37. * 检查是否可以保存到指定的目录
  38. *
  39. */
  40. public function checkIsSave()
  41. {
  42. if (!is_dir($this->policy['path'])) mkdir($this->policy['path'], 0777);
  43. if (is_dir($this->policy['path'])) {
  44. $this->policy['path'] = \Qii_Autoloader_Psr4::realpath($this->policy['path']);
  45. } else {
  46. \Qii::setError(false, __LINE__, 1401, $this->policy['path']);
  47. }
  48. //如果在系统目录就不让保存
  49. if (in_array($this->policy['path'], $this->exclude)) {
  50. throw new \Qii_Execptions_AccessDenied($this->policy['path']);
  51. }
  52. }
  53. /**
  54. * 获取文件名称
  55. *
  56. * @param String $id
  57. * @return String
  58. */
  59. public function getFileName($id)
  60. {
  61. $fileName = $this->policy['path'] . '/' . $this->policy['prefix'] . '.' . $id . '.' . (time() + $this->policy['life_time']);
  62. $fileName = $fileName . '.' . md5($fileName);
  63. return $fileName;
  64. }
  65. /**
  66. * 检查文件是否存在
  67. *
  68. * @param Int $id
  69. */
  70. public function scanFile($id)
  71. {
  72. $fileArray = glob($this->policy['path'] . '/' . $this->policy['prefix'] . '.' . $id . '.*');
  73. return $fileArray;
  74. }
  75. /**
  76. * 缓存数据
  77. *
  78. * @param $id
  79. * @param $data
  80. * @param array|null $policy
  81. * @return bool|int|void
  82. * @throws AccessDeniedExecption
  83. */
  84. public function set($id, $data, array $policy = null)//设置
  85. {
  86. if (!empty($policy)) {
  87. $this->policy = array_merge($this->policy, $policy);
  88. }
  89. $this->checkIsSave();
  90. $fileName = $this->getFileName($id);
  91. //检查文件是否存在,存在就先删除再保存
  92. $this->remove($id);
  93. return file_put_contents($fileName, serialize($data), LOCK_EX);
  94. }
  95. /**
  96. * 获取指定key的缓存
  97. *
  98. * @param $id
  99. * @return mixed|void
  100. * @throws AccessDeniedExecption
  101. */
  102. public function get($id)
  103. {
  104. $this->checkIsSave();
  105. $fileArray = glob($this->policy['path'] . '/' . $this->policy['prefix'] . '.' . $id . '.*');
  106. //检查文件是否存在
  107. if (count($fileArray) == 0) {
  108. return;
  109. }
  110. $fileName = $fileArray[0];
  111. //检查文件是否过期,如果过期就返回空
  112. $fileInfo = explode(".", $fileName);
  113. $time = $fileInfo[count($fileInfo) - 2];
  114. if ($this->policy['life_time'] > 0 && $time < time()) {
  115. return;
  116. }
  117. return unserialize(file_get_contents($fileName));
  118. }
  119. /**
  120. * 移除指定key的缓存
  121. *
  122. * @param $key
  123. */
  124. public function remove($key)
  125. {
  126. $fileArray = $this->scanFile($key);//检查文件是否存在,存在就先删除再保存
  127. foreach ($fileArray AS $file) {
  128. unlink($file);
  129. }
  130. }
  131. /**
  132. * 清除所有缓存
  133. *
  134. * @throws AccessDeniedExecption
  135. */
  136. public function clean()
  137. {
  138. $this->checkIsSave();
  139. //禁止清除Qii目录文件
  140. $handle = opendir($this->policy['path']);
  141. if ($handle) {
  142. while ($file = readdir($handle)) {
  143. if (is_file($this->policy['path'] . '/' . $file)) {
  144. unlink($this->policy['path'] . '/' . $file);
  145. }
  146. }
  147. }
  148. closedir($handle);
  149. }
  150. }