PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Illuminate/Cache/MemcachedStore.php

https://gitlab.com/pomirleanu.florentin/SMS-Client
PHP | 140 lines | 51 code | 16 blank | 73 comment | 2 complexity | 413a9d03099705736fdcb083e581ac3a MD5 | raw file
  1. <?php namespace Illuminate\Cache;
  2. use Illuminate\Contracts\Cache\Store;
  3. class MemcachedStore extends TaggableStore implements Store {
  4. /**
  5. * The Memcached instance.
  6. *
  7. * @var \Memcached
  8. */
  9. protected $memcached;
  10. /**
  11. * A string that should be prepended to keys.
  12. *
  13. * @var string
  14. */
  15. protected $prefix;
  16. /**
  17. * Create a new Memcached store.
  18. *
  19. * @param \Memcached $memcached
  20. * @param string $prefix
  21. * @return void
  22. */
  23. public function __construct($memcached, $prefix = '')
  24. {
  25. $this->memcached = $memcached;
  26. $this->prefix = strlen($prefix) > 0 ? $prefix.':' : '';
  27. }
  28. /**
  29. * Retrieve an item from the cache by key.
  30. *
  31. * @param string $key
  32. * @return mixed
  33. */
  34. public function get($key)
  35. {
  36. $value = $this->memcached->get($this->prefix.$key);
  37. if ($this->memcached->getResultCode() == 0)
  38. {
  39. return $value;
  40. }
  41. }
  42. /**
  43. * Store an item in the cache for a given number of minutes.
  44. *
  45. * @param string $key
  46. * @param mixed $value
  47. * @param int $minutes
  48. * @return void
  49. */
  50. public function put($key, $value, $minutes)
  51. {
  52. $this->memcached->set($this->prefix.$key, $value, $minutes * 60);
  53. }
  54. /**
  55. * Increment the value of an item in the cache.
  56. *
  57. * @param string $key
  58. * @param mixed $value
  59. * @return int|bool
  60. */
  61. public function increment($key, $value = 1)
  62. {
  63. return $this->memcached->increment($this->prefix.$key, $value);
  64. }
  65. /**
  66. * Decrement the value of an item in the cache.
  67. *
  68. * @param string $key
  69. * @param mixed $value
  70. * @return int|bool
  71. */
  72. public function decrement($key, $value = 1)
  73. {
  74. return $this->memcached->decrement($this->prefix.$key, $value);
  75. }
  76. /**
  77. * Store an item in the cache indefinitely.
  78. *
  79. * @param string $key
  80. * @param mixed $value
  81. * @return void
  82. */
  83. public function forever($key, $value)
  84. {
  85. return $this->put($key, $value, 0);
  86. }
  87. /**
  88. * Remove an item from the cache.
  89. *
  90. * @param string $key
  91. * @return bool
  92. */
  93. public function forget($key)
  94. {
  95. return $this->memcached->delete($this->prefix.$key);
  96. }
  97. /**
  98. * Remove all items from the cache.
  99. *
  100. * @return void
  101. */
  102. public function flush()
  103. {
  104. $this->memcached->flush();
  105. }
  106. /**
  107. * Get the underlying Memcached connection.
  108. *
  109. * @return \Memcached
  110. */
  111. public function getMemcached()
  112. {
  113. return $this->memcached;
  114. }
  115. /**
  116. * Get the cache key prefix.
  117. *
  118. * @return string
  119. */
  120. public function getPrefix()
  121. {
  122. return $this->prefix;
  123. }
  124. }