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

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

  1. /**
  2. * Create an instance of the Memcached session driver.
  3. *
  4. * @return IlluminateSessionStore
  5. */
  6. protected function createMemcacheDriver()
  7. {
  8. return $this->createCacheBased('memcache');
  9. }

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

  1. /**
  2. * Create an instance of the Memcache cache driver.
  3. *
  4. * @return IlluminateCacheMemcachedStore
  5. */
  6. protected function createMemcacheDriver(array $config)
  7. {
  8. $prefix = $this->getPrefix($config);
  9.  
  10. $memcache = $this->app['memcache.connector']->connect($config['servers']);
  11.  
  12. return $this->repository(new MemcacheStore($memcache, $prefix));
  13. }

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

  1. /**
  2. * Register the service provider.
  3. *
  4. * @return void
  5. */
  6. public function register()
  7. {
  8. $this->app->singleton('cache', function ($app) {
  9. return new CacheManager($app);
  10. });
  11.  
  12. $this->app->singleton('cache.store', function ($app) {
  13. return $app['cache']->driver();
  14. });
  15.  
  16. $this->app->singleton('memcached.connector', function () {
  17. return new MemcachedConnector;
  18. });
  19.  
  20. $this->app->singleton('memcache.connector',function() {
           // 这里返回了MemcacheConnector,记得在Cache目录下创建这个class
  21. return new MemcacheConnector;
  22. });
  23. $this->registerCommands();
  24. }
  1. /**
  2. * Get the services provided by the provider.
  3. *
  4. * @return array
  5. */
  6. public function provides()
  7. {
  8. return [
  9. 'cache', 'cache.store', 'memcached.connector', 'command.cache.clear','memcache.connector'
  10. ];
  11. }

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

  1. <?php
  2. namespace Illuminate\Cache;
  3.  
  4. use Memcache;
  5. use RuntimeException;
  6.  
  7. class MemcacheConnector {
  8.  
  9. /**
  10. * Create a new Memcached connection.
  11. *
  12. * @param array $servers
  13. * @return Memcache
  14. */
  15. public function connect(array $servers)
  16. {
  17. $memcache = $this->getMemcache();
  18.  
  19. // For each server in the array, we'll just extract the configuration and add
  20. // the server to the Memcache connection. Once we have added all of these
  21. // servers we'll verify the connection is successful and return it back.
  22. foreach ($servers as $server)
  23. {
  24. $memcache->addServer($server['host'], $server['port'], $server['weight']);
  25. }
  26.  
  27. if ($memcache->getVersion() === false)
  28. {
  29. throw new RuntimeException("Could not establish Memcache connection.");
  30. }
  31.  
  32. return $memcache;
  33. }
  34.  
  35. /**
  36. * Get a new Memcache instance.
  37. *
  38. * @return Memcached
  39. */
  40. protected function getMemcache()
  41. {
  42. return new Memcache;
  43. }
  44. }

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

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

  1. <?php
  2.  
  3. namespace Illuminate\Cache;
  4.  
  5. use Memcache;
  6. use Illuminate\Contracts\Cache\Store;
  7.  
  8. class MemcacheStore extends TaggableStore implements Store
  9. {
  10. /**
  11. * The Memcached instance.
  12. *
  13. * @var \Memcached
  14. */
  15. protected $memcached;
  16.  
  17. /**
  18. * A string that should be prepended to keys.
  19. *
  20. * @var string
  21. */
  22. protected $prefix;
  23.  
  24. public function __construct(Memcache $memcache, $prefix = '')
  25. {
  26. $this->memcache = $memcache;
  27. $this->prefix = strlen($prefix) > 0 ? $prefix.':' : '';
  28. }
  29.  
  30. /**
  31. * Retrieve an item from the cache by key.
  32. *
  33. * @param string|array $key
  34. * @return mixed
  35. */
  36. public function get($key)
  37. {
  38. return $this->memcache->get($this->prefix.$key);
  39. }
  40.  
  41. /**
  42. * Retrieve multiple items from the cache by key.
  43. *
  44. * Items not found in the cache will have a null value.
  45. *
  46. * @param array $keys
  47. * @return array
  48. */
  49. public function many(array $keys)
  50. {
  51. $prefixedKeys = array_map(function ($key) {
  52. return $this->prefix.$key;
  53. }, $keys);
  54.  
  55. $values = $this->memcache->getMulti($prefixedKeys, null, Memcache::GET_PRESERVE_ORDER);
  56.  
  57. if ($this->memcache->getResultCode() != 0) {
  58. return array_fill_keys($keys, null);
  59. }
  60.  
  61. return array_combine($keys, $values);
  62. }
  63.  
  64. /**
  65. * Store an item in the cache for a given number of minutes.
  66. *
  67. * @param string $key
  68. * @param mixed $value
  69. * @param int $minutes
  70. * @return void
  71. */
  72. public function put($key, $value, $minutes)
  73. {
  74. $compress = is_bool($value) || is_int($value) || is_float($value) ? false : MEMCACHE_COMPRESSED;
  75. $this->memcache->set($this->prefix.$key, $value, $compress, $minutes * 60);
  76. }
  77.  
  78. /**
  79. * Store multiple items in the cache for a given number of minutes.
  80. *
  81. * @param array $values
  82. * @param int $minutes
  83. * @return void
  84. */
  85. public function putMany(array $values, $minutes)
  86. {
  87. $prefixedValues = [];
  88.  
  89. foreach ($values as $key => $value) {
  90. $prefixedValues[$this->prefix.$key] = $value;
  91. }
  92.  
  93. $this->memcache->setMulti($prefixedValues, $minutes * 60);
  94. }
  95. /**
  96. * Store an item in the cache if the key doesn't exist.
  97. *
  98. * @param string $key
  99. * @param mixed $value
  100. * @param int $minutes
  101. * @return bool
  102. */
  103. public function add($key, $value, $minutes)
  104. {
  105. return $this->memcache->add($this->prefix.$key, $value, $minutes * 60);
  106. }
  107.  
  108. /**
  109. * Increment the value of an item in the cache.
  110. *
  111. * @param string $key
  112. * @param mixed $value
  113. * @return int|bool
  114. */
  115. public function increment($key, $value = 1)
  116. {
  117. return $this->memcache->increment($this->prefix.$key, $value);
  118. }
  119.  
  120. /**
  121. * Decrement the value of an item in the cache.
  122. *
  123. * @param string $key
  124. * @param mixed $value
  125. * @return int|bool
  126. */
  127. public function decrement($key, $value = 1)
  128. {
  129. return $this->memcache->decrement($this->prefix.$key, $value);
  130. }
  131.  
  132. /**
  133. * Store an item in the cache indefinitely.
  134. *
  135. * @param string $key
  136. * @param mixed $value
  137. * @return void
  138. */
  139. public function forever($key, $value)
  140. {
  141. $this->put($key, $value, 0);
  142. }
  143.  
  144. /**
  145. * Remove an item from the cache.
  146. *
  147. * @param string $key
  148. * @return bool
  149. */
  150. public function forget($key)
  151. {
  152. return $this->memcache->delete($this->prefix.$key);
  153. }
  154.  
  155. /**
  156. * Remove all items from the cache.
  157. *
  158. * @return void
  159. */
  160. public function flush()
  161. {
  162. $this->memcache->flush();
  163. }
  164.  
  165. /**
  166. * Get the underlying Memcached connection.
  167. *
  168. * @return \Memcached
  169. */
  170. public function getMemcached()
  171. {
  172. return $this->memcache;
  173. }
  174.  
  175. /**
  176. * Get the cache key prefix.
  177. *
  178. * @return string
  179. */
  180. public function getPrefix()
  181. {
  182. return $this->prefix;
  183. }
  184.  
  185. /**
  186. * Set the cache key prefix.
  187. *
  188. * @param string $prefix
  189. * @return void
  190. */
  191. public function setPrefix($prefix)
  192. {
  193. $this->prefix = ! empty($prefix) ? $prefix.':' : '';
  194. }
  195. }

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

  1. // 修改为memcached
  2. 'default' => env('CACHE_DRIVER', 'memcache'),
  3.  
  4. // stores 部分增加memcache配置
  5. 'memcache' => [
  6. 'driver' => 'memcache',
  7. 'servers' => [
  8. [
  9. 'host' => env('MEMCACHED_HOST', '127.0.0.1'),
  10. 'port' => env('MEMCACHED_PORT', 11211),
  11. 'weight' => 100,
  12. ],
  13. ],
  14. ],

