Home > Backend Development > PHP Tutorial > Implementing the use of Alibaba Cloud ACE cache service in the Laravel framework, laravel Alibaba_PHP tutorial

Implementing the use of Alibaba Cloud ACE cache service in the Laravel framework, laravel Alibaba_PHP tutorial

WBOY
Release: 2016-07-13 10:07:08
Original
957 people have browsed it

The Laravel framework implements the use of Alibaba Cloud ACE cache service, laravel Ali

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:

Copy code The code is as follows:

//Specify cache space name
'ace' => 'lblog-cache',

The second step is to put your own class files in the src/Ace directory for convenience 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.

Copy code 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:

Copy 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;
}
}

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:

Copy code The code is as follows:

// Cache driver with extension ace
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'];
     
//Create AceAceMemcachedStore object
$store = new AceAceMemcachedStore($space, $prefix);
     
//Create and return the IlluminateCacheRepository object
Return new IlluminateCacheRepository($store);

});

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:

Copy code The code is as follows:

// Add cache, valid for 10 minutes
Cache::put('my_key', 'my value', 10);

// Read cache
Cache::get('my_key')

// Determine whether the cache exists
Cache::has('my_key')

//Data query cache
$users = DB::table('users')->remember(10)->get();

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955977.htmlTechArticleThe Laravel framework implements the use of Alibaba Cloud ACE cache service. Before laravel Ali, I wrote an article in the Laravel 4 framework The article using Alibaba Cloud OCS cache introduces how to extend L...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template