This article mainly introduces the extended Memcached cache driver in Laravel to be implemented using Alibaba Cloud OCS cache. This article extends a Memcached cache driver that supports SASL authentication mode. Friends who need it can refer to it
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 trying 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:
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 the 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:
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 Memcached's addServer method 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:
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 username
'memcached_pass' => 'your ocs password', // Your OCS password
Extension driver
Then, open the bootstrap/start.php file and insert the code before return $app; in the last line:
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 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:
The code is as follows:
"driver" => "file", // The default is to use file caching
Modified to:
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. (