Plugin to allow visitor contributions to WordPress posts, wiki style.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

113 lines
2.2 KiB

  1. <?php
  2. namespace Caxy\HtmlDiff;
  3. use Doctrine\Common\Cache\Cache;
  4. /**
  5. * Class DiffCache
  6. * @package Caxy\HtmlDiff
  7. */
  8. class DiffCache
  9. {
  10. /**
  11. * @var Cache
  12. */
  13. protected $cacheProvider;
  14. /**
  15. * DiffCache constructor.
  16. *
  17. * @param Cache $cacheProvider
  18. */
  19. public function __construct(Cache $cacheProvider)
  20. {
  21. $this->cacheProvider = $cacheProvider;
  22. }
  23. /**
  24. * @return Cache
  25. */
  26. public function getCacheProvider()
  27. {
  28. return $this->cacheProvider;
  29. }
  30. /**
  31. * @param Cache $cacheProvider
  32. *
  33. * @return DiffCache
  34. */
  35. public function setCacheProvider($cacheProvider)
  36. {
  37. $this->cacheProvider = $cacheProvider;
  38. return $this;
  39. }
  40. /**
  41. * @param string $oldText
  42. * @param string $newText
  43. *
  44. * @return bool
  45. */
  46. public function contains($oldText, $newText)
  47. {
  48. return $this->cacheProvider->contains($this->getHashKey($oldText, $newText));
  49. }
  50. /**
  51. * @param string $oldText
  52. * @param string $newText
  53. *
  54. * @return string
  55. */
  56. public function fetch($oldText, $newText)
  57. {
  58. return $this->cacheProvider->fetch($this->getHashKey($oldText, $newText));
  59. }
  60. /**
  61. * @param string $oldText
  62. * @param string $newText
  63. * @param string $data
  64. * @param int $lifeTime
  65. *
  66. * @return bool
  67. */
  68. public function save($oldText, $newText, $data, $lifeTime = 0)
  69. {
  70. return $this->cacheProvider->save($this->getHashKey($oldText, $newText), $data, $lifeTime);
  71. }
  72. /**
  73. * @param string $oldText
  74. * @param string $newText
  75. *
  76. * @return bool
  77. */
  78. public function delete($oldText, $newText)
  79. {
  80. return $this->cacheProvider->delete($this->getHashKey($oldText, $newText));
  81. }
  82. /**
  83. * @return array|null
  84. */
  85. public function getStats()
  86. {
  87. return $this->cacheProvider->getStats();
  88. }
  89. /**
  90. * @param string $oldText
  91. * @param string $newText
  92. *
  93. * @return string
  94. */
  95. protected function getHashKey($oldText, $newText)
  96. {
  97. return sprintf('%s_%s', md5($oldText), md5($newText));
  98. }
  99. }