MemcachedProfilerStorage.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Profiler;
  11. use Memcached;
  12. /**
  13. * Memcached Profiler Storage
  14. *
  15. * @author Andrej Hudec <pulzarraider@gmail.com>
  16. */
  17. class MemcachedProfilerStorage extends BaseMemcacheProfilerStorage
  18. {
  19. /**
  20. * @var Memcached
  21. */
  22. private $memcached;
  23. /**
  24. * Internal convenience method that returns the instance of the Memcached
  25. *
  26. * @return Memcached
  27. *
  28. * @throws \RuntimeException
  29. */
  30. protected function getMemcached()
  31. {
  32. if (null === $this->memcached) {
  33. if (!preg_match('#^memcached://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
  34. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcached with an invalid dsn "%s". The expected format is "memcached://[host]:port".', $this->dsn));
  35. }
  36. $host = $matches[1] ?: $matches[2];
  37. $port = $matches[3];
  38. $memcached = new Memcached;
  39. //disable compression to allow appending
  40. $memcached->setOption(Memcached::OPT_COMPRESSION, false);
  41. $memcached->addServer($host, $port);
  42. $this->memcached = $memcached;
  43. }
  44. return $this->memcached;
  45. }
  46. /**
  47. * Set instance of the Memcached
  48. *
  49. * @param Memcached $memcached
  50. */
  51. public function setMemcached($memcached)
  52. {
  53. $this->memcached = $memcached;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function getValue($key)
  59. {
  60. return $this->getMemcached()->get($key);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function setValue($key, $value, $expiration = 0)
  66. {
  67. return $this->getMemcached()->set($key, $value, time() + $expiration);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function delete($key)
  73. {
  74. return $this->getMemcached()->delete($key);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function appendValue($key, $value, $expiration = 0)
  80. {
  81. $memcached = $this->getMemcached();
  82. if (!$result = $memcached->append($key, $value)) {
  83. return $memcached->set($key, $value, $expiration);
  84. }
  85. return $result;
  86. }
  87. }