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,
],
],
],

至此完工,尽情享用吧。

后话,不知是不是c/c++搞多了的原因,刚开始用php做项目时其实我很不喜欢这些框架,原始惯了什么都喜欢自己来写,后来发现不对呀现在网络发展得太快了,什么都自己来造的话明显是力不从心了,后来接触了第一个框架CI,用它做了个项目还不错,再后来看到大家都在说这个Laravel,自己跟着也看看怎么样,说实话你不用它来做项目的话还真不知道它的便捷性,就这样用它做了个网站感觉挺不错呢,就这样边学习边用上了。大家没试过的话其实可以试一试,毕竟什么都尝试一下才知道什么适合自己。

最后放个自己的小广告吧,这个就是我Laravel的第一个网站云南土特产,见笑了。想爽点纯正云南土特产的朋友也可以试一试。 网址:http://www.orangemall.cc (橘子坊云南土特产), my god!我去,心都碎了,怎么我的内容里不能搞链接,是等级不够吗???? 还是不爱写文章的原因,望指点!

让Laravel5支持memcache的方法的更多相关文章

  1. Memcache所有方法及参数详解

    memcache函数所有的方法列表如下: 参考http://www.php.net/manual/zh/function.Memcache-add.php Memcache::add - 添加一个值, ...

  2. 服务器配置ssl证书支持苹果ATS方法

    服务器配置ssl证书支持苹果ATS方法 发布日期:2016-12-14 苹果安全工程&架构部门主管Ivan Kristic表示ATS将在今年底成为App Sotre app的必要条件,这将大幅 ...

  3. C#中扩展StringBuilder支持链式方法

    本篇体验扩展StringBuilder使之支持链式方法. 这里有一个根据键值集合生成select元素的方法. private static string BuilderSelectBox(IDicti ...

  4. max-height,min-height在IE下不支持的解决方法

    max-height,min-height在IE下不支持的解决方法 max-width:160px; max-height:160px; _width:expression(this.width &g ...

  5. 4种检测是否支持HTML5的方法,你知道几个?

    4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...

  6. SAE下的Memcache使用方法

    SAE里面有Memcache,可以较大幅度改善数据库的鸭梨~ 之前一直想学习Memcache,却愁于不知如何下手,对这个名词完全没有概念,同时在SAE的文档里面,也很少对于Memcache的使用教程~ ...

  7. 正确的lnamp支持SSI的方法!即支持SHTML和include调用!

    正确的lnamp支持SSI的方法!即支持SHTML和include调用! 个地方:一个是apache和nginx里的conf文件 第一步:修改apache里的httpd.conf文件 查找:AddTy ...

  8. WPF PasswordBox不支持绑定解决方法

    原文:WPF PasswordBox不支持绑定解决方法 PasswordBox的Password属性因为安全原因不支持直接绑定,可以使用依赖属性实现.直接插入代码 public class Passw ...

  9. 兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法

    兼容firefox,ie,谷歌,阻止浏览器冒泡事件,Firefox不支持event解决方法 // 获取事件function getEvent(){ if(window.event) {return w ...

随机推荐

  1. 在适配iPhone 6 Plus屏幕的时候,模拟器上两边有很细的白边如何解决

    取消掉Constrain to margin 然后添加左右约束 版权声明:本文为博主原创文章,未经博主允许不得转载.    

  2. 第一百九十三天 how can I 坚持

    我以为我是谁. 你可以记录你今天看了电影 也可以记录你最近在听什么歌 但这都会成为回忆 .  ---oncelife 快受不了了啊.咋办. 今天看了<滚蛋吧,肿瘤君>,还看了<那山那 ...

  3. [置顶] 如何在浏览器中打开PDF文件并实现预览的思路与代码

    编写项目遇到一个需要在浏览器中打开PDF文件的问题.最终实现效果如下: 其实也就是简单的在浏览器中实现一个打开pdf文件,并有类似预览功能的边框. 其实在网上经常见到类似的页面,在浏览器中打开pdf文 ...

  4. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  5. C# 扩展方法奇思妙用高级篇六:WinForm 控件选择器

    在Web开发中,jQuery提供了功能异常强大的$选择器来帮助我们获取页面上的对象.但在WinForm中,.Net似乎没有这样一个使用起来比较方便的选择器.好在我们有扩展方法,可以很方便的打造一个. ...

  6. GetQueuedCompletionStatus的返回值

    完成端口GetQueuedCompletionStatus返回值的问题 先看看GetQueuedCompletionStatus函数的完整声明:BOOL GetQueuedCompletionStat ...

  7. iOS单例 宏定义

    #define singleton_interface(className) \ + (className *)shared##className; // @implementation #defin ...

  8. C# List 中 Find 方法

    实例化一个集合 List<User> userCollection = new List<User>(); userCollection.Add(new User(1, &qu ...

  9. OpenStack tokens id获取测试

     

  10. Oracle闪回

    在PLSQL开发时,有时候会遇到对表的误删除,其实遇到这种情况不需要紧张,如果问题较大,请DBA帮忙,如果只是小问题,只需自己处理,利用flashback闪回操作即可,可将表进行恢复 在删除表时,系统 ...