JinrouCache:プロパティを最適化
@@ -8,7 +8,7 @@ | ||
8 | 8 | const LOG_LIST = 'old_log_list'; |
9 | 9 | |
10 | 10 | //クラスのロード |
11 | - static function Load($name = null, $expire = null) { | |
11 | + static function Load($name = null, $expire = 0) { | |
12 | 12 | static $instance; |
13 | 13 | |
14 | 14 | if (is_null($instance)) { |
@@ -103,7 +103,9 @@ | ||
103 | 103 | //保存情報取得 |
104 | 104 | public static function Fetch($serialize = false) { |
105 | 105 | $data = JinrouCacheDB::Get(); |
106 | - if (self::Expire($data)) return null; | |
106 | + if (self::Expire($data)) { | |
107 | + return null; | |
108 | + } | |
107 | 109 | |
108 | 110 | self::Load()->updated = true; |
109 | 111 | self::Load()->next = $data['expire']; |
@@ -129,7 +131,9 @@ | ||
129 | 131 | |
130 | 132 | //保存処理 |
131 | 133 | public static function Store($object, $serialize = false, $force = false) { |
132 | - if (false === $force && self::Load()->updated) return; | |
134 | + if (false === $force && self::Load()->updated) { | |
135 | + return true; | |
136 | + } | |
133 | 137 | |
134 | 138 | $content = gzdeflate($serialize ? serialize($object) : $object); |
135 | 139 | if (JinrouCacheDB::Exists()) { //存在するならロックする |
@@ -169,7 +173,9 @@ | ||
169 | 173 | |
170 | 174 | //有効期限切れ判定 |
171 | 175 | private static function Expire($data) { |
172 | - if (is_null($data) || Time::Get() > $data['expire']) return true; | |
176 | + if (is_null($data) || Time::Get() > $data['expire']) { | |
177 | + return true; | |
178 | + } | |
173 | 179 | return isset(self::Load()->hash) && isset($data['hash']) && self::Load()->hash != $data['hash']; |
174 | 180 | } |
175 | 181 | } |
@@ -178,20 +184,21 @@ | ||
178 | 184 | final class JinrouCache { |
179 | 185 | public $room_no = 0; |
180 | 186 | public $name = null; |
187 | + public $now = null; | |
181 | 188 | public $expire = 0; |
189 | + public $next = null; | |
182 | 190 | public $hash = null; |
183 | 191 | public $updated = false; //更新済み判定 |
184 | - public $next = null; | |
185 | 192 | |
186 | 193 | //クラスの初期化 |
187 | 194 | public function __construct($name, $expire = 0) { |
188 | 195 | $this->room_no = DB::ExistsRoom() ? DB::$ROOM->id : 0; |
189 | 196 | $this->name = $name; |
197 | + $this->now = Time::Get(); | |
190 | 198 | $this->expire = $expire; |
199 | + $this->next = $this->now + $this->expire; | |
191 | 200 | if (DB::ExistsRoom() && DB::ExistsUser()) { |
192 | 201 | $this->hash = md5(DB::$ROOM->scene . DB::$USER->Count()); |
193 | - } else { | |
194 | - $this->hash = null; | |
195 | 202 | } |
196 | 203 | } |
197 | 204 |
@@ -25,16 +25,13 @@ | ||
25 | 25 | ->Into(['room_no', 'name', 'content', 'expire', 'hash'])->Duplicate(); |
26 | 26 | |
27 | 27 | $filter = JinrouCacheManager::Load(); |
28 | - $now = Time::Get(); | |
29 | - $expire = $now + $filter->expire; | |
30 | - $filter->next = $expire; | |
28 | + if (CacheConfig::DEBUG_MODE) { | |
29 | + self::OutputTime($filter, 'Insert'); | |
30 | + } | |
31 | 31 | $list = [ |
32 | - $filter->room_no, $filter->GetName(true), $content, $expire, $filter->hash, | |
33 | - $content, $expire, $filter->hash | |
32 | + $filter->room_no, $filter->GetName(true), $content, $filter->next, $filter->hash, | |
33 | + $content, $filter->next, $filter->hash | |
34 | 34 | ]; |
35 | - if (CacheConfig::DEBUG_MODE) { | |
36 | - self::OutputTime($now, $expire, 'Insert'); | |
37 | - } | |
38 | 35 | |
39 | 36 | DB::Prepare($query->Build(), $list); |
40 | 37 | return DB::Execute(); |
@@ -45,13 +42,10 @@ | ||
45 | 42 | $query = self::GetQueryUpdate()->Update()->Where(['room_no', 'name']); |
46 | 43 | |
47 | 44 | $filter = JinrouCacheManager::Load(); |
48 | - $now = Time::Get(); | |
49 | - $expire = $now + $filter->expire; | |
50 | - $filter->next = $expire; | |
51 | - $list = [$content, $expire, $filter->hash, $filter->room_no, $filter->GetName(true)]; | |
52 | 45 | if (CacheConfig::DEBUG_MODE) { |
53 | - self::OutputTime($now, $expire, 'Updated'); | |
46 | + self::OutputTime($filter, 'Updated'); | |
54 | 47 | } |
48 | + $list = [$content, $filter->next, $filter->hash, $filter->room_no, $filter->GetName(true)]; | |
55 | 49 | |
56 | 50 | DB::Prepare($query->Build(), $list); |
57 | 51 | return DB::Execute(); |
@@ -83,8 +77,8 @@ | ||
83 | 77 | } |
84 | 78 | |
85 | 79 | //時刻出力 (デバッグ用) |
86 | - private static function OutputTime($now, $expire, $name) { | |
87 | - JinrouCacheManager::OutputTime($now, $name); | |
88 | - JinrouCacheManager::OutputTime($expire); | |
80 | + private static function OutputTime(JinrouCache $filter, $name) { | |
81 | + JinrouCacheManager::OutputTime($filter->now, $name); | |
82 | + JinrouCacheManager::OutputTime($filter->next); | |
89 | 83 | } |
90 | 84 | } |