This article mainly introduces the implementation of using Alibaba Cloud ACE cache service in the Laravel framework. This article extends an ACE cache driver. In order to use Alibaba Cloud ACE cache service, friends who need it can refer to it
I previously wrote an article about using Alibaba Cloud OCS cache in the Laravel 4 framework, which introduced how to extend Laravel 4 to support the Alibaba Cloud OCS cache service that requires SASL authentication. Some netizens asked me how to use ACE's cache in Laravel 4. I originally thought that the same method should be used, but when I tried it myself, I discovered that ACE's cache is very different. So I’ll write another article to introduce how to use Alibaba Cloud ACE’s caching service in the Laravel framework.
How to extend Laravel’s cache driver
When using code like Cache::get($key), Cache::put($key, $value, $minutes) in Laravel 4, you are actually accessing the instantiated IlluminateCacheRepository, so we pass Cache: When the :extend method extends a custom cache driver, it should also return an IlluminateCacheRepository object.
Laravel 4’s built-in Memcached cache driver, the implementation process is as follows:
1. Create a new object of the standard Memcached class
2. Use the Memcached object created in the previous step to create an IlluminateCacheMemecachedStore object that implements the IlluminateCacheStoreInterface interface.
3. Create an IlluminateCacheRepository object using the MemcachedStore object created in the previous step.
So when we extend the custom Cache driver, we choose one of the above steps to customize according to our own situation, and ultimately return the IlluminateCacheRepository object. For example, in the previous article, in the first step, after creating the standard Memcached object, I set the username and password required by OCS through the setSaslAuthData() method. There is no need to customize subsequent steps 2 and 3.
ACE’s caching service
Alibaba Cloud ACE’s caching service is different from the default OCS:
1. Obtain the Cache object through the Alibaba::Cache() method.
2.ACE’s Cache object is different from the standard Memcached object and supports limited methods.
So, what you get in the first step this time is not a standard Memcached object, so you cannot create an IlluminateCacheMemcachedStore object. You need to implement the IlluminateCacheStoreInterface interface yourself.
After the cache space is created in the console, there will be a unique "cache space name", and then the Cache object is obtained through Alibaba::Cache('cache space name'). The following are the steps to implement the ACE cache service driver:
1. In order to facilitate modification, I added a key named ace in the configuration file app/config/cache.php to store the cache space name.
2. Then create an AceMemcachedStore class, which implements the IlluminateCacheStoreInterface interface.
3. Finally, use the AceMemcachedStore object to create the IlluminateCacheRepository object.
Let’s look at the specific code implementation:
Coding to implement a custom ACE cache driver:
The first step is to modify the configuration file. Open app/config/cache.php and add a line at the end:
The code is as follows:
// Specify cache space name
'ace' => 'lblog-cache',
Second step, for convenience, put your own class files in the src/Ace directory and use Ace as the namespace.
1. Create the directory src/Ace in the same level directory of app.
2. Open the composer.json file, modify the autoload section, and use psr-0 or psr-4 under the classmap to automatically load the file.
The code is as follows:
"autoload": {
"classmap": [
// autoload class
],
"psr-4": {
"Ace\": "src/Ace"
}
},
Create the src/Ace/AceMemcachedStore.php file with the following code:
The code is as follows:
namespace Ace;
Use IlluminateCacheStoreInterface;
Use IlluminateCacheTaggableStore;
class AceMemcachedStore extends TaggableStore implements StoreInterface {
protected $memcached;
protected $prefix;
public function __construct($space, $prefix = '') {
$this->memcached = Alibaba::Cache($space);
$this->prefix = strlen($prefix) > 0 ? $prefix.':' : '';
}
/**
* Retrieve an item from the cache by key.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
$value = $this->memcached->get($this->prefix.$key);
if(is_bool($value) && $value === false) {
return null;
}
return $value;
}
/**
* Store an item in the cache for a given number of minutes.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return boolean
*/
public function put($key, $value, $minutes)
{
return $this->memcached->set($this->prefix.$key, $value, $minutes);
}
/**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function increment($key, $value = 1)
{
return $this->memcached->increment($this->prefix.$key, $value);
}
/**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function decrement($key, $value = 1)
{
return $this->memcached->decrement($this->prefix.$key, $value);
}
/**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return boolean
*/
public function forever($key, $value)
{
return $this->memcached->set($key, $value, 0);
}
/**
* Remove an item from the cache.
*
* @param string $key
* @return boolean
*/
public function forget($key)
{
return $this->memcached->delete($this->prefix.$key);
}
/**
* Remove all items from the cache.
*
* @return void
*/
public function flush()
{
//$this->memcached->flush();
return false;
}
public function getMemcached()
{
return $this->memcached;
}
/**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
}
这段代码比较简单,不过要特别注意一下 get($key) 方法的实现。标准 memcached 以及 ACE 的缓存对象的 get 方法都是key有效时返回对应的缓存值,否则返回false,而在 Laravel 4 中,是通过检测 get 方法返回的是否 null 来做判断,所以这里需要处理一下,返回缓存值或者null。
AceMemcachedStore类已经创建好了,接下来在 bootstrap/start.php 文件中扩展 Cache:
打开 bootstrap/start.php, 添加以下代码:
代码如下:
// 扩展名为 ace 的缓存驱动
Cache::extend('ace', function($app)
{
// 从 app/config/cache.php 文件中读取 "ace" 的值
$space = $app['config']['cache.ace'];
// 从 app/config/cache.php 文件中读取 "prefix" 的值
$prefix = $app['config']['cache.prefix'];
// 创建 AceAceMemcachedStore 对象
$store = new AceAceMemcachedStore($space, $prefix);
// 创建并返回 IlluminateCacheRepository 对象
return new IlluminateCacheRepository($store);
});
指定系统使用 'ace' 作为缓存驱动:打开 app/config/cache.php,找到 'driver' => '...' 所在行,修改为:'driver' => 'ace'.
使用和限制
通过以上操作,就可以在 Laravel 4 中调用 ACE 的缓存服务,使用上与平常的用法完全一致,比如:
代码如下:
// 添加缓存,有效时间10分钟
Cache::put('my_key', 'my value', 10);
// 读取缓存
Cache::get('my_key')
// 判断缓存是否存在
Cache::has('my_key')
// 数据查询缓存
$users = DB::table('users')->remember(10)->get();
但是由于 ACE 缓存对象本身的限制,只能删除指定 key 的缓存对象,不能遍历、全量操作,因此 Cache::flush() 方法就不能使用。在上面的 AceMemcachedStore 对象中,flush 方法没有做任何操作,只是返回 false.