Skip to content

Commit d28010f

Browse files
committed
Cache bridge.
1 parent e9fd324 commit d28010f

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

src/CacheBridge.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat;
4+
5+
use Doctrine\Common\Cache\Cache as CacheInterface;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
/**
9+
* Cache bridge for laravel.
10+
*/
11+
class CacheBridge implements CacheInterface
12+
{
13+
/**
14+
* Fetches an entry from the cache.
15+
*
16+
* @param string $id The id of the cache entry to fetch.
17+
*
18+
* @return mixed The cached data or FALSE, if no cache entry exists for the given id.
19+
*/
20+
public function fetch($id)
21+
{
22+
return Cache::get($id);
23+
}
24+
25+
/**
26+
* Tests if an entry exists in the cache.
27+
*
28+
* @param string $id The cache id of the entry to check for.
29+
*
30+
* @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
31+
*/
32+
public function contains($id)
33+
{
34+
return Cache::has($id);
35+
}
36+
37+
/**
38+
* Puts data into the cache.
39+
*
40+
* If a cache entry with the given id already exists, its data will be replaced.
41+
*
42+
* @param string $id The cache id.
43+
* @param mixed $data The cache entry/data.
44+
* @param int $lifeTime The lifetime in number of seconds for this cache entry.
45+
* If zero (the default), the entry never expires (although it may be deleted from the cache
46+
* to make place for other entries).
47+
*
48+
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
49+
*/
50+
public function save($id, $data, $lifeTime = 0)
51+
{
52+
return Cache::put($id, $data, $lifeTime / 60);
53+
}
54+
55+
/**
56+
* Deletes a cache entry.
57+
*
58+
* @param string $id The cache id.
59+
*
60+
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
61+
* Deleting a non-existing entry is considered successful.
62+
*/
63+
public function delete($id)
64+
{
65+
return Cache::forget($id);
66+
}
67+
68+
/**
69+
* Retrieves cached information from the data store.
70+
*
71+
* @return array|null An associative array with server's statistics if available, NULL otherwise.
72+
*/
73+
public function getStats()
74+
{
75+
return null;
76+
}
77+
}

src/Facade.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ static public function getFacadeAccessor()
2525
*/
2626
static public function __callStatic($name, $args)
2727
{
28-
$app = static::getFacadeAccessor();
28+
$app = static::getFacadeRoot();
2929

30-
return call_user_func_array([$app, $name], $args);
30+
if (method_exists($app, $name)) {
31+
return call_user_func_array([$app, $name], $args);
32+
}
33+
34+
return $app->$name;
3135
}
3236
}

src/ServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public function register()
4040
);
4141

4242
$this->app->singleton(['EasyWeChat\\Foundation\\Application' => 'wechat'], function($app){
43-
return new Application(config('wechat'));
43+
$app = new Application(config('wechat'));
44+
45+
if (config('wechat.use_laravel_cache')) {
46+
$app->cache = new CacheBridge();
47+
}
48+
49+
return $app;
4450
});
4551
}
4652

src/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
*/
99
'debug' => true,
1010

11+
/**
12+
* 使用 Laravel 的缓存系统
13+
*/
14+
'use_laravel_cache' => true,
15+
1116
/**
1217
* 账号基本信息,请从微信公众平台/开放平台获取
1318
*/

0 commit comments

Comments
 (0)