Home > Article > Backend Development > Implementing the use of Alibaba Cloud ACE cache service in the Laravel framework, laravel Alibaba_PHP tutorial
Previously I 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 actually access 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 the 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 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:
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.
Create the src/Ace/AceMemcachedStore.php file with the following code:
This code is relatively simple, but special attention should be paid to the implementation of the get($key) method. The get method of the cache object of standard memcached and ACE returns the corresponding cache value when the key is valid, otherwise it returns false. In Laravel 4, the judgment is made by detecting whether the get method returns null, so it needs to be processed here. Return cached value or null.
The AceMemcachedStore class has been created. Next, extend Cache in the bootstrap/start.php file:
Open bootstrap/start.php and add the following code:
Specify that the system uses 'ace' as the cache driver: open app/config/cache.php, find the line where 'driver' => '...' and modify it to: 'driver' => 'ace'.
Uses and Restrictions
Through the above operations, you can call ACE's cache service in Laravel 4, and the usage is exactly the same as usual usage, such as:
However, due to the limitations of the ACE cache object itself, the cache object with the specified key can only be deleted, and cannot be traversed or fully operated, so the Cache::flush() method cannot be used. In the AceMemcachedStore object above, the flush method does nothing but returns false.