Home  >  Article  >  Backend Development  >  Extending the Memcached cache driver in Laravel to implement using Alibaba Cloud OCS cache, laravelmemcached_PHP tutorial

Extending the Memcached cache driver in Laravel to implement using Alibaba Cloud OCS cache, laravelmemcached_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:07729browse

The extended Memcached cache driver in Laravel implements the use of Alibaba Cloud OCS cache, laravelmemcached

Laravel is a PHP framework that I have used a lot recently and the more I use it, the more I like it. Since there is no historical baggage of backward compatibility, it is completely object-oriented and uses Facades’ elegant IoC Container implementation and Composer for packaging. Management, you can easily introduce and use excellent components in the open source community... In short, this is a "master-level PHP development framework" that truly allows you to "code happy".

When I tried to deploy my Laravel App to Alibaba Cloud, I encountered a problem: Laravel supports Memcached cache, and Alibaba Cloud's OCS is also based on Memcached cache, but Alibaba Cloud OCS uses SASL authentication, and Laravel's Memcached driver There is no relevant method implemented. Even if the SASL authentication option is enabled when compiling PHP Memcached on the server, there is no way to set the user name and password.

In Laravel, there are many ways to solve this problem. I chose the fastest and most trouble-free method: extend Laravel's own Memcached driver and specify the username and password through the setSaslAuthData method of the Memcached object itself.

Background knowledge

This is using the extend method of IlluminateCacheCacheManager (inherited from IlluminateSupportManager). Let’s take a look at the definition of this method first:

Copy code 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. This function is the method to be executed when your custom driver is called. By reading the source code of IlluminateCacheCacheManager, we can find that the function that creates the driver returns an instance of IlluminateCacheRepository. The constructor of IlluminateCacheRepository is as follows:
Copy code The code is 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 methods that the Cache object can execute. Since my plan is to extend the original Memcached cache driver, in the source code of IlluminateCacheCacheManager, you can see that Laravel creates the Memcached driver like this:

Copy code 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 the Memcached server you defined from the configuration file, and then creates a Memcached object (implemented through IlluminateCacheMemcachedConnector, which actually creates a standard Memcached object, and then calls the addServer method of Memcached to specify the server to connect to. Then returns the instantiated Memcached object )

.

Expand your own cache driver

After understanding the above background knowledge, you can expand your own cache driver. The idea is as follows:

1. In the app/config/cache.php file, add three configuration items to set "whether to use sasl authentication", "sasl authentication account", and "sasl authentication password".
2. In the bootstrap/start.php file, call the Cache::extend method to extend the driver.
3. In the app/config/cache.php file, modify the driver configuration item and specify that the system uses its own extended driver.

Add configuration items

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

Copy code The code is as follows:

'memcached' => array(
array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 100),
),

Modified to:
Copy code 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 username
'memcached_pass' => 'your ocs password', // Your OCS password

Extended driver

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

Copy code 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 Memcached server configuration from 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 sasl authentication user name 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 (This is done in Alibaba Cloud’s documentation...)
            $memcached->setOption(Memcached::OPT_COMPRESSION, false);

                    // Specify Memcached to use binary protocol (sasl authentication requirement)
           $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

                   // Specify the account password used for sasl authentication
          $memcached->setSaslAuthData($user, $pass);
}

// Read the cache prefix from the configuration file
$prefix = $app['config']['cache.prefix'];

//Create MemcachedStore object
$store = new IlluminateCacheMemcachedStore($memcached, $prefix);
     
//Create a Repository object and return
Return new IlluminateCacheRepository($store);
});

Modify the configuration and use your own extended cache driver

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

Copy code The code is as follows:

"driver" => "file", // The default is to use file caching

Modified to:
Copy code The code is as follows:

"driver" => "saslMemcached", // The name of the driver just expanded and implemented

Now, you can let Laravel use the Alibaba Cloud OCS cache service on your Alibaba Cloud ECS server. (The premise is that your PHP supports the Memcached extension and SASL authentication is enabled, refer to: http://help.aliyun.com/doc/view/13553932.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955978.htmlTechArticleExtended Memcached cache driver implementation in Laravel uses Alibaba Cloud OCS cache, laravelmemcached Laravel is something I have used a lot recently and it is becoming more and more popular. The more I use it, the more I like the PHP framework. Since I don’t have any suggestions for...
Statement:
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