ApcWrapper.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Illuminate\Cache;
  3. class ApcWrapper
  4. {
  5. /**
  6. * Indicates if APCu is supported.
  7. *
  8. * @var bool
  9. */
  10. protected $apcu = false;
  11. /**
  12. * Create a new APC wrapper instance.
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. $this->apcu = function_exists('apcu_fetch');
  19. }
  20. /**
  21. * Get an item from the cache.
  22. *
  23. * @param string $key
  24. * @return mixed
  25. */
  26. public function get($key)
  27. {
  28. return $this->apcu ? apcu_fetch($key) : apc_fetch($key);
  29. }
  30. /**
  31. * Store an item in the cache.
  32. *
  33. * @param string $key
  34. * @param mixed $value
  35. * @param int $seconds
  36. * @return array|bool
  37. */
  38. public function put($key, $value, $seconds)
  39. {
  40. return $this->apcu ? apcu_store($key, $value, $seconds) : apc_store($key, $value, $seconds);
  41. }
  42. /**
  43. * Increment the value of an item in the cache.
  44. *
  45. * @param string $key
  46. * @param mixed $value
  47. * @return int|bool
  48. */
  49. public function increment($key, $value)
  50. {
  51. return $this->apcu ? apcu_inc($key, $value) : apc_inc($key, $value);
  52. }
  53. /**
  54. * Decrement the value of an item in the cache.
  55. *
  56. * @param string $key
  57. * @param mixed $value
  58. * @return int|bool
  59. */
  60. public function decrement($key, $value)
  61. {
  62. return $this->apcu ? apcu_dec($key, $value) : apc_dec($key, $value);
  63. }
  64. /**
  65. * Remove an item from the cache.
  66. *
  67. * @param string $key
  68. * @return bool
  69. */
  70. public function delete($key)
  71. {
  72. return $this->apcu ? apcu_delete($key) : apc_delete($key);
  73. }
  74. /**
  75. * Remove all items from the cache.
  76. *
  77. * @return bool
  78. */
  79. public function flush()
  80. {
  81. return $this->apcu ? apcu_clear_cache() : apc_clear_cache('user');
  82. }
  83. }