From 29b1d1f3731368d5fa9aafb8f93de996686d45ee Mon Sep 17 00:00:00 2001 From: Anyon Date: Tue, 23 Apr 2019 15:52:21 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=A2=9E=E5=8A=A0=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=96=87=E4=BB=B6=E5=86=99=E5=85=A5=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WeChat/Contracts/Tools.php | 26 +++++++++++++++++++++++++- _test/config.php | 16 ++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/WeChat/Contracts/Tools.php b/WeChat/Contracts/Tools.php index d0de000..60a435d 100644 --- a/WeChat/Contracts/Tools.php +++ b/WeChat/Contracts/Tools.php @@ -31,6 +31,17 @@ class Tools */ public static $cache_path = null; + /** + * 缓存写入操作 + * @var array + */ + public static $cache_callable = [ + 'set' => null, // 写入缓存 + 'get' => null, // 获取缓存 + 'del' => null, // 删除缓存 + 'put' => null, // 写入文件 + ]; + /** * 网络缓存 * @var array @@ -356,6 +367,9 @@ class Tools */ public static function pushFile($name, $content) { + if (is_callable(self::$cache_callable['put'])) { + return call_user_func_array(self::$cache_callable['put'], func_get_args()); + } $file = self::_getCacheName($name); if (!file_put_contents($file, $content)) { throw new LocalCacheException('local file write error.', '0'); @@ -373,8 +387,12 @@ class Tools */ public static function setCache($name, $value = '', $expired = 3600) { + if (is_callable(self::$cache_callable['set'])) { + return call_user_func_array(self::$cache_callable['set'], func_get_args()); + } $file = self::_getCacheName($name); - if (!file_put_contents($file, serialize(['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]))) { + $data = ['name' => $name, 'value' => $value, 'expired' => time() + intval($expired)]; + if (!file_put_contents($file, serialize($data))) { throw new LocalCacheException('local cache error.', '0'); } return $file; @@ -387,6 +405,9 @@ class Tools */ public static function getCache($name) { + if (is_callable(self::$cache_callable['get'])) { + return call_user_func_array(self::$cache_callable['get'], func_get_args()); + } $file = self::_getCacheName($name); if (file_exists($file) && ($content = file_get_contents($file))) { $data = unserialize($content); @@ -405,6 +426,9 @@ class Tools */ public static function delCache($name) { + if (is_callable(self::$cache_callable['del'])) { + return call_user_func_array(self::$cache_callable['del'], func_get_args()); + } $file = self::_getCacheName($name); return file_exists($file) ? unlink($file) : true; } diff --git a/_test/config.php b/_test/config.php index 010618c..e6f8075 100644 --- a/_test/config.php +++ b/_test/config.php @@ -12,6 +12,22 @@ // | github开源项目:https://github.com/zoujingli/WeChatDeveloper // +---------------------------------------------------------------------- +// 配置缓存处理函数 +//\WeChat\Contracts\Tools::$cache_callable = [ +// 'set' => function ($name, $value, $expired = 360) { +// var_dump(func_get_args()); +// }, +// 'get' => function ($name) { +// var_dump(func_get_args()); +// }, +// 'del' => function ($name) { +// var_dump(func_get_args()); +// }, +// 'put' => function ($name) { +// var_dump(func_get_args()); +// }, +//]; + return [ 'token' => 'test', 'appid' => 'wx60a43dd8161666d4',