Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/storage/PhabricatorUserCache.php
12256 views
1
<?php
2
3
final class PhabricatorUserCache extends PhabricatorUserDAO {
4
5
protected $userPHID;
6
protected $cacheIndex;
7
protected $cacheKey;
8
protected $cacheData;
9
protected $cacheType;
10
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',
19
),
20
self::CONFIG_KEY_SCHEMA => array(
21
'key_usercache' => array(
22
'columns' => array('userPHID', 'cacheIndex'),
23
'unique' => true,
24
),
25
'key_cachekey' => array(
26
'columns' => array('cacheIndex'),
27
),
28
'key_cachetype' => array(
29
'columns' => array('cacheType'),
30
),
31
),
32
) + parent::getConfiguration();
33
}
34
35
public function save() {
36
$this->cacheIndex = Filesystem::digestForIndex($this->getCacheKey());
37
return parent::save();
38
}
39
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,
52
),
53
));
54
}
55
56
public static function writeCaches(array $values) {
57
if (PhabricatorEnv::isReadOnly()) {
58
return;
59
}
60
61
if (!$values) {
62
return;
63
}
64
65
$table = new self();
66
$conn_w = $table->establishConnection('w');
67
68
$sql = array();
69
foreach ($values as $value) {
70
$key = $value['key'];
71
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());
80
}
81
82
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
83
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);
94
}
95
96
unset($unguarded);
97
}
98
99
public static function readCaches(
100
PhabricatorUserCacheType $type,
101
$key,
102
array $user_phids) {
103
104
$table = new self();
105
$conn = $table->establishConnection('r');
106
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));
115
116
return ipull($rows, 'cacheData', 'userPHID');
117
}
118
119
public static function clearCache($key, $user_phid) {
120
return self::clearCaches($key, array($user_phid));
121
}
122
123
public static function clearCaches($key, array $user_phids) {
124
if (PhabricatorEnv::isReadOnly()) {
125
return;
126
}
127
128
if (!$user_phids) {
129
return;
130
}
131
132
$table = new self();
133
$conn_w = $table->establishConnection('w');
134
135
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
136
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);
143
144
unset($unguarded);
145
}
146
147
public static function clearCacheForAllUsers($key) {
148
if (PhabricatorEnv::isReadOnly()) {
149
return;
150
}
151
152
$table = new self();
153
$conn_w = $table->establishConnection('w');
154
155
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
156
157
queryfx(
158
$conn_w,
159
'DELETE FROM %T WHERE cacheIndex = %s',
160
$table->getTableName(),
161
PhabricatorHash::digestForIndex($key));
162
163
unset($unguarded);
164
}
165
166
}
167
168