Drupal investigation

MemcacheProfilerStorage.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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__.'\MemcacheProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  12. /**
  13. * Memcache 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 MemcacheProfilerStorage extends BaseMemcacheProfilerStorage
  21. {
  22. /**
  23. * @var \Memcache
  24. */
  25. private $memcache;
  26. /**
  27. * Internal convenience method that returns the instance of the Memcache.
  28. *
  29. * @return \Memcache
  30. *
  31. * @throws \RuntimeException
  32. */
  33. protected function getMemcache()
  34. {
  35. if (null === $this->memcache) {
  36. if (!preg_match('#^memcache://(?(?=\[.*\])\[(.*)\]|(.*)):(.*)$#', $this->dsn, $matches)) {
  37. throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcache with an invalid dsn "%s". The expected format is "memcache://[host]:port".', $this->dsn));
  38. }
  39. $host = $matches[1] ?: $matches[2];
  40. $port = $matches[3];
  41. $memcache = new \Memcache();
  42. $memcache->addServer($host, $port);
  43. $this->memcache = $memcache;
  44. }
  45. return $this->memcache;
  46. }
  47. /**
  48. * Set instance of the Memcache.
  49. *
  50. * @param \Memcache $memcache
  51. */
  52. public function setMemcache($memcache)
  53. {
  54. $this->memcache = $memcache;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function getValue($key)
  60. {
  61. return $this->getMemcache()->get($key);
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function setValue($key, $value, $expiration = 0)
  67. {
  68. return $this->getMemcache()->set($key, $value, false, time() + $expiration);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function delete($key)
  74. {
  75. return $this->getMemcache()->delete($key);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function appendValue($key, $value, $expiration = 0)
  81. {
  82. $memcache = $this->getMemcache();
  83. if (method_exists($memcache, 'append')) {
  84. // Memcache v3.0
  85. if (!$result = $memcache->append($key, $value, false, $expiration)) {
  86. return $memcache->set($key, $value, false, $expiration);
  87. }
  88. return $result;
  89. }
  90. // simulate append in Memcache <3.0
  91. $content = $memcache->get($key);
  92. return $memcache->set($key, $content.$value, false, $expiration);
  93. }
  94. }