-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
自定义缓存怎么使用? #16
Comments
我按照文档写了两个函数 if (! function_exists('database_set_cache'))
{
function database_set_cache($key, $value, $lifetime)
{
$cache = \App\Models\Admin\WxWorkCache::where('key',$key)->first();
if($cache === null)
{
$newCache = new \App\Models\Admin\WxWorkCache;
$newCache->key = $key;
$newCache->body = $value;
$newCache->lifetime = $lifetime;
$newCache->save();
return $newCache;
}
else
{
$cache->key = $key;
$cache->body = $value;
$cache->lifetime = $lifetime;
$cache->save();
return $cache;
}
}
}
if(! function_exists('database_get_cache'))
{
function database_get_cache($key)
{
$cache = \App\Models\Admin\WxWorkCache::where('key',$key)->first();
return $cache->body;
}
} 请问一下,$lifetime得到的是6400,那么cache类是怎么判断token过期的呢? |
cache类和token没有关系,cache类在这里只是负责缓存写入和读取,在这里只是把token存到了缓存中。自定义缓存的setter和getter函数写成匿名函数就可以了,这样的话缓存的读取和写入就会使用你自定义的形式。 |
我现在是写了一个middleware 把缓存方法写在了里头 <?php
namespace App\Http\Middleware;
use Closure;
class WxWorkCacheMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
\Stoneworld\Wechat\Cache::setter(function($key,$value,$lifetime){
return $this->database_set_cache($key,$value,$lifetime);
});
\Stoneworld\Wechat\Cache::getter(function($key){
return $this->database_get_cache($key);
});
return $next($request);
}
private function database_set_cache($key, $value, $lifetime)
{
$cache = \App\Models\Admin\WxWorkCache::where('key',$key)->first();
if($cache === null)
{
$newCache = new \App\Models\Admin\WxWorkCache;
$newCache->key = $key;
$newCache->body = $value;
$newCache->lifetime = $lifetime;
$newCache->save();
return $newCache;
}
else
{
$cache->key = $key;
$cache->body = $value;
$cache->lifetime = $lifetime;
$cache->save();
return $cache;
}
}
private function database_get_cache($key)
{
$cache = \App\Models\Admin\WxWorkCache::where('key',$key)->first();
if($cache === null)
{
return null;
}
elseif($cache->updated_at->getTimestamp() + $cache->lifetime > time())
{
return $cache->body;
}
else
{
return null;
}
}
} 但是我觉得还是没有安正超的公众号sdk的自定义缓存用起来方便,安正超的是直接在options里定义的cache |
这个包是基于超哥的sdk2.0改写的,所以和现在超哥的3.0之后的肯定都不一样的。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wiki的介绍看不懂啊
自定义缓存的setter和getter函数怎么写呢?
比如我使用User类管理成员时,怎么确定我使用的是自定义缓存,而非系统的文件缓存呢?
The text was updated successfully, but these errors were encountered: