123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- class Cache
- {
-
- var $_items = array();
- var $_inlineCache = true;
- static $_inst = null;
- const COMPRESSED = 1;
- private function __construct() {
-
-
-
- $this->_inlineCache = (php_sapi_name() != 'cli');
- }
-
- static function instance()
- {
- if (is_null(self::$_inst)) {
- self::$_inst = new Cache();
- }
- return self::$_inst;
- }
-
- static function key($extra)
- {
- $base_key = common_config('cache', 'base');
- if (empty($base_key)) {
- $base_key = self::keyize(common_config('site', 'name'));
- }
- return 'gnusocial:' . $base_key . ':' . $extra;
- }
-
-
- static function codeKey($extra)
- {
- static $prefix = null;
-
- if (empty($prefix)) {
-
- $names = array();
-
- foreach (StatusNet::getActivePlugins() as $plugin=>$attrs) {
- $names[] = $plugin;
- }
-
- asort($names);
-
-
-
- $uniq = crc32(implode(',', $names));
- $build = common_config('site', 'build');
- $prefix = GNUSOCIAL_VERSION.':'.$build.':'.$uniq;
- }
-
- return Cache::key($prefix.':'.$extra);
- }
-
-
- static function keyize($str)
- {
- $str = strtolower($str);
- $str = preg_replace('/\s/', '_', $str);
- return $str;
- }
-
- function get($key)
- {
- $value = false;
- common_perf_counter('Cache::get', $key);
- if (Event::handle('StartCacheGet', array(&$key, &$value))) {
- if ($this->_inlineCache && array_key_exists($key, $this->_items)) {
- $value = unserialize($this->_items[$key]);
- }
- Event::handle('EndCacheGet', array($key, &$value));
- }
- return $value;
- }
-
- function set($key, $value, $flag=null, $expiry=null)
- {
- $success = false;
- common_perf_counter('Cache::set', $key);
- if (Event::handle('StartCacheSet', array(&$key, &$value, &$flag,
- &$expiry, &$success))) {
- if ($this->_inlineCache) {
- $this->_items[$key] = serialize($value);
- }
- $success = true;
- Event::handle('EndCacheSet', array($key, $value, $flag,
- $expiry));
- }
- return $success;
- }
-
- function increment($key, $step=1)
- {
- $value = false;
- common_perf_counter('Cache::increment', $key);
- if (Event::handle('StartCacheIncrement', array(&$key, &$step, &$value))) {
-
-
- $value = $this->get($key);
- if ($value !== false) {
- $value += $step;
- $ok = $this->set($key, $value);
- $got = $this->get($key);
- }
- Event::handle('EndCacheIncrement', array($key, $step, $value));
- }
- return $value;
- }
-
- function delete($key)
- {
- $success = false;
- common_perf_counter('Cache::delete', $key);
- if (Event::handle('StartCacheDelete', array(&$key, &$success))) {
- if ($this->_inlineCache && array_key_exists($key, $this->_items)) {
- unset($this->_items[$key]);
- }
- $success = true;
- Event::handle('EndCacheDelete', array($key));
- }
- return $success;
- }
-
- function reconnect()
- {
- $success = false;
- if (Event::handle('StartCacheReconnect', array(&$success))) {
- $success = true;
- Event::handle('EndCacheReconnect', array());
- }
- return $success;
- }
- }
|