ApcWrapper.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $fetchedValue = $this->apcu ? apcu_fetch($key, $success) : apc_fetch($key, $success);
  29. return $success ? $fetchedValue : null;
  30. }
  31. /**
  32. * Store an item in the cache.
  33. *
  34. * @param string $key
  35. * @param mixed $value
  36. * @param int $seconds
  37. * @return array|bool
  38. */
  39. public function put($key, $value, $seconds)
  40. {
  41. return $this->apcu ? apcu_store($key, $value, $seconds) : apc_store($key, $value, $seconds);
  42. }
  43. /**
  44. * Increment the value of an item in the cache.
  45. *
  46. * @param string $key
  47. * @param mixed $value
  48. * @return int|bool
  49. */
  50. public function increment($key, $value)
  51. {
  52. return $this->apcu ? apcu_inc($key, $value) : apc_inc($key, $value);
  53. }
  54. /**
  55. * Decrement 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 decrement($key, $value)
  62. {
  63. return $this->apcu ? apcu_dec($key, $value) : apc_dec($key, $value);
  64. }
  65. /**
  66. * Remove an item from the cache.
  67. *
  68. * @param string $key
  69. * @return bool
  70. */
  71. public function delete($key)
  72. {
  73. return $this->apcu ? apcu_delete($key) : apc_delete($key);
  74. }
  75. /**
  76. * Remove all items from the cache.
  77. *
  78. * @return bool
  79. */
  80. public function flush()
  81. {
  82. return $this->apcu ? apcu_clear_cache() : apc_clear_cache('user');
  83. }
  84. }