]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/CacheLog/CacheLogPlugin.php
Merge branch 'nightly' of git.gnu.io:gnu/gnu-social into mmn_fixes
[quix0rs-gnu-social.git] / plugins / CacheLog / CacheLogPlugin.php
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
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Log cache access
39  *
40  * Note that since most caching plugins return false for StartCache*
41  * methods, you should add this plugin before them, i.e.
42  *
43  *     addPlugin('CacheLog');
44  *     addPlugin('XCache');
45  *
46  * @category  Cache
47  * @package   StatusNet
48  * @author    Evan Prodromou <evan@status.net>
49  * @copyright 2009 StatusNet, Inc.
50  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
51  * @link      http://status.net/
52  */
53 class CacheLogPlugin extends Plugin
54 {
55     function onStartCacheGet(&$key, &$value)
56     {
57         $this->log(LOG_INFO, "Fetching key '$key'");
58         return true;
59     }
60
61     function onEndCacheGet($key, &$value)
62     {
63         if ($value === false) {
64             $this->log(LOG_INFO, sprintf('Cache MISS for key "%s"', $key));
65         } else {
66             $this->log(LOG_INFO, sprintf('Cache HIT for key "%s": %s', $key, self::showValue($value)));
67         }
68         return true;
69     }
70
71     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
72     {
73         $this->log(LOG_INFO, "Begin setting cache value for key '$key'");
74         return true;
75     }
76
77     function onEndCacheSet($key, $value, $flag, $expiry)
78     {
79         $this->log(LOG_INFO, sprintf('Set cache value %s for key "%s" (flags: %d, expiry %d)',
80                                      self::showValue($value),
81                                      $key,
82                                      $flag,
83                                      $expiry));
84         return true;
85     }
86
87     function onStartCacheDelete(&$key, &$success)
88     {
89         $this->log(LOG_INFO, "Deleting cache value for key '$key'");
90         return true;
91     }
92
93     function onEndCacheDelete($key)
94     {
95         $this->log(LOG_INFO, "Done deleting cache value for key '$key'");
96         return true;
97     }
98
99     function onPluginVersion(array &$versions)
100     {
101         $versions[] = array('name' => 'CacheLog',
102                             'version' => GNUSOCIAL_VERSION,
103                             'author' => 'Evan Prodromou',
104                             'homepage' => 'http://status.net/wiki/Plugin:CacheLog',
105                             'description' =>
106                             // TRANS: Plugin description.
107                             _m('Log reads and writes to the cache.'));
108         return true;
109     }
110
111     static function showValue($value)
112     {
113         if (is_object($value)) {
114             return sprintf('object of class %s', get_class($value));
115         } else if (is_array($value)) {
116             return sprintf('array of length %d', count($value));
117         } else if (is_string($value)) {
118             return sprintf('string "%s"', $value);
119         } else if (is_integer($value)) {
120             return sprintf('integer %d', $value);
121         } else if (is_null($value)) {
122             return 'null';
123         } else {
124             return 'unknown';
125         }
126     }
127 }