Apc.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Va7JiZHlmFiglbjcc81nl7704NvYqgtf8oGWFahcHME=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.02]
  11. */
  12. namespace Cube\Cache\Adapter;
  13. class Apc extends AbstractAdapter
  14. {
  15. /**
  16. *
  17. * class constructor
  18. *
  19. * @param array $options configuration array
  20. *
  21. * @throws \RuntimeException
  22. */
  23. public function __construct($options = array())
  24. {
  25. parent::__construct($options);
  26. if (!self::enabled()) {
  27. throw new \RuntimeException("APC cache module is not available.");
  28. }
  29. }
  30. /**
  31. *
  32. * check if module is available and enabled
  33. *
  34. * @return bool
  35. */
  36. public static function enabled()
  37. {
  38. return (extension_loaded('apc') && ini_get('apc.enabled')) ? true : false;
  39. }
  40. /**
  41. *
  42. * reads the contents of a cache file and returns the output or false if the file could not be found
  43. *
  44. * @param string $name
  45. * @param string $type
  46. *
  47. * @return string|false
  48. */
  49. public function read($name, $type)
  50. {
  51. $contents = apc_fetch($type . $name);
  52. if ($contents !== false) {
  53. return ($this->_serialization === true) ? unserialize($contents) : $contents;
  54. }
  55. return false;
  56. }
  57. /**
  58. *
  59. * create/update cache file
  60. *
  61. * @param string $name
  62. * @param string $type
  63. * @param mixed $data
  64. * @param int $expires
  65. *
  66. * @return $this
  67. * @throws \RuntimeException
  68. */
  69. public function write($name, $type, $data, $expires = null)
  70. {
  71. if ($this->_serialization === true) {
  72. $data = serialize($data);
  73. }
  74. if ($expires === null) {
  75. $expires = $this->getExpires();
  76. }
  77. $result = apc_store($type . $name, $data, $expires);
  78. if (!$result) {
  79. throw new \RuntimeException(
  80. sprintf("APC store failure - '%s'.", $name));
  81. }
  82. return $this;
  83. }
  84. /**
  85. *
  86. * delete a variable from cache
  87. *
  88. * @param string $name
  89. * @param string $type
  90. *
  91. * @return boolean
  92. */
  93. public function delete($name, $type)
  94. {
  95. return apc_delete($type . $name);
  96. }
  97. /**
  98. *
  99. * purge cache
  100. * - for APC cache variables expire automatically so this method does nothing
  101. *
  102. * @param string $type
  103. * @param boolean $force
  104. *
  105. * @return $this
  106. */
  107. public function purge($type, $force = false)
  108. {
  109. return $this;
  110. }
  111. /**
  112. *
  113. * clear cache
  114. *
  115. * @return $this
  116. */
  117. public function clear()
  118. {
  119. apc_clear_cache();
  120. return $this;
  121. }
  122. }