Laravel5框架在Cache和Session中不支持Memcache,看清了是Memcache而不是Memcached哦,MemCached是支持的但是这个扩展真的是装的蛋疼,只有修改部分源码让其来支持memcache了。具体修改部分如下:

找到sessioni管理器 Laravel\vendor/laravel/framework/src/Illuminate/Session/SessionManager.php,并增加如下代码:

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

接下来修改cache部分,找到Laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php
在该文件中增加以下代码:

/**
* Create an instance of the Memcache cache driver.
*
* @return IlluminateCacheMemcachedStore
*/
protected function createMemcacheDriver(array $config)
{
$prefix = $this->getPrefix($config); $memcache = $this->app['memcache.connector']->connect($config['servers']); return $this->repository(new MemcacheStore($memcache, $prefix));
}

接下来找到文件: Laravel/vendor/laravel/framework/src/Illuminate/Cache/CacheServiceProvider.php 将memcache注册进去
修改如下:

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('cache', function ($app) {
return new CacheManager($app);
}); $this->app->singleton('cache.store', function ($app) {
return $app['cache']->driver();
}); $this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
}); $this->app->singleton('memcache.connector',function() {
       // 这里返回了MemcacheConnector,记得在Cache目录下创建这个class
return new MemcacheConnector;
});
$this->registerCommands();
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'cache', 'cache.store', 'memcached.connector', 'command.cache.clear','memcache.connector'
];
}

我们看到这个闭包函数中返回了MemcacheContector的对象,现在来创建此类。文件位置:Laravel/vendor/laravel/framework/src/Illuminate/Cache/MemcacheConnector.php

<?php
namespace Illuminate\Cache; use Memcache;
use RuntimeException; 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("Could not establish Memcache connection.");
} return $memcache;
} /**
* Get a new Memcache instance.
*
* @return Memcached
*/
protected function getMemcache()
{
return new Memcache;
}
}

按照第二步的流程这个文件会执行connect的方法,执行完成之后返回一个Memcache的对象, 这时候第二步那里根据这边来创建了一个MemcacheStore的存储器。

文件地址:Laravel/vendor/laravel/framework/src/Illuminate/Cache/MemcacheStore.php

<?php

namespace Illuminate\Cache;

