Cache.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | wechat-php-sdk
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方文档: https://www.kancloud.cn/zoujingli/wechat-php-sdk
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/wechat-php-sdk
  12. // +----------------------------------------------------------------------
  13. namespace Wechat\Lib;
  14. use Wechat\Loader;
  15. /**
  16. * 微信SDK基础缓存类
  17. *
  18. * @author Anyon <zoujingli@qq.com>
  19. * @date 2016-08-20 17:50
  20. */
  21. class Cache
  22. {
  23. /**
  24. * 缓存位置
  25. * @var string
  26. */
  27. static public $cachepath;
  28. /**
  29. * 设置缓存
  30. * @param string $name
  31. * @param string $value
  32. * @param int $expired
  33. * @return mixed
  34. */
  35. static public function set($name, $value, $expired = 0)
  36. {
  37. if (isset(Loader::$callback['CacheSet'])) {
  38. return call_user_func_array(Loader::$callback['CacheSet'], func_get_args());
  39. }
  40. $data = serialize(array('value' => $value, 'expired' => $expired > 0 ? time() + $expired : 0));
  41. return self::check() && file_put_contents(self::$cachepath . $name, $data);
  42. }
  43. /**
  44. * 读取缓存
  45. * @param string $name
  46. * @return mixed
  47. */
  48. static public function get($name)
  49. {
  50. if (isset(Loader::$callback['CacheGet'])) {
  51. return call_user_func_array(Loader::$callback['CacheGet'], func_get_args());
  52. }
  53. if (self::check() && ($file = self::$cachepath . $name) && file_exists($file) && ($data = file_get_contents($file)) && !empty($data)) {
  54. $data = unserialize($data);
  55. if (isset($data['expired']) && ($data['expired'] > time() || $data['expired'] === 0)) {
  56. return isset($data['value']) ? $data['value'] : null;
  57. }
  58. }
  59. return null;
  60. }
  61. /**
  62. * 删除缓存
  63. * @param string $name
  64. * @return mixed
  65. */
  66. static public function del($name)
  67. {
  68. if (isset(Loader::$callback['CacheDel'])) {
  69. return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
  70. }
  71. return self::check() && @unlink(self::$cachepath . $name);
  72. }
  73. /**
  74. * 输出内容到日志
  75. * @param string $line
  76. * @param string $filename
  77. * @return mixed
  78. */
  79. static public function put($line, $filename = '')
  80. {
  81. if (isset(Loader::$callback['CachePut'])) {
  82. return call_user_func_array(Loader::$callback['CachePut'], func_get_args());
  83. }
  84. empty($filename) && $filename = date('Ymd') . '.log';
  85. return self::check() && file_put_contents(self::$cachepath . $filename, '[' . date('Y/m/d H:i:s') . "] {$line}\n", FILE_APPEND);
  86. }
  87. /**
  88. * 检查缓存目录
  89. * @return bool
  90. */
  91. static protected function check()
  92. {
  93. empty(self::$cachepath) && self::$cachepath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
  94. self::$cachepath = rtrim(self::$cachepath, '/\\') . DIRECTORY_SEPARATOR;
  95. if (!is_dir(self::$cachepath) && !mkdir(self::$cachepath, 0755, true)) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * 文件缓存,成功返回文件路径
  102. * @param string $content 文件内容
  103. * @param string $filename 文件名称
  104. * @return bool|string
  105. */
  106. static public function file($content, $filename = '')
  107. {
  108. if (isset(Loader::$callback['CacheFile'])) {
  109. return call_user_func_array(Loader::$callback['CacheFile'], func_get_args());
  110. }
  111. empty($filename) && $filename = md5($content) . '.' . self::getFileExt($content);
  112. if (self::check() && file_put_contents(self::$cachepath . $filename, $content)) {
  113. return self::$cachepath . $filename;
  114. }
  115. return false;
  116. }
  117. /**
  118. * 根据文件流读取文件后缀
  119. * @param string $content
  120. * @return string
  121. */
  122. static public function getFileExt($content)
  123. {
  124. $types = array(
  125. 255216 => 'jpg', 7173 => 'gif', 6677 => 'bmp', 13780 => 'png',
  126. 7368 => 'mp3', 4838 => 'wma', 7784 => 'mid', 6063 => 'xml',
  127. );
  128. $typeInfo = @unpack("C2chars", substr($content, 0, 2));
  129. $typeCode = intval($typeInfo['chars1'] . $typeInfo['chars2']);
  130. return isset($types[$typeCode]) ? $types[$typeCode] : 'mp4';
  131. }
  132. }