搜索
首页后端开发php教程多级缓存的实现---责任链模式

多级缓存责任链模式。
* client提交给 hander,hander发现责任链上能处理该任务的函数,处理;可以归纳为:用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合, 唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去。
  1. /**
  2. * \责任链模式,其目的是组织一个对象链处理一个如方法调用的请求。
  3. *
  4. * 最著名的责任链示例:多级缓存。
  5. * client提交给 hander,hander发现责任链上能处理该任务的函数,处理;
  6. * 可以归纳为:用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合,
  7. * 唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,
  8. * 就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去。
  9. */
  10. /**
  11. * The Handler abstraction. Objects that want to be a part of the
  12. * ChainOfResponsibility must implement this interface directly or via
  13. * inheritance from an AbstractHandler.
  14. * 处理抽象类,对象如果想成为责任链的一部分必须直接实现这个接口或
  15. * 继承一个抽象的处理类
  16. */
  17. interface KeyValueStore{
  18. /**
  19. * Obtain a value.
  20. * @param string $key
  21. * @return mixed
  22. */
  23. public function get($key);
  24. }
  25. /**
  26. * Basic no-op implementation which ConcreteHandlers not interested in
  27. * caching or in interfering with the retrieval inherit from.
  28. * 接收一个请求,设法满足它,如果不成功就委派给下一个处理程序。
  29. */
  30. abstract class AbstractKeyValueStore implements KeyValueStore{
  31. protected $_nextHandler;
  32. public function get($key){
  33. return $this->_nextHandler->get($key);
  34. }
  35. }
  36. /**
  37. * Ideally the last ConcreteHandler in the chain. At least, if inserted in
  38. * a Chain it will be the last node to be called.
  39. * 理想情况下,责任链上最后的具体处理类,加入链上,将是最后被调用的节点。
  40. */
  41. class SlowStore implements KeyValueStore{
  42. /**
  43. * This could be a somewhat slow store: a database or a flat file.
  44. */
  45. protected $_values;
  46. public function __construct(array $values = array()){
  47. $this->_values = $values;
  48. }
  49. public function get($key){
  50. return $this->_values[$key];
  51. }
  52. }
  53. /**
  54. * A ConcreteHandler that handles the request for a key by looking for it in
  55. * its own cache. Forwards to the next handler in case of cache miss.
  56. * 在缓存没命中的情况下,转发到下一个处理对象
  57. */
  58. class InMemoryKeyValueStore implements KeyValueStore{
  59. protected $_nextHandler;
  60. protected $_cached = array();
  61. public function __construct(KeyValueStore $nextHandler){
  62. $this->_nextHandler = $nextHandler;
  63. }
  64. protected function _load($key){
  65. if (!isset($this->_cached[$key])) {
  66. $this->_cached[$key] = $this->_nextHandler->get($key);
  67. }
  68. }
  69. public function get($key){
  70. $this->_load($key);
  71. return $this->_cached[$key];
  72. }
  73. }
  74. /**
  75. * A ConcreteHandler that delegates the request without trying to
  76. * understand it at all. It may be easier to use in the user interface
  77. * because it can specialize itself by defining methods that generates
  78. * html, or by addressing similar user interface concerns.
  79. * Some Clients see this object only as an instance of KeyValueStore
  80. * and do not care how it satisfy their requests, while other ones
  81. * may use it in its entirety (similar to a class-based adapter).
  82. * No client knows that a chain of Handlers exists.
  83. * 不用关心调用的具体实现的外部具体具体处理程序;背后是责任链。
  84. */
  85. class FrontEnd extends AbstractKeyValueStore{
  86. public function __construct(KeyValueStore $nextHandler){
  87. $this->_nextHandler = $nextHandler;
  88. }
  89. public function getEscaped($key){
  90. return htmlentities($this->get($key), ENT_NOQUOTES, 'UTF-8');
  91. }
  92. }
  93. // Client code
  94. $store = new SlowStore(
  95. array(
  96. 'pd' => 'Philip K. Dick',
  97. 'ia' => 'Isaac Asimov',
  98. 'ac' => 'Arthur C. Clarke',
  99. 'hh' => 'Helmut Hei.enbttel'
  100. )
  101. );
  102. // in development, we skip cache and pass $store directly to FrontEnd
  103. $cache = new InMemoryKeyValueStore($store);
  104. $frontEnd = new FrontEnd($cache);
  105. echo $frontEnd->get('ia'). "\n";
  106. echo $frontEnd->getEscaped('hh'). "\n";
  107. /**
  108. * expect: ...
  109. * Isaac Asimov
  110. * Helmut Hei.enbttel
  111. *
  112. * 参与者:
  113. ◆Client(客户端):向Handler(处理程序)提交一个请求;
  114. ◆Handler(处理程序)抽象:接收一个请求,以某种方式满足它;
  115. ◆ConcreteHandlers(具体的处理程序):接收一个请求,设法满足它,如果不成功就委派给下一个处理程序。
  116. */
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
您如何修改PHP会话中存储的数据?您如何修改PHP会话中存储的数据?Apr 27, 2025 am 12:23 AM

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然后使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

