-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsCache.php
More file actions
237 lines (215 loc) · 5.67 KB
/
NetCommonsCache.php
File metadata and controls
237 lines (215 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* NetCommons用キャッシュ Utility
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Cache', 'Cache');
/**
* Configure the cache used for general framework caching. Path information,
* object listings, and translation cache files are stored with this configuration.
*/
$cacheSetting = Cache::settings('_cake_core_');
$cacheSetting['duration'] = '+999 days';
$cacheSetting['prefix'] =
preg_replace('/cake_core_/', 'netcommons_core_', $cacheSetting['prefix']);
Cache::config('netcommons_core', $cacheSetting);
$cacheSetting = Cache::settings('_cake_model_');
$cacheSetting['duration'] = '+999 days';
$cacheSetting['prefix'] =
preg_replace('/cake_model_/', 'netcommons_model_', $cacheSetting['prefix']);
Cache::config('netcommons_model', $cacheSetting);
/**
* NetCommons用キャッシュ Utility
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Utility
*/
class NetCommonsCache {
/**
* キャッシュの設定値
*
* @var array
*/
private $__setting = [];
/**
* キャッシュのデータ保持用
*
* @var array
*/
private $__data = [];
/**
* キャッシュ名
*
* @var string
*/
private $__cacheName = '';
/**
* キャッシュ種別
*
* @var string
*/
private $__cacheType = '';
/**
* テストか否か
*
* @var bool
*/
private $__isTest = false;
/**
* インストールされているか否か
*
* @var bool
*/
private $__isInstalled = false;
/**
* コンストラクタ
*
* @param string $cacheName キャッシュ名
* @param bool $isTest テストか否か
* @param string $cacheType キャッシュ種別
* @return void
*/
public function __construct($cacheName, $isTest, $cacheType = 'netcommons_core') {
if (defined('NC3_VERSION') && $cacheName !== 'version') {
$this->__cacheName = $cacheName . '_' . preg_replace('/\./', '_', NC3_VERSION);
} else {
$this->__cacheName = $cacheName;
}
$this->__isTest = $isTest;
$this->__cacheType = $cacheType;
$this->__setting = Cache::settings($this->__cacheType);
$this->__isInstalled = Configure::read('NetCommons.installed');
if (! $this->__isTest && $this->__isInstalled) {
$result = Cache::read($this->__cacheName, $this->__cacheType);
if ($result !== false) {
$this->__data = $result;
}
}
}
/**
* 空か否か
*
* @return bool
*/
public function isEmpty() {
return empty($this->__data);
}
/**
* キャッシュ変数からの読み込み
*
* @param string|null $mainKey メインキー
* @param string|null $subKey サブキー
* @return array|null|string|int|bool
*/
public function read($mainKey = null, $subKey = null) {
if (is_null($mainKey)) {
return $this->__data;
} elseif (is_null($subKey)) {
if (isset($this->__data[$mainKey])) {
return $this->__data[$mainKey];
} else {
return null;
}
} else {
if (isset($this->__data[$mainKey][$subKey])) {
return $this->__data[$mainKey][$subKey];
} else {
return null;
}
}
}
/**
* キャッシュの書き込み
*
* @param array|int|string|bool $value キャッシュに書き込む値
* @param string|null $mainKey メインキー
* @param string|null $subKey サブキー
* @return void
*/
public function write($value, $mainKey = null, $subKey = null) {
if (is_null($mainKey)) {
$this->__data = $value;
} elseif (is_null($subKey)) {
$this->__data[$mainKey] = $value;
} else {
$this->__data[$mainKey][$subKey] = $value;
}
if (! $this->__isTest && $this->__isInstalled) {
Cache::write($this->__cacheName, $this->__data, $this->__cacheType);
}
}
/**
* キャッシュから削除
*
* @param string|null $mainKey メインキー
* @param string|null $subKey サブキー
* @return void
*/
public function delete($mainKey = null, $subKey = null) {
if (is_null($mainKey)) {
$this->__data = [];
} elseif (is_null($subKey)) {
if (isset($this->__data[$mainKey])) {
unset($this->__data[$mainKey]);
}
} else {
if (isset($this->__data[$mainKey][$subKey])) {
unset($this->__data[$mainKey][$subKey]);
}
}
if (! $this->__isTest && $this->__isInstalled) {
$success = Cache::write($this->__cacheName, $this->__data, $this->__cacheType);
if (! $success) {
$this->__triggerWarning('delete');
}
}
}
/**
* キャッシュのクリア
*
* @return void
*/
public function clear() {
$this->__data = [];
if (! $this->__isTest && $this->__isInstalled) {
//$engine = Cache::engine($this->__cacheType);
//if ($engine && $engine->key($this->__cacheName)) {
// $success = Cache::delete($this->__cacheName, $this->__cacheType);
Cache::delete($this->__cacheName, $this->__cacheType);
//} else {
// $success = true;
//}
//if (! $success) {
// $this->__triggerWarning('clear');
//}
}
}
/**
* キャッシュのクリア処理や削除処理が失敗した際にWarningを発生させる
*
* @param string $type 種別。'delete' or 'clear'
* @return void
*/
private function __triggerWarning($type) {
if ($type === 'delete') {
$message = __d(
'net_commons',
'Could not write to the %s cache file. Please check the file permissions.',
$this->__setting['path'] . $this->__setting['prefix'] . $this->__cacheName
);
} else {
$message = __d(
'net_commons',
'The %s cache file could not be deleted. ' .
'Check the permissions of the file and delete the cache file.',
$this->__setting['path'] . $this->__setting['prefix'] . $this->__cacheName
);
}
trigger_error($message, E_USER_WARNING);
}
}