ApcStore.php 2.6KB

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