kick-in / memcache-bundle
Web调试工具栏中的Memcache Doctrine缓存和会话存储
Requires
- php: >=7.2
- ext-memcache: >=3.0
- symfony/framework-bundle: >=3.4
Requires (Dev)
- phpunit/phpunit: ~4.0
- dev-master / 3.x-dev
- v3.0
- v2.1.12
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.2.18
- v1.2.17
- v1.2.16
- v1.2.15
- v1.2.14
- v1.2.13
- v1.2.12
- v1.2.11
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
This package is auto-updated.
Last update: 2021-01-26 20:16:24 UTC
README
LswMemcacheBundle
如果您想优化您的Web应用程序以应对高负载和/或低加载时间,Memcache是一个不可或缺的工具。它将管理您的会话数据,而无需在Web或数据库服务器上进行磁盘I/O操作。您还可以将其作为网站的中心对象存储来运行。在这个角色中,它通过使用Doctrine缓存支持或通过实现使用Memcache "get" 和 "set" 命令的缓存来缓存数据库查询或昂贵的API调用。
此Symfony扩展包将为Symfony和Doctrine提供Memcache集成,用于会话存储和缓存。它具有完整的Web调试工具栏集成,允许您分析和调试缓存行为和性能。
阅读关于LswMemcacheBundle的LeaseWebLabs博客
需求
- PHP 7.2或更高版本
- php-memcache 3.0.6或更高版本
- memcached 1.4或更高版本
注意:此包不再使用使用 "libmemcached" 的 PHP "memcached" 扩展,请参阅 "注意事项"。
PHP7支持可以通过编译和安装:https://github.com/websupport-sk/pecl-memcache/tree/php7
安装
要使用Composer安装LswMemcacheBundle,只需执行以下命令
php composer.phar require kick-in/memcache-bundle
最后,将此包添加到'app/AppKernel.php'文件中的AppKernel类的registerBundles函数
public function registerBundles()
{
$bundles = array(
...
new Lsw\MemcacheBundle\LswMemcacheBundle(),
...
);
通过向app/config/config.yml添加以下内容来配置此包
lsw_memcache: session: pool: default pools: default: servers: - { host: localhost, tcp_port: 11211 }
安装以下依赖项(在基于Debian的系统上使用 'apt')
apt-get install memcached php-memcache
不要忘记在添加Memcache模块后重新启动您的Web服务器。现在,Memcache信息应该会在您的调试工具栏中显示一个小双向箭头(快进)图标。
使用方法
当您想从控制器中调用缓存时,可以简单调用
$this->get('memcache.default')->set('someKey', 'someValue', 0, $timeToLive);
$this->get('memcache.default')->get('someKey');
上面的示例显示了如何将值 'someValue' 存储在键 'someKey' 下,最多 $timeToLive 秒(0参数是 'flags')。在第二行中,从Memcache检索值。如果找不到键或指定的秒数已过,则'get'函数返回值 'false'。
配置
以下是一个此包的示例配置。
lsw_memcache: pools: default: servers: - { host: 10.0.0.1, tcp_port: 11211, weight: 15 } - { host: 10.0.0.2, tcp_port: 11211, weight: 30 } options: allow_failover: true max_failover_attempts: 20 default_port: 11211 chunk_size: 32768 protocol: ascii hash_strategy: consistent hash_function: crc32 redundancy: true session_redundancy: 2 compress_threshold: 20000 lock_timeout: 15 sessions: servers: - { host: localhost, tcp_port: 11212 }
会话支持
此包还提供了在Memcache服务器上存储会话数据的支持。要启用会话支持,您必须通过 session
键启用它(默认情况下 auto_load
是 true)。请注意,会话支持的唯一必需子键是 pool
(一个有效的池)。您还可以指定一个 prefix
和一个 ttl
。
lsw_memcache: session: pool: sessions auto_load: true prefix: "session_" ttl: 7200 locking: true spin_lock_wait: 150000 # pools
请注意,会话锁定默认启用,默认的自旋锁定设置为每150毫秒轮询一次(150000微秒)。
负载均衡器后运行的应用程序的会话管理
当您的应用程序在多个服务器上运行时,您必须意识到所有实例都应该与1个缓存服务器进行通信以保持一致性;否则,每个实例都有自己的会话,这会产生意外结果。
为了避免上述问题,您必须将LockingSessionHandler作为服务添加。通过这样做,所有实例都将使用会话处理程序,并且会话处理程序会将数据存储在配置的memcache服务器中。
my.memcache.service: class: Memcache calls: - [addServer, ['your_memcache_address', 'your_memcache_port']] my.memcached.session.handler: class: Lsw\MemcacheBundle\Session\Storage\LockingSessionHandler arguments: - "@my.memcache.service" - prefix: 'your_prefix' - expiretime: 'your_expire_time'
Doctrine支持
此包还提供了对Memcache服务器上Doctrine缓存的支撑。要启用Doctrine缓存,您必须通过doctrine
键启用它。请注意,您可以指定所有三种Doctrine缓存类型:'metadata'、'result'和'query'。这些子键下的所需键是pool
(有效的池)和entity_manager
(通常是:默认)。您还可以指定一个prefix
。
lsw_memcache: doctrine: metadata_cache: pool: default entity_manager: default # the name of your entity_manager connection document_manager: default # the name of your document_manager connection result_cache: pool: default entity_manager: [default, read] # you may specify multiple entity_managers prefix: "result_" # you may specify a prefix for the entries query_cache: pool: default entity_manager: default # pools
防火墙支持
此包还提供了支持防火墙,限制每个IP地址的并发请求数量。它维护每个IP地址的运行请求计数器,并在必要时延迟(节流)请求。要启用防火墙支持,您必须通过firewall
键启用它。请注意,防火墙支持的唯一所需子键是pool
(有效的池)。您还可以指定一个prefix
和一个concurrency
(默认为10)。如果您使用一个或多个反向代理,则必须在reverse_proxies
键中指定它们。
lsw_memcache: firewall: pool: firewall prefix: "firewall_" concurrency: 10 reverse_proxies: [10.0.0.1] # pools
ADP:反犬群效应
让我们分析一个高流量网站案例,看看Memcache的表现如何。
您的缓存存储时间为90分钟。计算缓存值大约需要3秒钟,从缓存中读取缓存值大约需要1毫秒。您大约每秒有5000个请求,假设值已被缓存。您每秒得到5000个请求,从缓存中读取值大约需要5000毫秒。您可能认为这是不可能的,因为5000 > 1000,但这取决于您的Web服务器上的工作进程数量。假设大约有100个工作进程(在高负载下),每个工作进程有75个线程。您的Web请求大约需要20毫秒。每当缓存无效化(90分钟后),在3秒内,将有15000个请求得到缓存缺失。所有得到缺失的线程将开始计算缓存值(因为它们不知道其他线程正在做相同的事情)。这意味着在(几乎)3秒内,服务器不会回答任何请求,但请求仍然持续到来。由于每个工作进程有75个线程(持有100 x 75个连接),为了处理它们,工作进程的数量必须增加。
大量地创建进程会导致额外的CPU使用,并且每个工作进程将使用额外的RAM。这种意外的RAM和CPU增加被称为“犬群效应”或“冲击群”或“暴风雨群”,在Web服务的峰值时段是非常不受欢迎的。
有一个解决方案:我们在计算新值的同时服务旧的缓存条目,并通过使用原子读写操作,我们可以确保当内容无效化时,只有一个线程会收到缓存缺失。该算法在LswMemcacheBundle的AntiDogPileMemcache类中实现。它提供了getAdp()、setAdp()和deleteAdp()函数,可以用作正常get、set和delete的替代。
请注意
- 如果您击中量较少或计算新值相对较快,可能不需要ADP。
- 如果可以将大计算分解成更小的部分,甚至为每个部分设置不同的超时时间,可能就不需要ADP。
- ADP可能提供比指定的无效化更旧的数据。特别是当一个线程/工作进程在“get”请求中返回“false”,但在之后未能“set”新计算值时。
- ADP的“getAdp”、“setAdp”和“deleteAdp”比正常的“get”、“set”和“delete”更昂贵,这会减慢所有缓存命中。
- ADP不能保证不会发生狗堆效应。重启Memcache、清除数据或RAM不足也会导致键被驱逐,您仍然会遇到问题。
注意事项
LswMemcacheBundle使用的是'memcache' PHP扩展(memcached客户端),而不是基于libmemcache的'memcached' PHP扩展。
此捆绑包的1.0主要版本使用了其他扩展。在捆绑包的2.0主要版本中,选择了功能完整的PECL "memcache" 3.0.8版本(没有'd'),因为它具有完整的特性集,并且设计良好,支持良好。
已知问题
触发memcache set操作的会话写入在页面渲染之后执行。memcache数据收集器的collect调用在页面渲染完成之前执行,因此也在会话写入执行之前。这导致会话写入不会显示在Web调试工具栏中。
致谢
Doctrine支持基于SncRedisBundle的实现
https://github.com/snc/SncRedisBundle 由Henrik Westphal
- DependencyInjection/LswMemcacheExtension.php
- DependencyInjection/Configuration.php
- DependencyInjection/Compiler/EnableSessionSupport.php
是基于以下实现的
https://github.com/Emagister/MemcachedBundle 由Christian Soronellas
- Command/StatisticsCommand.php
是基于以下实现的
https://github.com/beryllium/CacheBundle 由Kevin Boyd
许可证
此捆绑包受MIT许可证的约束。