至此完工,尽情享用吧。

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. ABP框架 - 嵌入资源

    文档目录 本节内容: 简介 创建嵌入文件 xproj/project.json 格式 csproj 格式 添加到嵌入式资源管理器 使用嵌入式视图 使用嵌入式资源 ASP.NET Core 配置 忽略文 ...

  2. html5_canvas初学

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. MongoDB学习总结(二) —— 基本操作命令(增删改查)

    上一篇介绍了MongoDB在Windows平台下的安装,这一篇介绍一下MongoDB的一些基本操作命令. 下面我们直奔主题,用简单的实例依次介绍一下. > 查看所有数据库 (show dbs) ...

  4. OC中extern,static,const的用法

    1.const的作用: const仅仅用来修饰右边的变量(基本数据变量p,指针变量*p). 例如 NSString *const SIAlertViewWillDismissNotification; ...

  5. python之数据库(mysql)操作

    前言: 最近开始学django了,学了下web框架,顿时感觉又会了好多知识.happy~~ 这篇博客整理写下数据库基本操作,内容挺少.明天写SQLAlchemy. 一.数据库基本操作 1. 想允许在数 ...

  6. Hibernate双向关联的增删改操作的属性

    双向关联关系下的增删改操作的属性                    1.cascade属性:  eg:<set name = "emps" cascade="s ...

  7. TCP四个计数器

    持续计时器 TCP 为每一个连接设有一个持续计时器. 只要 TCP 连接的一方收到对方的零窗口通知,就启动持续计时器. 若持续计时器设置的时间到期,就发送一个零窗口探测报文段(仅携带 1 字节的数据) ...

  8. smarty模板基础3 *缓存数据*

    缓存数据,这个并不是暂存的缓存,而是写入了内存的缓存 通过一个例子来书写:缓存数据 一.书写php和html页面的基本功能 既然是用smarty模板,那么前端和后端要分开写了 (1)php页面 < ...

  9. Debian安装Oracle Java步骤

    在Debian下安装OpenJDK使用apt命令非常方便的安装,但安装Oracle就需要手动了,这里需了解ln和update-alternatvies命令. ln链接 首先我们来说说linux的链接, ...

  10. 警惕!MySQL成数据勒索新目标

    据最新报道显示,继MongoDB和Elasticsearch之后,MySQL成为下个数据勒索目标,从2月12日凌晨开始,已有成百上千个开放在公网的MySQL数据库被劫持,删除了数据库中的存储数据,攻击 ...