Enable laravel to support memcache configuration

Source: Internet
Author: User
Tags memcached mixed redis

1: Find this file:
Laravel/vendor/laravel/framework/src/Illuminate/Session/SessionManager. php
This is a session manager file.
Add the following code:

The code is as follows: Copy code

/**
* Create an instance of the Memcached session driver.
  *
* @ Return IlluminateSessionStore
*/
Protected function createMemcacheDriver ()
 {
Return $ this-> createCacheBased ('memcache ');
 }

This is mainly the cache support of a memcahce,
We know that in laravel, many of the memcahce driver logic is implemented by the cache driver logic, so we need to modify the cache.
2: find the file:
Laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheManager. php
Add the following code to the file:

The code is as follows: Copy code

/**
* Create an instance of the Memcache cache driver.
  *
* @ Return IlluminateCacheMemcachedStore
*/
Protected function createMemcacheDriver ()
 {
$ Servers = $ this-> app ['config'] ['cache. Memcached'];

$ Memcache = $ this-> app ['memcache. Ctor ctor ']-> connect ($ servers );

Return $ this-> repository (new MemcacheStore ($ memcache, $ this-> getPrefix ()));
 }

In fact, the final driving trend of session memcahce still follows this method, so we can see the code "$ this-> app ['memcache. Ctor ctor ']->" In this method,
So where is the connector stored in the app attribute?
It is actually registered when we start the app and set the relevant serviceProvider, then we find the file:
Laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheServiceProvider. php
Modify as follows:

The code is as follows: Copy code

/**
* Register the service provider.
  *
* @ Return void
*/
Public function register ()
 {
$ This-> app ['cache'] = $ this-> app-> share (function ($ app)
  {
Return new CacheManager ($ app );
});

$ This-> app ['memcached. Ctor ctor '] = $ this-> app-> share (function ()
  {
Return new MemcachedConnector;
});
// Add the following code
$ This-> app ['memcache. Ctor ctor '] = $ this-> app-> share (function ()
  {
Return new MemcacheConnector;
});

$ This-> registerCommands ();
 }


We can see that the closure function returns the object of MemcacheContector. How can we get this class? Let's create one.
File address: Laravel/vendor/laravel/framework/src/Illuminate/Cache/memcacheconne. php
The file code is as follows: in fact, it was modified using the code of MemcachedContector.

The code is as follows: Copy code

<? Php namespace IlluminateCache;

Use Memcache;

Class MemcacheConnector {

/**
* Create a new Memcached connection.
  *
* @ Param array $ servers
* @ Return Memcache
*/
Public function connect (array $ servers)
 {
$ Memcache = $ this-> getMemcache ();

// For each server in the array, we'll just extract the configuration and add
// The server to the Memcache connection. Once we have added all of these
// Servers we'll verify the connection is successful and return it back.
Foreach ($ servers as $ server)
  {
$ Memcache-> addServer ($ server ['host'], $ server ['port'], $ server ['weight']);
  }

If ($ memcache-> getVersion () = false)
  {
Throw new RuntimeException ("cocould not establish Memcache connection .");
  }

Return $ memcache;
 }

/**
* Get a new Memcache instance.
  *
* @ Return Memcached
*/
Protected function getMemcache ()
 {
Return new Memcache;
 }
}


According to the process in step 2, the file will execute the connect method. After the execution is complete, a Memcache object will be returned,
At this time, the second step will create a MemcacheStore based on the memory here, so this file also needs to be created. It is very simple. Just change it with a copy of memcachedSrote.
File address: Laravel/vendor/laravel/framework/src/Illuminate/Cache/MemcacheStore. php
After copying the file, we need to change the following:
1: Application class name:

The code is as follows: Copy code

Use Memcache;

Class MemcacheStore implements StoreInterface {


Type of the constructor:

The code is as follows: Copy code
Public function _ construct (Memcache $ memcache, $ prefix = '')
 {
$ This-> memcache = $ memcache;
$ This-> prefix = strlen ($ prefix)> 0? $ Prefix .':':'';
 }

To modify the get method, we can directly return the value.

The code is as follows: Copy code
/**
* Retrieve an item from the cache by key.
  *
* @ Param string $ key
* @ Return mixed
*/
Public function get ($ key)
 {
Return $ this-> memcache-> get ($ this-> prefix. $ key );
 }

Modify the put method:

The code is as follows: Copy code

/**
* Store an item in the cache for a given number of minutes.
  *
* @ Param string $ key
* @ Param mixed $ value
* @ Param int $ minutes
* @ Return void
*/
Public function put ($ key, $ value, $ minutes)
 {
$ Compress = is_bool ($ value) | is_int ($ value) | is_float ($ value )? False: MEMCACHE_COMPRESSED;

$ This-> memcache-> set ($ this-> prefix. $ key, $ value, $ compress, $ minutes * 60 );
 }

This is basically the end of the change.
So I can use memcahe as the driver when using cache and session. What should I do with the configuration information of this memcache?
Just like the memcached configuration, you can configure it in the memcached index of cache. php.

Here, we will explain the problem that redis cannot use the specified server mentioned in the previous article. In fact, both the cache and session. php configuration files have a connection index.
The configuration item can be specified here. For example, connection => session indicates that the configuration information specified by the redis session key of database. php is used.

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.