Comments (4)
我按照文档写了两个函数
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过期的呢?
from wechat.
cache类和token没有关系,cache类在这里只是负责缓存写入和读取,在这里只是把token存到了缓存中。自定义缓存的setter和getter函数写成匿名函数就可以了,这样的话缓存的读取和写入就会使用你自定义的形式。
from wechat.
我现在是写了一个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
from wechat.
这个包是基于超哥的sdk2.0改写的,所以和现在超哥的3.0之后的肯定都不一样的。
from wechat.
Related Issues (9)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wechat.