Exemplo n.º 1
0
 function blowCache()
 {
     $cache = common_memcache();
     if ($cache) {
         $cache->delete(common_cache_key('notice_tag:notice_stream:' . $this->tag));
     }
 }
Exemplo n.º 2
0
 function handle($args)
 {
     parent::handle($args);
     $c = common_memcache();
     if (!empty($c)) {
         $cacheKey = common_cache_key(MinifyPlugin::cacheKey . ':' . $this->file . '?v=' . empty($this->v) ? '' : $this->v);
         $out = $c->get($cacheKey);
     }
     if (empty($out)) {
         $out = $this->minify($this->file);
     }
     if (!empty($c)) {
         $c->set($cacheKey, $out);
     }
     $sec = session_cache_expire() * 60;
     header('Cache-Control: public, max-age=' . $sec);
     header('Pragma: public');
     $this->raw($out);
 }
Exemplo n.º 3
0
 /**
  * etag for file
  *
  * This returns the same data (inode, size, mtime) as Apache would,
  * but in decimal instead of hex.
  *
  * @return string etag http header
  */
 function etag()
 {
     if (common_config('site', 'use_x_sendfile')) {
         return null;
     }
     $cache = common_memcache();
     if ($cache) {
         $key = common_cache_key('attachments:etag:' . $this->path);
         $etag = $cache->get($key);
         if ($etag === false) {
             $etag = crc32(file_get_contents($this->path));
             $cache->set($key, $etag);
         }
         return $etag;
     }
     $stat = stat($this->path);
     return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
 }
Exemplo n.º 4
0
 static function cacheSet($keyPart, $value)
 {
     $c = self::memcache();
     if (empty($c)) {
         return false;
     }
     $cacheKey = common_cache_key($keyPart);
     return $c->set($cacheKey, $value);
 }
Exemplo n.º 5
0
$start_at = $argc > 1 ? $argv[1] : null;
common_log(LOG_INFO, 'Updating user inboxes.');
$user = new User();
if ($start_at) {
    $user->whereAdd('id >= ' . $start_at);
}
$cnt = $user->find();
$cache = common_memcache();
while ($user->fetch()) {
    common_log(LOG_INFO, 'Updating inbox for user ' . $user->id);
    $user->query('BEGIN');
    $inbox = new Notice_inbox();
    $result = $inbox->query('INSERT LOW_PRIORITY INTO notice_inbox (user_id, notice_id, created) ' . 'SELECT ' . $user->id . ', notice.id, notice.created ' . 'FROM subscription JOIN notice ON subscription.subscribed = notice.profile_id ' . 'WHERE subscription.subscriber = ' . $user->id . ' ' . 'AND notice.created >= subscription.created ' . 'AND NOT EXISTS (SELECT user_id, notice_id ' . 'FROM notice_inbox ' . 'WHERE user_id = ' . $user->id . ' ' . 'AND notice_id = notice.id)');
    if (is_null($result) || $result === false) {
        common_log_db_error($inbox, 'INSERT', __FILE__);
        continue;
    }
    $orig = clone $user;
    $user->inboxed = 1;
    $result = $user->update($orig);
    if (!$result) {
        common_log_db_error($user, 'UPDATE', __FILE__);
        continue;
    }
    $user->query('COMMIT');
    $inbox->free();
    unset($inbox);
    if ($cache) {
        $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
    }
}
Exemplo n.º 6
0
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$shortoptions = "t:c:v:k:";
$helptext = <<<ENDOFHELP
USAGE: showcache.php <args>
shows the cached object based on the args

  -t table     Table to look up
  -c column    Column to look up, default "id"
  -v value     Value to look up
  -k key       Key to look up; other args are ignored

