1: <?php
2: class QCache extends QBaseClass {
3: public static $CachePath = __FILE_CACHE__;
4:
5: protected $strNamespace;
6: protected $strKey;
7: protected $strExtension;
8: protected $strCheckFilesArray;
9:
10: 11: 12:
13: public function __construct($strNamespace, $strKey, $strExtension = 'txt', $mixCheckFiles = null) {
14: $this->strNamespace = trim(strtolower($strNamespace));
15: $this->strKey = md5(trim(strtolower($strKey)));
16: $this->strExtension = trim(strtolower($strExtension));
17:
18: if (is_array($mixCheckFiles))
19: $this->strCheckFilesArray = $mixCheckFiles;
20: else if ($mixCheckFiles)
21: $this->strCheckFilesArray = array($mixCheckFiles);
22: else
23: $this->strCheckFilesArray = array();
24: }
25:
26: public function GetData() {
27:
28: if (file_exists($this->GetFilePath())) {
29: if (count($this->strCheckFilesArray)) {
30:
31: $strHash = $this->GetCheckFilesHash();
32:
33:
34: if ($strHash === false) {
35: unlink($this->GetFilePath());
36: return false;
37: }
38:
39:
40: $strHashFile = $this->GetFilePath() . '.hash';
41: if (!file_exists($strHashFile) ||
42: ($strHash != file_get_contents($strHashFile))) {
43: unlink($this->GetFilePath());
44: return false;
45: }
46: }
47:
48:
49: return file_get_contents($this->GetFilePath());
50: } else
51: return false;
52: }
53:
54: public function SaveData($strData) {
55: if (!is_dir($this->GetCacheDirectory())) {
56: mkdir($this->GetCacheDirectory(),0777,true);
57: }
58:
59: file_put_contents($this->GetFilePath(), $strData);
60:
61: if (count($this->strCheckFilesArray)) {
62: file_put_contents($this->GetFilePath() . '.hash', $this->GetCheckFilesHash());
63: }
64: }
65:
66: public function DeleteData() {
67: if (!is_dir($this->GetCacheDirectory())) {
68: return;
69: }
70:
71: if (file_exists($this->GetFilePath() . '.hash')) {
72: unlink($this->GetFilePath() . '.hash');
73: }
74: }
75:
76: 77: 78:
79: public static function ClearNamespace($strNamespace) {
80: $strNamespace = trim(strtolower($strNamespace));
81: $strCachePath = strtolower(QCache::$CachePath) . "/";
82:
83: if (substr_count($strNamespace, $strCachePath) == 0) {
84: $strDir = sprintf('%s%s', $strCachePath, $strNamespace);
85: } else {
86: $strDir = $strNamespace;
87: }
88:
89: if (!is_dir($strDir)) {
90: return false;
91: }
92:
93: $dir = opendir($strDir);
94: if (false === $dir) {
95: return false;
96: }
97:
98: while($filename = readdir($dir)) {
99: if ($filename != "." && $filename != "..") {
100: if (!is_dir($strDir."/".$filename)) {
101: unlink($strDir."/".$filename);
102: } else {
103: if(false === self::ClearNamespace($strDir.'/'.$filename)) {
104: return false;
105: }
106: rmdir($strDir.'/'.$filename);
107: }
108: }
109: }
110: closedir($dir);
111: if ($strDir != $strCachePath) {
112: rmdir($strDir);
113: }
114: return true;
115: }
116:
117: public function GetFilePath() {
118: return sprintf('%s/%s/%s.%s', QCache::$CachePath, $this->strNamespace, $this->strKey, $this->strExtension);
119: }
120:
121: protected function GetCheckFilesHash() {
122: $intFoundFileCount = 0;
123: $strData = '';
124: foreach($this->strCheckFilesArray as $strCheckFile) {
125: if (file_exists($strCheckFile)) {
126: $intFoundFileCount++;
127: $strData .= filemtime($strCheckFile);
128: }
129: }
130:
131: if ($intFoundFileCount == 0)
132: return false;
133: else
134: return $intFoundFileCount . '_' . $strData;
135: }
136:
137: protected function GetCacheDirectory() {
138: return sprintf('%s/%s', QCache::$CachePath, $this->strNamespace);
139: }
140: }