CacheLogTrait.php 842 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\Common;
  3. use App\Servers\ClientServer;
  4. trait CacheLogTrait
  5. {
  6. public function setCache($msg_id, $data)
  7. {
  8. $value = '';
  9. if (is_array($data)) {
  10. $value = "array||" . json_encode($data);
  11. } else {
  12. $value = "string||" . $data;
  13. }
  14. self::$redis->setex(ClientServer::CACHE_KEY . $msg_id, 5 * 60, $value);
  15. }
  16. public function getCache($msg_id)
  17. {
  18. $val = self::$redis->get(ClientServer::CACHE_KEY . $msg_id);
  19. $array = explode('||', $val);
  20. if (count($array) !== 2) return false;
  21. list($type, $data) = $array;
  22. switch ($type) {
  23. case 'string':
  24. return $data;
  25. break;
  26. case 'array':
  27. return json_decode($data);
  28. }
  29. return false;
  30. }
  31. }