Home > Backend Development > PHP Tutorial > Implementation of multi-level cache---chain of responsibility model

Implementation of multi-level cache---chain of responsibility model

WBOY
Release: 2016-07-25 09:06:21
Original
1144 people have browsed it
Multi-level caching chain of responsibility model.
* The client submits it to the handler, and the handler finds a function in the chain of responsibility that can handle the task and processes it; it can be summarized as: using a series of classes to try to handle a request. There is a loose coupling between these classes, and the only thing they have in common is The point is to pass the request between them. In other words, when a request comes, Class A will handle it first. If it is not processed, it will be passed to Class B for processing. If it is not processed, it will be passed to Class C for processing. It is like a chain. (chain) is passed on.
  1. /**
  2. * Chain of Responsibility pattern, its purpose is to organize an object chain to handle a request such as a method call.
  3. *
  4. * The most famous example of chain of responsibility: multi-level caching.
  5. * The client submits it to the handler, and the handler finds a function in the chain of responsibility that can handle the task and processes it;
  6. * It can be summarized as: using a series of classes to try to handle a request, and there is a loose coupling between these classes.
  7. * The only thing they have in common is that requests are passed between them. In other words, when a request comes, Class A will handle it first. If it is not processed,
  8. * will be passed to Class B for processing. If it is not processed, it will be passed to Class C for processing. , and it is passed on like a 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. * Interface or
  15. * inherit an abstract processing class
  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. * Receives a request, tries to satisfy it, and delegates to the next handler if unsuccessful.
  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. * Ideally, the last concrete processing class on the responsibility chain is added to the chain and will is the last node called.
  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. * In case of cache miss, forward to the next processing object
  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. * Participants:
  113. ◆Client (client): Submit a request to Handler (handler);
  114. ◆Handler (handler) Abstract: Receive a request, satisfy it in some way;
  115. ◆ConcreteHandlers (concrete handlers): Receive a request, try to satisfy it, and delegate to the next handler if unsuccessful.
  116. */
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template