APCPlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009, StatusNet, Inc.
  5. *
  6. * Plugin to implement cache interface for APC variable cache
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2009 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * A plugin to use APC's variable cache for the cache interface
  37. *
  38. * New plugin interface lets us use alternative cache systems
  39. * for caching. This one uses APC's variable cache.
  40. *
  41. * @category Cache
  42. * @package StatusNet
  43. * @author Evan Prodromou <evan@status.net>
  44. * @copyright 2009 StatusNet, Inc.
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class APCPlugin extends Plugin
  49. {
  50. /**
  51. * Get a value associated with a key
  52. *
  53. * The value should have been set previously.
  54. *
  55. * @param string &$key in; Lookup key
  56. * @param mixed &$value out; value associated with key
  57. *
  58. * @return boolean hook success
  59. */
  60. function onStartCacheGet(&$key, &$value)
  61. {
  62. $value = apc_fetch($key);
  63. Event::handle('EndCacheGet', array($key, &$value));
  64. return false;
  65. }
  66. /**
  67. * Associate a value with a key
  68. *
  69. * @param string &$key in; Key to use for lookups
  70. * @param mixed &$value in; Value to associate
  71. * @param integer &$flag in; Flag (passed through to Memcache)
  72. * @param integer &$expiry in; Expiry (passed through to Memcache)
  73. * @param boolean &$success out; Whether the set was successful
  74. *
  75. * @return boolean hook success
  76. */
  77. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  78. {
  79. $success = apc_store($key, $value, ((is_null($expiry)) ? 0 : $expiry));
  80. Event::handle('EndCacheSet', array($key, $value, $flag,
  81. $expiry));
  82. return false;
  83. }
  84. /**
  85. * Delete a value associated with a key
  86. *
  87. * @param string &$key in; Key to lookup
  88. * @param boolean &$success out; whether it worked
  89. *
  90. * @return boolean hook success
  91. */
  92. function onStartCacheDelete(&$key, &$success)
  93. {
  94. $success = apc_delete($key);
  95. Event::handle('EndCacheDelete', array($key));
  96. return false;
  97. }
  98. function onPluginVersion(&$versions)
  99. {
  100. $versions[] = array('name' => 'APC',
  101. 'version' => GNUSOCIAL_VERSION,
  102. 'author' => 'Evan Prodromou',
  103. 'homepage' => 'http://status.net/wiki/Plugin:APC',
  104. 'rawdescription' =>
  105. // TRANS: Plugin description.
  106. _m('Use the <a href="http://pecl.php.net/package/apc">APC</a> variable cache to cache query results.'));
  107. return true;
  108. }
  109. }