Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / people / storage / PhabricatorUserCache.php
blob71b3e4816b8c1a609327d95fb993902af0aa7b9b
1 <?php
3 final class PhabricatorUserCache extends PhabricatorUserDAO {
5 protected $userPHID;
6 protected $cacheIndex;
7 protected $cacheKey;
8 protected $cacheData;
9 protected $cacheType;
11 protected function getConfiguration() {
12 return array(
13 self::CONFIG_TIMESTAMPS => false,
14 self::CONFIG_COLUMN_SCHEMA => array(
15 'cacheIndex' => 'bytes12',
16 'cacheKey' => 'text255',
17 'cacheData' => 'text',
18 'cacheType' => 'text32',
20 self::CONFIG_KEY_SCHEMA => array(
21 'key_usercache' => array(
22 'columns' => array('userPHID', 'cacheIndex'),
23 'unique' => true,
25 'key_cachekey' => array(
26 'columns' => array('cacheIndex'),
28 'key_cachetype' => array(
29 'columns' => array('cacheType'),
32 ) + parent::getConfiguration();
35 public function save() {
36 $this->cacheIndex = Filesystem::digestForIndex($this->getCacheKey());
37 return parent::save();
40 public static function writeCache(
41 PhabricatorUserCacheType $type,
42 $key,
43 $user_phid,
44 $raw_value) {
45 self::writeCaches(
46 array(
47 array(
48 'type' => $type,
49 'key' => $key,
50 'userPHID' => $user_phid,
51 'value' => $raw_value,
53 ));
56 public static function writeCaches(array $values) {
57 if (PhabricatorEnv::isReadOnly()) {
58 return;
61 if (!$values) {
62 return;
65 $table = new self();
66 $conn_w = $table->establishConnection('w');
68 $sql = array();
69 foreach ($values as $value) {
70 $key = $value['key'];
72 $sql[] = qsprintf(
73 $conn_w,
74 '(%s, %s, %s, %s, %s)',
75 $value['userPHID'],
76 PhabricatorHash::digestForIndex($key),
77 $key,
78 $value['value'],
79 $value['type']->getUserCacheType());
82 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
84 foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
85 queryfx(
86 $conn_w,
87 'INSERT INTO %T (userPHID, cacheIndex, cacheKey, cacheData, cacheType)
88 VALUES %LQ
89 ON DUPLICATE KEY UPDATE
90 cacheData = VALUES(cacheData),
91 cacheType = VALUES(cacheType)',
92 $table->getTableName(),
93 $chunk);
96 unset($unguarded);
99 public static function readCaches(
100 PhabricatorUserCacheType $type,
101 $key,
102 array $user_phids) {
104 $table = new self();
105 $conn = $table->establishConnection('r');
107 $rows = queryfx_all(
108 $conn,
109 'SELECT userPHID, cacheData FROM %T WHERE userPHID IN (%Ls)
110 AND cacheType = %s AND cacheIndex = %s',
111 $table->getTableName(),
112 $user_phids,
113 $type->getUserCacheType(),
114 PhabricatorHash::digestForIndex($key));
116 return ipull($rows, 'cacheData', 'userPHID');
119 public static function clearCache($key, $user_phid) {
120 return self::clearCaches($key, array($user_phid));
123 public static function clearCaches($key, array $user_phids) {
124 if (PhabricatorEnv::isReadOnly()) {
125 return;
128 if (!$user_phids) {
129 return;
132 $table = new self();
133 $conn_w = $table->establishConnection('w');
135 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
137 queryfx(
138 $conn_w,
139 'DELETE FROM %T WHERE cacheIndex = %s AND userPHID IN (%Ls)',
140 $table->getTableName(),
141 PhabricatorHash::digestForIndex($key),
142 $user_phids);
144 unset($unguarded);
147 public static function clearCacheForAllUsers($key) {
148 if (PhabricatorEnv::isReadOnly()) {
149 return;
152 $table = new self();
153 $conn_w = $table->establishConnection('w');
155 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
157 queryfx(
158 $conn_w,
159 'DELETE FROM %T WHERE cacheIndex = %s',
160 $table->getTableName(),
161 PhabricatorHash::digestForIndex($key));
163 unset($unguarded);