阿里雲 ACE (App Cloud Engine) 快取服務介面與標準 Memcached 不同,需透過 Alibaba::Cache() 取得專用物件。本文示範實作自訂 StoreInterface 並註冊為 Laravel 快取驅動。
Alibaba::Cache('空間名稱') 取得快取物件get、set、delete、increment、decrement、flush (可能不支援)MemcachedStore<?php
// src/Ace/AceStore.php
namespace App\Cache;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Cache\TaggableStore;
class AceStore implements TaggableStore
{
protected $cache;
protected $prefix;
public function __construct(string $space, string $prefix = '')
{
// ACE 環境下自動載入 Alibaba 類別
if (!class_exists('Alibaba')) {
throw new \RuntimeException('Alibaba SDK 未載入,請確認運行於 ACE 環境');
}
$this->cache = \Alibaba::Cache($space);
$this->prefix = $prefix ? $prefix . ':' : '';
}
protected function getKey($key)
{
return $this->prefix . $key;
}
public function get($key)
{
$value = $this->cache->get($this->getKey($key));
// ACE 回傳 false 表示不存在,Laravel 期望 null
return $value === false ? null : $value;
}
public function many(array $keys)
{
$result = [];
foreach ($keys as $key) {
$value = $this->get($key);
if ($value !== null) {
$result[$key] = $value;
}
}
return $result;
}
public function put($key, $value, $minutes)
{
$seconds = $minutes * 60;
return $this->cache->set($this->getKey($key), $value, $seconds);
}
public function putMany(array $values, $minutes)
{
foreach ($values as $key => $value) {
$this->put($key, $value, $minutes);
}
return true;
}
public function increment($key, $value = 1)
{
return $this->cache->increment($this->getKey($key), $value);
}
public function decrement($key, $value = 1)
{
return $this->cache->decrement($this->getKey($key), $value);
}
public function forever($key, $value)
{
// ACE 可能不支援永久,用極大值近似
return $this->cache->set($this->getKey($key), $value, 31536000); // 1 年
}
public function forget($key)
{
return $this->cache->delete($this->getKey($key));
}
public function flush()
{
// ACE 可能不支援 flush,回傳 false 表示不支援
return false;
}
public function getPrefix()
{
return $this->prefix;
}
// TaggableStore 介面 (若不需標籤功能可拋出例外或空實作)
public function tags(array $names) { throw new \BadMethodCallException('ACE 快取不支援標籤'); }
public function reconnect() {}
}
?>
<?php
// app/Providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Cache\Repository;
use App\Cache\AceStore;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$this->app->booted(function () {
$this->app['cache']->extend('ace', function ($app) {
$config = $app['config']['cache.stores.ace'];
$store = new AceStore($config['space'], $config['prefix'] ?? '');
return new Repository($store);
});
});
}
}
?>
'stores' => [
// ...
'ace' => [
'driver' => 'ace',
'space' => env('ACE_CACHE_SPACE', 'default_space'),
'prefix' => env('CACHE_PREFIX', 'laravel_'),
],
],
CACHE_DRIVER=ace
ACE_CACHE_SPACE=my_app_cache
<?php
// 完全相容 Laravel Cache 介面
Cache::put('key', 'value', 10); // 10 分鐘
$value = Cache::get('key');
Cache::has('key');
Cache::forget('key');
Cache::increment('counter');
Cache::decrement('counter');
Cache::tags(['users', 'posts'])->put('key', 'value', 10); // 會拋出例外,ACE 不支援標籤
Cache::tags() 會拋出例外<?php
// config/cache.php
'default' => env('CACHE_DRIVER', app()->environment('production') ? 'ace' : 'file'),
// 或
'default' => env('CACHE_DRIVER', 'file'), // 本地預設 file,部署時用環境變數覆蓋
?>
實作 StoreInterface 並透過 Cache::extend() 註冊,即可將任何快取服務整合進 Laravel 統一介面。ACE 驅動展示了如何包裝非標準快取客戶端。