举一个在PHP会话中存储数组的示例。举一个在PHP会话中存储数组的示例。Apr 27, 2025 am 12:20 AM

在PHP会话中可以存储数组。1.启动会话,使用session_start()。2.创建数组并存储在$_SESSION中。3.通过$_SESSION检索数组。4.优化会话数据以提升性能。

垃圾收集如何用于PHP会议?垃圾收集如何用于PHP会议?Apr 27, 2025 am 12:19 AM

PHP会话垃圾回收通过概率机制触发,清理过期会话数据。1)配置文件中设置触发概率和会话生命周期;2)可使用cron任务优化高负载应用;3)需平衡垃圾回收频率与性能,避免数据丢失。

如何在PHP中跟踪会话活动?如何在PHP中跟踪会话活动?Apr 27, 2025 am 12:10 AM

PHP中追踪用户会话活动通过会话管理实现。1)使用session_start()启动会话。2)通过$_SESSION数组存储和访问数据。3)调用session_destroy()结束会话。会话追踪用于用户行为分析、安全监控和性能优化。

如何使用数据库存储PHP会话数据?如何使用数据库存储PHP会话数据?Apr 27, 2025 am 12:02 AM

利用数据库存储PHP会话数据可以提高性能和可扩展性。1)配置MySQL存储会话数据:在php.ini或PHP代码中设置会话处理器。2)实现自定义会话处理器:定义open、close、read、write等函数与数据库交互。3)优化和最佳实践:使用索引、缓存、数据压缩和分布式存储来提升性能。

简单地说明PHP会话的概念。简单地说明PHP会话的概念。Apr 26, 2025 am 12:09 AM

phpsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIdStoredInacookie.here'showtomanageThemeffectionaly:1)startAsessionWithSessionwwithSession_start()和stordoredAtain $ _session.2)

您如何循环中存储在PHP会话中的所有值?您如何循环中存储在PHP会话中的所有值?Apr 26, 2025 am 12:06 AM

在PHP中,遍历会话数据可以通过以下步骤实现:1.使用session_start()启动会话。2.通过foreach循环遍历$_SESSION数组中的所有键值对。3.处理复杂数据结构时,使用is_array()或is_object()函数,并用print_r()输出详细信息。4.优化遍历时,可采用分页处理,避免一次性处理大量数据。这将帮助你在实际项目中更有效地管理和使用PHP会话数据。

说明如何使用会话进行用户身份验证。说明如何使用会话进行用户身份验证。Apr 26, 2025 am 12:04 AM

会话通过服务器端的状态管理机制实现用户认证。1)会话创建并生成唯一ID,2)ID通过cookies传递,3)服务器存储并通过ID访问会话数据,4)实现用户认证和状态管理,提升应用安全性和用户体验。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具