Path: blob/master/src/applications/people/storage/PhabricatorUserCache.php
12256 views
<?php12final class PhabricatorUserCache extends PhabricatorUserDAO {34protected $userPHID;5protected $cacheIndex;6protected $cacheKey;7protected $cacheData;8protected $cacheType;910protected function getConfiguration() {11return array(12self::CONFIG_TIMESTAMPS => false,13self::CONFIG_COLUMN_SCHEMA => array(14'cacheIndex' => 'bytes12',15'cacheKey' => 'text255',16'cacheData' => 'text',17'cacheType' => 'text32',18),19self::CONFIG_KEY_SCHEMA => array(20'key_usercache' => array(21'columns' => array('userPHID', 'cacheIndex'),22'unique' => true,23),24'key_cachekey' => array(25'columns' => array('cacheIndex'),26),27'key_cachetype' => array(28'columns' => array('cacheType'),29),30),31) + parent::getConfiguration();32}3334public function save() {35$this->cacheIndex = Filesystem::digestForIndex($this->getCacheKey());36return parent::save();37}3839public static function writeCache(40PhabricatorUserCacheType $type,41$key,42$user_phid,43$raw_value) {44self::writeCaches(45array(46array(47'type' => $type,48'key' => $key,49'userPHID' => $user_phid,50'value' => $raw_value,51),52));53}5455public static function writeCaches(array $values) {56if (PhabricatorEnv::isReadOnly()) {57return;58}5960if (!$values) {61return;62}6364$table = new self();65$conn_w = $table->establishConnection('w');6667$sql = array();68foreach ($values as $value) {69$key = $value['key'];7071$sql[] = qsprintf(72$conn_w,73'(%s, %s, %s, %s, %s)',74$value['userPHID'],75PhabricatorHash::digestForIndex($key),76$key,77$value['value'],78$value['type']->getUserCacheType());79}8081$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();8283foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {84queryfx(85$conn_w,86'INSERT INTO %T (userPHID, cacheIndex, cacheKey, cacheData, cacheType)87VALUES %LQ88ON DUPLICATE KEY UPDATE89cacheData = VALUES(cacheData),90cacheType = VALUES(cacheType)',91$table->getTableName(),92$chunk);93}9495unset($unguarded);96}9798public static function readCaches(99PhabricatorUserCacheType $type,100$key,101array $user_phids) {102103$table = new self();104$conn = $table->establishConnection('r');105106$rows = queryfx_all(107$conn,108'SELECT userPHID, cacheData FROM %T WHERE userPHID IN (%Ls)109AND cacheType = %s AND cacheIndex = %s',110$table->getTableName(),111$user_phids,112$type->getUserCacheType(),113PhabricatorHash::digestForIndex($key));114115return ipull($rows, 'cacheData', 'userPHID');116}117118public static function clearCache($key, $user_phid) {119return self::clearCaches($key, array($user_phid));120}121122public static function clearCaches($key, array $user_phids) {123if (PhabricatorEnv::isReadOnly()) {124return;125}126127if (!$user_phids) {128return;129}130131$table = new self();132$conn_w = $table->establishConnection('w');133134$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();135136queryfx(137$conn_w,138'DELETE FROM %T WHERE cacheIndex = %s AND userPHID IN (%Ls)',139$table->getTableName(),140PhabricatorHash::digestForIndex($key),141$user_phids);142143unset($unguarded);144}145146public static function clearCacheForAllUsers($key) {147if (PhabricatorEnv::isReadOnly()) {148return;149}150151$table = new self();152$conn_w = $table->establishConnection('w');153154$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();155156queryfx(157$conn_w,158'DELETE FROM %T WHERE cacheIndex = %s',159$table->getTableName(),160PhabricatorHash::digestForIndex($key));161162unset($unguarded);163}164165}166167168