apc.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php namespace Laravel\Cache;
  2. /**
  3. * Wrap the APC functions in a class that can be injected into driver.
  4. * Since the APC functions are global, the driver is untestable without
  5. * injecting a wrapper around them.
  6. */
  7. class APC_Engine {
  8. /**
  9. * Get an item from the APC cache.
  10. *
  11. * @param string $key
  12. * @return mixed
  13. */
  14. public function get($key)
  15. {
  16. return apc_fetch($key);
  17. }
  18. /**
  19. * Store an item in the APC cache.
  20. *
  21. * @param string $key
  22. * @param mixed $value
  23. * @param int $minutes
  24. * @return void
  25. */
  26. public function put($key, $value, $seconds)
  27. {
  28. apc_store($key, $value, $seconds);
  29. }
  30. /**
  31. * Delete an item from the APC cache.
  32. *
  33. * @param string $key
  34. * @return void
  35. */
  36. public function forget($key)
  37. {
  38. apc_delete($key);
  39. }
  40. }
  41. class APC extends Driver {
  42. /**
  43. * The APC Engine instance.
  44. *
  45. * @var APC_Engine
  46. */
  47. private $apc;
  48. /**
  49. * The cache key from the cache configuration file.
  50. *
  51. * @var string
  52. */
  53. private $key;
  54. /**
  55. * Create a new APC cache driver instance.
  56. *
  57. * @param APC_Engine $apc
  58. * @return void
  59. */
  60. public function __construct(APC_Engine $apc, $key)
  61. {
  62. $this->apc = $apc;
  63. $this->key = $key;
  64. }
  65. /**
  66. * Determine if an item exists in the cache.
  67. *
  68. * @param string $key
  69. * @return bool
  70. */
  71. public function has($key)
  72. {
  73. return ( ! is_null($this->get($key)));
  74. }
  75. /**
  76. * Retrieve an item from the cache driver.
  77. *
  78. * @param string $key
  79. * @return mixed
  80. */
  81. protected function retrieve($key)
  82. {
  83. return ( ! is_null($cache = $this->apc->get($this->key.$key))) ? $cache : null;
  84. }
  85. /**
  86. * Write an item to the cache for a given number of minutes.
  87. *
  88. * @param string $key
  89. * @param mixed $value
  90. * @param int $minutes
  91. * @return void
  92. */
  93. public function put($key, $value, $minutes)
  94. {
  95. $this->apc->put($this->key.$key, $value, $minutes * 60);
  96. }
  97. /**
  98. * Delete an item from the cache.
  99. *
  100. * @param string $key
  101. * @return void
  102. */
  103. public function forget($key)
  104. {
  105. $this->apc->forget($this->key.$key);
  106. }
  107. }