Skip to content

自定义缓存

Stoneworld edited this page Mar 7, 2016 · 2 revisions

微信的 access_token 是不能太频繁的调用的,所以需要缓存,本 SDK 默认使用文件缓存,文件会创建在代码运行环境的临时目录里,使用sys_get_temp_dir() 函数获取的临时目录下。

如果你需要自定义缓存方式,那么 Wechat 提供了以下两个方法:

<?php

use Stoneworld\Wechat\Cache;

// 写入
Cache::setter(function($key, $value, $lifetime){
    return your_custom_set_cache($key, $value, $lifetime);
});

// 读取
Cache::getter(function($key){
    return your_custom_get_cache($key);
});

your_custom_set_cache 为你自己的方法,这里是举例。

当你的 getter 没有返回缓存的值时,会重新请求 access_token。