CacheLogPlugin.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2009, StatusNet, Inc.
  5. *
  6. * Logs cache access
  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 AGPL 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. * Log cache access
  37. *
  38. * Note that since most caching plugins return false for StartCache*
  39. * methods, you should add this plugin before them, i.e.
  40. *
  41. * addPlugin('CacheLog');
  42. * addPlugin('XCache');
  43. *
  44. * @category Cache
  45. * @package StatusNet
  46. * @author Evan Prodromou <evan@status.net>
  47. * @copyright 2009 StatusNet, Inc.
  48. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  49. * @link http://status.net/
  50. */
  51. class CacheLogPlugin extends Plugin
  52. {
  53. const PLUGIN_VERSION = '2.0.0';
  54. function onStartCacheGet(&$key, &$value)
  55. {
  56. $this->log(LOG_INFO, "Fetching key '$key'");
  57. return true;
  58. }
  59. function onEndCacheGet($key, &$value)
  60. {
  61. if ($value === false) {
  62. $this->log(LOG_INFO, sprintf('Cache MISS for key "%s"', $key));
  63. } else {
  64. $this->log(LOG_INFO, sprintf('Cache HIT for key "%s": %s', $key, self::showValue($value)));
  65. }
  66. return true;
  67. }
  68. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  69. {
  70. $this->log(LOG_INFO, "Begin setting cache value for key '$key'");
  71. return true;
  72. }
  73. function onEndCacheSet($key, $value, $flag, $expiry)
  74. {
  75. $this->log(LOG_INFO, sprintf('Set cache value %s for key "%s" (flags: %d, expiry %d)',
  76. self::showValue($value),
  77. $key,
  78. $flag,
  79. $expiry));
  80. return true;
  81. }
  82. function onStartCacheDelete(&$key, &$success)
  83. {
  84. $this->log(LOG_INFO, "Deleting cache value for key '$key'");
  85. return true;
  86. }
  87. function onEndCacheDelete($key)
  88. {
  89. $this->log(LOG_INFO, "Done deleting cache value for key '$key'");
  90. return true;
  91. }
  92. function onPluginVersion(array &$versions)
  93. {
  94. $versions[] = array('name' => 'CacheLog',
  95. 'version' => self::PLUGIN_VERSION,
  96. 'author' => 'Evan Prodromou',
  97. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/CacheLog',
  98. 'description' =>
  99. // TRANS: Plugin description.
  100. _m('Log reads and writes to the cache.'));
  101. return true;
  102. }
  103. static function showValue($value)
  104. {
  105. if (is_object($value)) {
  106. return sprintf('object of class %s', get_class($value));
  107. } else if (is_array($value)) {
  108. return sprintf('array of length %d', count($value));
  109. } else if (is_string($value)) {
  110. return sprintf('string "%s"', $value);
  111. } else if (is_integer($value)) {
  112. return sprintf('integer %d', $value);
  113. } else if (is_null($value)) {
  114. return 'null';
  115. } else {
  116. return 'unknown';
  117. }
  118. }
  119. }