欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

多级缓存的实现---责任链模式

程序员文章站 2024-04-04 12:48:11
...
多级缓存责任链模式。
* 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. */
复制代码