1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- namespace App\Common;
- use App\Servers\ClientServer;
- trait CacheLogTrait
- {
- public function setCache($msg_id, $data)
- {
- $value = '';
- if (is_array($data)) {
- $value = "array||" . json_encode($data);
- } else {
- $value = "string||" . $data;
- }
- self::$redis->setex(ClientServer::CACHE_KEY . $msg_id, 5 * 60, $value);
- }
- public function getCache($msg_id)
- {
- $val = self::$redis->get(ClientServer::CACHE_KEY . $msg_id);
- $array = explode('||', $val);
- if (count($array) !== 2) return false;
- list($type, $data) = $array;
- switch ($type) {
- case 'string':
- return $data;
- break;
- case 'array':
- return json_decode($data);
- }
- return false;
- }
- }
|