Laravel Extended memcached Cache driver implementation using Aliyun OCS cache

Source: Internet
Author: User
Tags closure extend memcached php file php framework php memcached aliyun

This article mainly introduces the Laravel extended memcached cache driver implementation using the Aliyun OCS cache, this article extends a support SASL authentication mode memcached cache driver, need friends can refer to the following

Laravel is I recently used a lot and the more I like a PHP framework, because there is no backward-compatible historical baggage, fully object-oriented style, with the help of facades elegant IOC Container implementation, the use of Composer package management, can easily introduce and use the best components in the open source community ... In short, this is a real let you "code Happy" "Master-level PHP development framework."

When trying to deploy their laravel App to Aliyun, a problem has been encountered: Laravel supports Memcached caching, Aliyun OCS is also Memcached based caching, but Aliyun OCS uses SASL authentication and Laravel Memcached driver does not implement the relevant methods, even if the PHP Memcached on the server enabled the SASL authentication option, there is no way to set user name and password.

In Laravel, there are many ways to solve such problems. I chose the quickest and easiest way: to extend Laravel own Memcached drive, and to specify the username and password by Memcached the Setsaslauthdata method of the object itself.

Background knowledge

This is the use of the Illuminatecachecachemanager extend method (inherited from Illuminatesupportmanager). Let's take a look at the definition of this method:

The code is as follows:

/**

* Register a custom driver creator Closure.

*

* @param string $driver

* @param Closure $callback

* @return Illuminatesupportmanager|static

*/

Public function Extend ($driver, Closure $callback) {}

This method receives two parameters, the first is a string representing your custom driver name, and the second is a closure callback function, which is the method to execute when calling your custom driver. By reading the Illuminatecachecachemanager source code, we can see that creating a driven function returns an Illuminatecacherepository instance, illuminatecacherepository The constructor functions are as follows:

Copy code code as follows:

/**

* Create a new cache repository instance.

*

* @param illuminatecachestoreinterface $store

*/

Public function __construct (Storeinterface $store)

{

$this->store = $store;

}

It requires an object that implements the Illuminatecachestoreinterface interface, which defines the method that the Cache object can execute. Since my plan is to extend the original Memcached cache driver, in Illuminatecachecachemanager source code, you can see that laravel is creating Memcached-driven:

The code is as follows:

/**

* Create An instance of the Memcached cache driver.

*

* @return Illuminatecachememcachedstore

*/

protected function Creatememcacheddriver ()

{

$servers = $this->app[' config ' [' cache.memcached '];

$memcached = $this->app[' memcached.connector ']->connect ($servers);

Return $this->repository (new Memcachedstore ($memcached, $this->getprefix ()));

}

It first reads your defined Memcached server from the configuration file, and then creates a Memcached object (implemented by Illuminatecachememcachedconnector, which is actually creating a standard Memcached object. Then call the Memcached Addserver method to specify the server to connect to, and then return the instantiated Memcached object. )

Extend your own cache driver

Once you know the background, you can expand your cache driver. Ideas are as follows:

1. In the app/config/cache.php file, add three configuration items to set up "whether to use SASL authentication", "SASL Authentication Account", "SASL Authentication password".

2. In the bootstrap/start.php file, call the Cache::extend method extension driver.

3. In the app/config/cache.php file, modify the driver configuration entry to specify that the system uses its own extended drive.

Adding Configuration Items

First, open the app/config/cache.php file and find:

The code is as follows:

' memcached ' => Array (

Array (' Host ' => ' 127.0.0.1 ', ' Port ' => 11211, ' weight ' => 100),

),

Modified to:

The code is as follows:

' memcached ' => Array (

Array (' Host ' => ' 127.0.0.1 ', ' Port ' => 11211, ' weight ' => 100),

),

' Memcached_sasl ' => ' true ',//enable SASL authentication

' Memcached_user ' => ' your OCS username ',//your OCS user name

' Memcached_pass ' => ' Your OCS password ',//Your OCS password

Extended Drive

Then, open the bootstrap/start.php file and return $app on the last line; Insert code before:

The code is as follows:

Based on the system's own Memcached cache driver, extend a cache driver named saslmemcached

Cache::extend (' saslmemcached ', function ($app) {

Read the Memcached server configuration from the configuration file

$servers = $app [' config '] [' cache.memcached '];

Use the Illuminatecachememcachedconnector class to create a new Memcached object

$memcached = $app [' Memcached.connector ']->connect ($servers);

If the PHP Memcached extension on the server supports SASL authentication

if (Ini_get (' MEMCACHED.USE_SASL ')) {

Read the SASL authenticated username from the configuration file

$user = $app [' config '] [' cache.memcached_user '];

Read the SASL authentication password from the configuration file

$pass = $app [' config '] [' cache.memcached_pass '];

Disable Memcached compression (in Aliyun's documentation ...)

$memcached->setoption (Memcached::opt_compression, false);

Specify Memcached to use binary protocol (SASL authentication requirements)

$memcached->setoption (Memcached::opt_binary_protocol, true);

Specify the account password for SASL authentication

$memcached->setsaslauthdata ($user, $pass);

}

Read the cache prefix from the configuration file

$prefix = $app [' config '] [' cache.prefix '];

Creating Memcachedstore Objects

$store = new Illuminatecachememcachedstore ($memcached, $prefix);

Creates a Repository object and returns

return new Illuminatecacherepository ($store);

});

Modify the configuration to use your own extended cache driver

Open the app/config/cache.php file and find:

The code is as follows:

"Driver" => "file",//default is to use the files cache

Modified to:

The code is as follows:

"Driver" => "saslmemcached",//The driver name of the extension implementation just now

Now you can get laravel to use the Aliyun OCS caching service on your Aliyun ECS server. (

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.