Drupal investigation

MemcachedProfilerStorage.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. @trigger_error('The '.__NAMESPACE__.'\MemcachedProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * Memcached Profiler Storage.
  14. *
  15. * @author Andrej Hudec <pulzarraider@gmail.com>
  16. *
  17. * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  18. * Use {@link FileProfilerStorage} instead.
  19. */
  20. class MemcachedProfilerStorage extends BaseMemcacheProfilerStorage
  21. {
  22. /**
  23. * @var \Memcached
  24. */
  25. private $memcached;
  26. /**
  27. * Internal convenience method that returns the instance of the Memcached.
  28. *
  29. * @return \Memcached
  30. *
  31. * @throws \RuntimeException
  32. */
  33. protected function getMemcached()
  34. {
  35. if (null === $this->memcached) {
  36. if (!preg_match('#^memcached://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
  37. 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));
  38. }
  39. $host = $matches[1] ?: $matches[2];
  40. $port = $matches[3];
  41. $memcached = new \Memcached();
  42. // disable compression to allow appending
  43. $memcached->setOption(\Memcached::OPT_COMPRESSION, false);
  44. $memcached->addServer($host, $port);
  45. $this->memcached = $memcached;
  46. }
  47. return $this->memcached;
  48. }
  49. /**
  50. * Set instance of the Memcached.
  51. *
  52. * @param \Memcached $memcached
  53. */
  54. public function setMemcached($memcached)
  55. {
  56. $this->memcached = $memcached;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function getValue($key)
  62. {
  63. return $this->getMemcached()->get($key);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. protected function setValue($key, $value, $expiration = 0)
  69. {
  70. return $this->getMemcached()->set($key, $value, time() + $expiration);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. protected function delete($key)
  76. {
  77. return $this->getMemcached()->delete($key);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function appendValue($key, $value, $expiration = 0)
  83. {
  84. $memcached = $this->getMemcached();
  85. if (!$result = $memcached->append($key, $value)) {
  86. return $memcached->set($key, $value, $expiration);
  87. }
  88. return $result;
  89. }
  90. }