ENDOFHELP;
require_once INSTALLDIR . '/scripts/commandline.inc';
$karg = get_option_value('k');
if (!empty($karg)) {
    $k = common_cache_key($karg);
} else {
    $table = get_option_value('t');
    if (empty($table)) {
        die("No table or key specified\n");
    }
    $column = get_option_value('c');
    if (empty($column)) {
        $column = 'id';
    }
    $value = get_option_value('v');
    $k = Memcached_DataObject::cacheKey($table, $column, $value);
}
print "Checking key '{$k}'...\n";
$c = common_memcache();
if (empty($c)) {
Exemplo n.º 7
0
 static function cachedQuery($cls, $qry, $expiry = 3600)
 {
     $c = Memcached_DataObject::memcache();
     if (!$c) {
         $inst = new $cls();
         $inst->query($qry);
         return $inst;
     }
     $key_part = common_keyize($cls) . ':' . md5($qry);
     $ckey = common_cache_key($key_part);
     $stored = $c->get($ckey);
     if ($stored) {
         return new ArrayWrapper($stored);
     }
     $inst = new $cls();
     $inst->query($qry);
     $cached = array();
     while ($inst->fetch()) {
         $cached[] = clone $inst;
     }
     $inst->free();
     $c->set($ckey, $cached, MEMCACHE_COMPRESSED, $expiry);
     return new ArrayWrapper($cached);
 }
Exemplo n.º 8
0
 function _blowSettingsCache()
 {
     $c = self::memcache();
     if (!empty($c)) {
         $c->delete(common_cache_key(self::settingsKey));
     }
 }
Exemplo n.º 9
0
 static function getCachedStream($qry, $cachekey, $offset, $limit, $order)
 {
     # If outside our cache window, just go to the DB
     if ($offset + $limit > NOTICE_CACHE_WINDOW) {
         return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
     }
     # Get the cache; if we can't, just go to the DB
     $cache = common_memcache();
     if (!$cache) {
         return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
     }
     # Get the notices out of the cache
     $notices = $cache->get(common_cache_key($cachekey));
     # On a cache hit, return a DB-object-like wrapper
     if ($notices !== false) {
         $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
         return $wrapper;
     }
     # If the cache was invalidated because of new data being
     # added, we can try and just get the new stuff. We keep an additional
     # copy of the data at the key + ';last'
     # No cache hit. Try to get the *last* cached version
     $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
     if ($last_notices) {
         # Reverse-chron order, so last ID is last.
         $last_id = $last_notices[0]->id;
         # XXX: this assumes monotonically increasing IDs; a fair
         # bet with our DB.
         $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, $last_id, null, $order, null);
         if ($new_notice) {
             $new_notices = array();
             while ($new_notice->fetch()) {
                 $new_notices[] = clone $new_notice;
             }
             $new_notice->free();
             $notices = array_slice(array_merge($new_notices, $last_notices), 0, NOTICE_CACHE_WINDOW);
             # Store the array in the cache for next time
             $result = $cache->set(common_cache_key($cachekey), $notices);
             $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
             # return a wrapper of the array for use now
             return new ArrayWrapper(array_slice($notices, $offset, $limit));
         }
     }
     # Otherwise, get the full cache window out of the DB
     $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
     # If there are no hits, just return the value
     if (!$notice) {
         return $notice;
     }
     # Pack results into an array
     $notices = array();
     while ($notice->fetch()) {
         $notices[] = clone $notice;
     }
     $notice->free();
     # Store the array in the cache for next time
     $result = $cache->set(common_cache_key($cachekey), $notices);
     $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
     # return a wrapper of the array for use now
     $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
     return $wrapper;
 }
Exemplo n.º 10
0
 function blowNoticeCount()
 {
     $c = common_memcache();
     if (!empty($c)) {
         $c->delete(common_cache_key('profile:notice_count:' . $this->id));
     }
 }
Exemplo n.º 11
0
 function blowFavesCache()
 {
     $cache = common_memcache();
     if ($cache) {
         // Faves don't happen chronologically, so we need to blow
         // ;last cache, too
         $cache->delete(common_cache_key('fave:ids_by_user:'******'fave:ids_by_user:'******';last'));
         $cache->delete(common_cache_key('fave:ids_by_user_own:' . $this->id));
         $cache->delete(common_cache_key('fave:ids_by_user_own:' . $this->id . ';last'));
     }
     $profile = $this->getProfile();
     $profile->blowFaveCount();
 }
