APC.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  4. All rights reserved.
  5. Contact: Barry O'Donovan - barry (at) opensolutions (dot) ie
  6. http://www.opensolutions.ie/
  7. This file is part of the OSS_SNMP package.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. * Neither the name of Open Source Solutions Limited nor the
  16. names of its contributors may be used to endorse or promote products
  17. derived from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. namespace OSS\Cache;
  30. /**
  31. * APC cache implementation
  32. *
  33. * @copyright Copyright (c) 2012, Open Source Solutions Limited, Dublin, Ireland
  34. * @author Barry O'Donovan <barry@opensolutions.ie>
  35. */
  36. class APC extends \OSS\Cache
  37. {
  38. /**
  39. * Default time to live for cache variables in seconds
  40. * @var int Default time to live for cache variables in seconds (defaults to 300s - 5 mins)
  41. */
  42. protected $_ttl = 300;
  43. /**
  44. * Prefix to use for caching items
  45. * @var string Prefix to use for caching items
  46. */
  47. protected $_prefix = 'OSS_SNMP_';
  48. /**
  49. * Cache constructor.
  50. *
  51. * For basic cache, takes no parameters.
  52. *
  53. * @param int $ttl Set the default ttl
  54. * @param string $prefix Set the default prefix for caching variable names
  55. * @return \OSS\Cache\Basic An instance of the cache ($this) for fluent interfaces
  56. */
  57. public function __construct( $ttl = 300, $prefix = 'OSS_SNMP_' )
  58. {
  59. // do we have APC?
  60. if( !ini_get( 'apc.enabled' ) )
  61. throw new \OSS\Exception( 'APC is not installed or not enabled' );
  62. $this->_ttl = $ttl;
  63. $this->_prefix = $prefix;
  64. return $this;
  65. }
  66. /**
  67. * Load a named value from the cache (or null if not present)
  68. *
  69. * @param string $var The name of the value to load
  70. * @return mixed|null The value from the cache or null
  71. */
  72. public function load( $var )
  73. {
  74. $success = true;
  75. $val = apc_fetch( $this->_prefix . $var, $success );
  76. if( $success === false )
  77. return null;
  78. return $val;
  79. }
  80. /**
  81. * Save a named value to the cache
  82. *
  83. * @param string $var The name of the value to save
  84. * @param mixed $val The value to save
  85. * @return mixed The value (as passed)
  86. */
  87. public function save( $var, $val )
  88. {
  89. return $this->save( $var, $val, null );
  90. }
  91. /**
  92. * Save a named value to the cache
  93. *
  94. * @param string $var The name of the value to save
  95. * @param mixed $val The value to save
  96. * @param int $ttl The time to live of the variable if you want to override the default
  97. * @return mixed The value (as passed)
  98. */
  99. public function save( $var, $val, $ttl = null )
  100. {
  101. if( $ttl === null )
  102. $ttl = $this->_ttl;
  103. if( apc_store( $this->_prefix . $var, $val, $ttl ) )
  104. return $val;
  105. return null;
  106. }
  107. /**
  108. * Clear a named value from the cache
  109. *
  110. * @param string $var The name of the value to clear
  111. */
  112. public function clear( $var )
  113. {
  114. apc_delete( $this->_prefix . $var );
  115. }
  116. /**
  117. * Clear all values from the cache
  118. *
  119. */
  120. public function clearAll()
  121. {
  122. foreach ( new APCIterator( 'user', '/^' . $this->_prefix . '/') as $var )
  123. apc_delete( $var );
  124. }
  125. }