Implementation of multilevel cache---responsibility chain model

Source: Internet
Author: User
Multilevel cache responsibility chain mode.
* The client submits to hander,hander the function that can handle the task on the chain of discovery, processing, can be summed up as: A series of classes (classes) to try to handle a request requests, these classes are a loose coupling, The only thing in common is to pass the request between them. That is, a request, a class first processing, if not processed, is passed to Class B processing, if not processed, passed to the Class C processing, just like a chain (chain) passed down.
  1. /**
  2. * \ Responsibility chain mode, the purpose is to organize an object chain to process a request such as a method call.
  3. *
  4. * Best-known chain of responsibility Examples: multilevel caching.
  5. * The client submits to hander,hander the function that can handle the task on the responsibility chain, processing;
  6. * Can be summed up as: Using a series of classes (classes) to try to handle a request requests, these classes are a loose coupling,
  7. * The only thing in common is to pass the request between them. In other words, a request is made, a class is processed first, and if not processed,
  8. * Passed to Class B processing, if not processed, passed to Class C processing, just like a chain (chain) passed down.
  9. */
  10. /**
  11. * The Handler abstraction. Objects that want to being a part of the
  12. * Chainofresponsibility must implement this interface directly or via
  13. * Inheritance from an abstracthandler.
  14. * Handling abstract classes, objects that must be implemented directly if they want to be part of the chain of responsibility
  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, if unsuccessful, delegates to the next handler.
  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'll be, the last node to be called.
  39. * Ideally, the last specific processing class on the chain of responsibility, added to the chain, will be the last node to be called.
  40. */
  41. Class Slowstore implements keyvaluestore{
  42. /**
  43. * This could is 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 the case of cache dead, 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 is easier to use 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 about it satisfy their requests, while other ones
  81. * May use it on it entirety (similar to a class-based adapter).
  82. * No client knows that a chain of handlers exists.
  83. * Do not care about the specific implementation of the external specific processing procedures; Behind the chain of responsibility.
  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: Submits a request to the handler (handler);
  114. Handler (handler) Abstraction: receives a request to satisfy it in some way;
  115. Concretehandlers (Specific handler): Receives a request, tries to satisfy it, and, if unsuccessful, delegates to the next handler.
  116. */
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.