use Memcache;
use Illuminate\Contracts\Cache\Store; class MemcacheStore extends TaggableStore implements Store
{
/**
* The Memcached instance.
*
* @var \Memcached
*/
protected $memcached; /**
* A string that should be prepended to keys.
*
* @var string
*/
protected $prefix; public function __construct(Memcache $memcache, $prefix = '')
{
$this->memcache = $memcache;
$this->prefix = strlen($prefix) > 0 ? $prefix.':' : '';
} /**
* Retrieve an item from the cache by key.
*
* @param string|array $key
* @return mixed
*/
public function get($key)
{
return $this->memcache->get($this->prefix.$key);
} /**
* Retrieve multiple items from the cache by key.
*
* Items not found in the cache will have a null value.
*
* @param array $keys
* @return array
*/
public function many(array $keys)
{
$prefixedKeys = array_map(function ($key) {
return $this->prefix.$key;
}, $keys); $values = $this->memcache->getMulti($prefixedKeys, null, Memcache::GET_PRESERVE_ORDER); if ($this->memcache->getResultCode() != 0) {
return array_fill_keys($keys, null);
} return array_combine($keys, $values);
} /**
* 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);
} /**
* Store multiple items in the cache for a given number of minutes.
*
* @param array $values
* @param int $minutes
* @return void
*/
public function putMany(array $values, $minutes)
{
$prefixedValues = []; foreach ($values as $key => $value) {
$prefixedValues[$this->prefix.$key] = $value;
} $this->memcache->setMulti($prefixedValues, $minutes * 60);
}
/**
* Store an item in the cache if the key doesn't exist.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return bool
*/
public function add($key, $value, $minutes)
{
return $this->memcache->add($this->prefix.$key, $value, $minutes * 60);
} /**
* Increment the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function increment($key, $value = 1)
{
return $this->memcache->increment($this->prefix.$key, $value);
} /**
* Decrement the value of an item in the cache.
*
* @param string $key
* @param mixed $value
* @return int|bool
*/
public function decrement($key, $value = 1)
{
return $this->memcache->decrement($this->prefix.$key, $value);
} /**
* Store an item in the cache indefinitely.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function forever($key, $value)
{
$this->put($key, $value, 0);
} /**
* Remove an item from the cache.
*
* @param string $key
* @return bool
*/
public function forget($key)
{
return $this->memcache->delete($this->prefix.$key);
} /**
* Remove all items from the cache.
*
* @return void
*/
public function flush()
{
$this->memcache->flush();
} /**
* Get the underlying Memcached connection.
*
* @return \Memcached
*/
public function getMemcached()
{
return $this->memcache;
} /**
* Get the cache key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
} /**
* Set the cache key prefix.
*
* @param string $prefix
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
}
}

上述步骤操作完成后,接下来修改一下config.php中的驱动部分,增加如下代码段:

// 修改为memcached
'default' => env('CACHE_DRIVER', 'memcache'), // stores 部分增加memcache配置
'memcache' => [
'driver' => 'memcache',
'servers' => [
[
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'weight' => 100,
],
],
],

至此完工,尽情享用吧。

laravel 框架memcache的配置的更多相关文章

  1. Laravel框架的一些配置

    服务器的配置 1.在apache下的配置 配置httpd-conf:php5_module.rewrite_module.Listen 配置extra/httpd-vhost:端口.站点.域名.默认首 ...

  2. laravel框架的安装与配置

    正常安装: 1.composer.(https://getcomposer.org/Composer-Setup.exe) 安装之前要确保目录:wamp\bin\php\php5.4.3下的php.i ...

  3. laravel 框架配置404等异常页面的方法详解(代码示例)

    本篇文章给大家带来的内容是关于laravel 框架配置404等异常页面的方法详解(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在Laravel中所有的异常都由Handl ...

  4. CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 2018.3.11

    CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 阿里云服务器的选择 当然是选择学生优惠啦.这里阿里云还提供了轻量级服务器这个选项,可以预装 LA ...

  5. CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架

    <!doctype html> CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架.mdhtml {overflow-x: initia ...

  6. PHP开源框架Laravel的安装与配置

    编将带领大家一步步在Windows 7平台下搭建该框架: 工具/原料   windows 7 Composer Laravel最新框架 方法/步骤   1 安装composer.安装之前要确保目录:w ...

  7. [php]laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

  8. php的laravel框架快速集成微信登录

    最终的解决方案是:https://github.com/liuyunzhuge/php_weixin_provider,详细的介绍请往下阅读. 本文面向的是php语言laravel框架的用户,介绍的是 ...

  9. [麦先生]初学Laravel框架与ThinkPHP框架的不同(2)

    在经过了一段时间的开发后,我对Laravel框架的认识又在逐步的加深,对于这个世界占有量NO.1的框架...  我想说,我已经在逐步的感受到他的恐怖之处... 一.建表--Laravel在数据库建表上 ...

随机推荐

  1. C#中的foreach语句与枚举器接口(IEnumerator)及其泛型 相关问题

    这个问题从<C#高级编程>数组一节中的foreach语句(6.7.2)发现的. 因为示例代码与之前的章节连贯,所以我修改了一下,把自定义类型改为了int int[] bs = { 2, 3 ...

  2. pyqt的 .ui 转换为 .py 后的操作注意事项

    1. 增加 import sys 2. 将 Ui_MainWindow(object) 中的 object 修改成修改成 QtGui.QMainWindow 3. 在 Ui_MainWindow 类中 ...

  3. Monkey实例测试

    三.Monkey实例测试 Windows下(注:2-4步是为了查看我们可以测试哪些应用程序包,可省略): 1. 通过eclipse启动一个Android的emulator或者2 2. 在命令行中输入: ...

  4. js五种设计模式

    1.js工厂模式 var lev=function(){ return "嘿哈"; }; function Parent(){ var Child = new object(); ...

  5. php投票练习

    一.题目要求:建立如下两个数据库,实现投票和%的统计结果: 二.具体编写方法: (1).建立数据库: 里面的蓝色背景的表格名称就是我们所需的表格! 表格内容如下: 表名:diaoyantimu 表名: ...

  6. MongoDB分布式

    结构: ShardServer1:27020 ShardServer2:27021 ShardServer3:27022 ShardServer4:27023 ConfigServer:27100 R ...

  7. angular : direative : scope | 指令scope和transclude的关系

    今天记入的是指令的scope和transclude关系 a 和 b 都是指令 <div a> <div b></div> </div> a transc ...

  8. 页面添加数据的PHP

    (接前面写的) 第一个页面tianjia.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  9. webpack-react之webpack篇(http://www.jianshu.com/p/794d573d2c53)

    构建一个小项目——FlyBird,学习webpack和react.(本文成文于2017/2/25) 从webpack开始本篇从零开始,详细记录webpack的各个方面.文章中将会放入很多链接以便扩展, ...

  10. 用GDB调试程序

    转自:http://blog.csdn.net/haoel/article/details/2879 是一篇从基础讲gdb的博文 用GDB调试程序 GDB概述---- GDB是GNU开源组织发布的一个 ...