Implementation using Aliyun ACE Caching service in Laravel framework

Source: Internet
Author: User
Tags autoload config create directory extend flush php file php and aliyun

This article introduces the implementation of the Laravel framework using the Aliyun ACE Caching service, this article expands an ACE cache driver to use the Aliyun Ace Caching service, the need for friends can refer to the

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 accessing the instantiated illuminatecacherepository, so we pass Cache::extend method extends a custom cache driver, it should also return a Illuminatecacherepository 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 Illuminatecachememecachedstore object that implements the Illuminatecachestoreinterface interface with the Memcached object created in the previous step.

3. Create a Illuminatecacherepository 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 Illuminatecacherepository 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 Illuminatecachememcachedstore object. You need to implement the Illuminatecachestoreinterface 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 Illuminatecachestoreinterface interface.

3. Finally, use the Acememcachedstore object to create the Illuminatecacherepository 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:

The code is 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.

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

The code is 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 Aceacememcachedstore Objects

$store = new Aceacememcachedstore ($space, $prefix);

Create and return a Illuminatecacherepository object

return new Illuminatecacherepository ($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:

The code is 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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.