Before I wrote an article using the Aliyun OCS cache in the Laravel 4 framework, I described how to support SASL OCS caching services that require Aliyun authentication by extending Laravel 4来. Some netizens asked me how the ACE cache was used in Laravel 4. I thought that I should be able to use the same method completely, and later when I tried it, I found that the cache of ACE was very different. So write another article about how to use the Aliyun ACE cache service in the Laravel framework.
How to extend the Laravel cache driver
In Laravel 4 using Cache::get ($key), Cache::p ut ($key, $value, $minutes) code like this is actually access to the instantiated illuminate\cache\repository, so we When you extend a custom cache driver through the Cache::extend method, you should also return a Illuminate\cache\repository object.
The Laravel 4 built-in Memcached cache driver, the implementation of the process is this:
1. Create a new object for a standard Memcached class
2. Create a Illuminate\cache\memecachedstore object that implements the Illuminate\cache\storeinterface interface with the Memcached object created in the previous step.
3. Create a Illuminate\cache\repository object with the Memcachedstore object you created in the previous step.
So when we extend our custom Cache driver, we choose one of the steps above to customize it to our own situation, and we end up returning the Illuminate\cache\repository object. For example, in the previous article, I was in the first step, after creating the standard Memcached object, the Setsaslauthdata () method was used to set the username password required by OCS. The 2nd and 3rd steps do not need to be customized after that.
The caching service for Aces
The caching service for the Aliyun ACE differs from the default OCS:
1. The Cache object is obtained by Alibaba::cache () method.
The Cache object of 2.ACE differs from the standard Memcached object, and the supported methods are limited.
So, this first step is not the standard Memcached object, so you can't create the Illuminate\cache\memcachedstore object. You need to implement the Illuminate\cache\storeinterface interface yourself.
After the console has created the cache space, there is a unique "cache space name" and then the cache object is obtained by Alibaba::cache (' Cache space name '). Here are the steps to implement the ACE caching service driver:
1. To facilitate modification, I added a key named ace to the config file app/config/cache.php to store the cache space name.
2. Then create a Acememcachedstore class, this class implements the Illuminate\cache\storeinterface interface.
3. Finally, use the Acememcachedstore object to create the Illuminate\cache\repository object.
Here's a concrete code implementation:
Encoding implements custom ACE cache drivers:
The first step is to modify the configuration file. Open app/config/cache.php and add a line at the end:
Copy Code code as follows:
Specify the cache space name
' Ace ' => ' Lblog-cache ',
The second step, for convenience, put your own class files in the Src/ace directory, using Aces as a namespace.
1. Create directory Src/ace in app's sibling directory.
2. Open the Composer.json file, modify the AutoLoad section, and use psr-0 or psr-4 to load the file under Classmap.
Copy Code code as follows:
"AutoLoad": {
"Classmap": [
AutoLoad class
],
"Psr-4": {
"ace\\": "Src/ace"
}
},
Create the src/ace/acememcachedstore.php file with the following code:
Copy Code code as follows:
<?php
namespace Ace;
Use Illuminate\cache\storeinterface;
Use Illuminate\cache\taggablestore;
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 a 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 a 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;
}
}
This code is relatively simple, but pay special attention to the implementation of the Get ($key) method. Both the standard memcached and the Get method of the ACE's cached object return the corresponding cached value when the key is valid, otherwise the return false, and in Laravel 4, it is judged by detecting whether the Get method returns null or not, so it needs to be processed here to return the cached value or n Ull.
The Acememcachedstore class has been created, and then the Cache is extended in the bootstrap/start.php file:
Open bootstrap/start.php and add the following code:
Copy Code code as follows:
Cache driver with an ace extension
Cache::extend (' Ace ', function ($app)
{
Read the value of "Ace" from the app/config/cache.php file
$space = $app [' config '] [' cache.ace '];
Read the value of "prefix" from the app/config/cache.php file
$prefix = $app [' config '] [' cache.prefix '];
Creating \ace\acememcachedstore Objects
$store = new \ace\acememcachedstore ($space, $prefix);
Create and return a \illuminate\cache\repository object
return new \illuminate\cache\repository ($store);
});
Specifies that the system uses ' Ace ' as the cache driver: Open app/config/cache.php, find ' driver ' => ' ... ' On the line, change to: ' Driver ' => ' Ace '.
Use and Restrictions
By doing so, you can call the ACE's caching service in Laravel 4, which is consistent with the usual usage, such as:
Copy Code code as follows:
Add cache, effective time 10 minutes
Cache::p ut (' my_key ', ' My value ', 10);
Read cache
Cache::get (' My_key ')
To determine if the cache exists
Cache::has (' My_key ')
Data query Caching
$users = db::table (' users ')->remember->get ();
However, because of the limitations of the ACE cache object itself, only the cached object of the specified key can be deleted, cannot traverse, and the full operation, so the Cache::flush () method cannot be used. In the above Acememcachedstore object, the Flush method does nothing, but returns false.