Exemplo n.º 12
0
 function get_ldap_connection($config = null)
 {
     if ($config == null) {
         $config = $this->ldap_config;
     }
     $config_id = crc32(serialize($config));
     if (array_key_exists($config_id, self::$ldap_connections)) {
         $ldap = self::$ldap_connections[$config_id];
     } else {
         //cannot use Net_LDAP2::connect() as StatusNet uses
         //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
         //PEAR handling can be overridden on instance objects, so we do that.
         $ldap = new Net_LDAP2($config);
         $ldap->setErrorHandling(PEAR_ERROR_RETURN);
         $err = $ldap->bind();
         if (Net_LDAP2::isError($err)) {
             // if we were called with a config, assume caller will handle
             // incorrect username/password (LDAP_INVALID_CREDENTIALS)
             if (isset($config) && $err->getCode() == 0x31) {
                 throw new LdapInvalidCredentialsException('Could not connect to LDAP server: ' . $err->getMessage());
             }
             throw new Exception('Could not connect to LDAP server: ' . $err->getMessage());
         }
         $c = common_memcache();
         if (!empty($c)) {
             $cacheObj = new MemcacheSchemaCache(array('c' => $c, 'cacheKey' => common_cache_key('ldap_schema:' . $config_id)));
             $ldap->registerSchemaCache($cacheObj);
         }
         self::$ldap_connections[$config_id] = $ldap;
     }
     return $ldap;
 }
Exemplo n.º 13
0
 function repeatStream($limit = 100)
 {
     $cache = common_memcache();
     if (empty($cache)) {
         $ids = $this->_repeatStreamDirect($limit);
     } else {
         $idstr = $cache->get(common_cache_key('notice:repeats:' . $this->id));
         if ($idstr !== false) {
             $ids = explode(',', $idstr);
         } else {
             $ids = $this->_repeatStreamDirect(100);
             $cache->set(common_cache_key('notice:repeats:' . $this->id), implode(',', $ids));
         }
         if ($limit < 100) {
             // We do a max of 100, so slice down to limit
             $ids = array_slice($ids, 0, $limit);
         }
     }
     return Notice::getStreamByIds($ids);
 }
Exemplo n.º 14
0
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$helptext = <<<ENDOFHELP
uncache_users.php <idfile>

Uncache users listed in an ID file, default 'ids.txt'.

ENDOFHELP;
require_once INSTALLDIR . '/scripts/commandline.inc';
$id_file = count($args) > 1 ? $args[0] : 'ids.txt';
common_log(LOG_INFO, 'Updating user inboxes.');
$ids = file($id_file);
$memc = common_memcache();
foreach ($ids as $id) {
    $user = User::staticGet('id', $id);
    if (!$user) {
        common_log(LOG_WARNING, 'No such user: '******'user:notices_with_friends:' . $user->id));
    $memc->delete(common_cache_key('user:notices_with_friends:' . $user->id . ';last'));
}
Exemplo n.º 15
0
 function cacheKey($attrs)
 {
     $key = 'geonames:' . implode(',', array_keys($attrs)) . ':' . common_keyize(implode(',', array_values($attrs)));
     if ($this->cachePrefix) {
         return $this->cachePrefix . ':' . $key;
     } else {
         return common_cache_key($key);
     }
 }
Exemplo n.º 16
0
 function onStartStyleElement($action, &$code, &$type, &$media)
 {
     if ($this->minifyInlineCss && $type == 'text/css') {
         $c = common_memcache();
         if (!empty($c)) {
             $cacheKey = common_cache_key(self::cacheKey . ':' . crc32($code));
             $out = $c->get($cacheKey);
         }
         if (empty($out)) {
             $out = $this->minifyCss($code);
         }
         if (!empty($c)) {
             $c->set($cacheKey, $out);
         }
         if (!empty($out)) {
             $code = $out;
         }
     }
 }
Exemplo n.º 17
0
function subs_unsubscribe_to($user, $other)
{
    if (!$user->isSubscribed($other)) {
        return _('Not subscribed!.');
    }
    $sub = DB_DataObject::factory('subscription');
    $sub->subscriber = $user->id;
    $sub->subscribed = $other->id;
    $sub->find(true);
    // note we checked for existence above
    if (!$sub->delete()) {
        return _('Couldn\'t delete subscription.');
    }
    $cache = common_memcache();
    if ($cache) {
        $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
    }
    return true;
}
Exemplo n.º 18
0
 static function cacheSet($keyPart, $value, $flag = null, $expiry = null)
 {
     $c = self::memcache();
     if (empty($c)) {
         return false;
     }
     $cacheKey = common_cache_key($keyPart);
     return $c->set($cacheKey, $value, $flag, $expiry);
 }
Exemplo n.º 19
0
 function blowFavesCache()
 {
     $cache = common_memcache();
     if ($cache) {
         # Faves don't happen chronologically, so we need to blow
         # ;last cache, too
         $cache->delete(common_cache_key('user:faves:' . $this->id));
         $cache->delete(common_cache_key('user:faves:' . $this->id) . ';last');
     }
 }