CacheLogPlugin.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. function onStartCacheGet(&$key, &$value)
  54. {
  55. $this->log(LOG_INFO, "Fetching key '$key'");
  56. return true;
  57. }
  58. function onEndCacheGet($key, &$value)
  59. {
  60. if ($value === false) {
  61. $this->log(LOG_INFO, sprintf('Cache MISS for key "%s"', $key));
  62. } else {
  63. $this->log(LOG_INFO, sprintf('Cache HIT for key "%s": %s', $key, self::showValue($value)));
  64. }
  65. return true;
  66. }
  67. function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
  68. {
  69. $this->log(LOG_INFO, "Begin setting cache value for key '$key'");
  70. return true;
  71. }
  72. function onEndCacheSet($key, $value, $flag, $expiry)
  73. {
  74. $this->log(LOG_INFO, sprintf('Set cache value %s for key "%s" (flags: %d, expiry %d)',
  75. self::showValue($value),
  76. $key,
  77. $flag,
  78. $expiry));
  79. return true;
  80. }
  81. function onStartCacheDelete(&$key, &$success)
  82. {
  83. $this->log(LOG_INFO, "Deleting cache value for key '$key'");
  84. return true;
  85. }
  86. function onEndCacheDelete($key)
  87. {
  88. $this->log(LOG_INFO, "Done deleting cache value for key '$key'");
  89. return true;
  90. }
  91. function onPluginVersion(array &$versions)
  92. {
  93. $versions[] = array('name' => 'CacheLog',
  94. 'version' => GNUSOCIAL_VERSION,
  95. 'author' => 'Evan Prodromou',
  96. 'homepage' => 'http://status.net/wiki/Plugin:CacheLog',
  97. 'description' =>
  98. // TRANS: Plugin description.
  99. _m('Log reads and writes to the cache.'));
  100. return true;
  101. }
  102. static function showValue($value)
  103. {
  104. if (is_object($value)) {
  105. return sprintf('object of class %s', get_class($value));
  106. } else if (is_array($value)) {
  107. return sprintf('array of length %d', count($value));
  108. } else if (is_string($value)) {
  109. return sprintf('string "%s"', $value);
  110. } else if (is_integer($value)) {
  111. return sprintf('integer %d', $value);
  112. } else if (is_null($value)) {
  113. return 'null';
  114. } else {
  115. return 'unknown';
  116. }
  117. }
  118. }