Skip to content

Commit 42a3ca3

Browse files
committed
Migrate SortedUserCache to TolerantCache
1 parent 1ae1d1d commit 42a3ca3

File tree

6 files changed

+135
-60
lines changed

6 files changed

+135
-60
lines changed

wcfsetup/install/files/lib/system/box/UserListBoxController.class.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use wcf\data\condition\Condition;
66
use wcf\data\DatabaseObject;
77
use wcf\data\user\UserProfileList;
8-
use wcf\system\cache\builder\MostActiveMembersCacheBuilder;
9-
use wcf\system\cache\builder\MostLikedMembersCacheBuilder;
10-
use wcf\system\cache\builder\NewestMembersCacheBuilder;
118
use wcf\system\cache\runtime\UserProfileRuntimeCache;
9+
use wcf\system\cache\tolerant\AbstractTolerantCache;
10+
use wcf\system\cache\tolerant\SortedUserCache;
1211
use wcf\system\condition\IObjectListCondition;
1312
use wcf\system\event\EventHandler;
1413
use wcf\system\request\LinkHandler;
@@ -28,13 +27,19 @@ class UserListBoxController extends AbstractDatabaseObjectListBoxController
2827
{
2928
/**
3029
* maps special sort fields to cache builders
30+
*
3131
* @var string[]
32+
*
33+
* @deprecated 6.2 use `$cacheHandlers` instead
3234
*/
33-
public $cacheBuilders = [
34-
'activityPoints' => MostActiveMembersCacheBuilder::class,
35-
'likesReceived' => MostLikedMembersCacheBuilder::class,
36-
'registrationDate' => NewestMembersCacheBuilder::class,
37-
];
35+
public $cacheBuilders = [];
36+
37+
/**
38+
* maps special sort fields to tolerant caches
39+
*
40+
* @var array<string, callable(int $limt, string $sortOrder, Condition[] $conditions): AbstractTolerantCache<list<int>>>
41+
*/
42+
public array $cacheHandlers;
3843

3944
/**
4045
* @inheritDoc
@@ -76,8 +81,30 @@ class UserListBoxController extends AbstractDatabaseObjectListBoxController
7681
*/
7782
public function __construct()
7883
{
84+
$this->cacheHandlers = [
85+
'registrationDate' => static fn(int $limit, string $sortOrder, array $conditions) => new SortedUserCache(
86+
'registrationDate',
87+
$sortOrder,
88+
$limit,
89+
conditions: $conditions
90+
),
91+
'activityPoints' => static fn(int $limit, string $sortOrder, array $conditions) => new SortedUserCache(
92+
'activityPoints',
93+
$sortOrder,
94+
$limit,
95+
true,
96+
$conditions
97+
)
98+
];
99+
79100
if (!empty($this->validSortFields) && MODULE_LIKE) {
80101
$this->validSortFields[] = 'likesReceived';
102+
103+
$this->cacheHandlers['likesReceived'] = static fn(
104+
int $limit,
105+
string $sortOrder,
106+
array $conditions
107+
) => new SortedUserCache('likesReceived', $sortOrder, $limit, true, $conditions);
81108
}
82109

83110
parent::__construct();
@@ -106,16 +133,22 @@ public function getLink(): string
106133
protected function getObjectList()
107134
{
108135
// use specialized cache builders
109-
if ($this->sortOrder && $this->sortField && isset($this->cacheBuilders[$this->sortField])) {
136+
if ($this->sortOrder && $this->sortField) {
110137
$conditions = \array_filter($this->box->getConditions(), static function (Condition $condition) {
111138
return $condition->getObjectType()->getProcessor() instanceof IObjectListCondition;
112139
});
113140

114-
$this->userIDs = \call_user_func([$this->cacheBuilders[$this->sortField], 'getInstance'])->getData([
115-
'conditions' => $conditions,
116-
'limit' => $this->limit,
117-
'sortOrder' => $this->sortOrder,
118-
]);
141+
if (isset($this->cacheHandlers[$this->sortField])) {
142+
$tolerantCache = $this->cacheHandlers[$this->sortField]($this->limit, $this->sortOrder, $conditions);
143+
$this->userIDs = $tolerantCache->getCache();
144+
} elseif (isset($this->cacheBuilders[$this->sortField])) {
145+
// backwards compatibility
146+
$this->userIDs = \call_user_func([$this->cacheBuilders[$this->sortField], 'getInstance'])->getData([
147+
'conditions' => $conditions,
148+
'limit' => $this->limit,
149+
'sortOrder' => $this->sortOrder,
150+
]);
151+
}
119152
}
120153

121154
if ($this->userIDs !== null) {

wcfsetup/install/files/lib/system/cache/builder/AbstractSortedUserCacheBuilder.class.php

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
namespace wcf\system\cache\builder;
44

5-
use wcf\data\condition\Condition;
6-
use wcf\data\DatabaseObject;
7-
use wcf\data\DatabaseObjectList;
8-
use wcf\data\user\UserList;
9-
use wcf\system\condition\IObjectListCondition;
5+
use wcf\system\cache\tolerant\SortedUserCache;
106

117
/**
128
* Caches a list of the newest members.
@@ -15,8 +11,9 @@
1511
* @copyright 2001-2019 WoltLab GmbH
1612
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
1713
* @since 3.0
14+
* @deprecated 6.2 use `SortedUserCache` instead
1815
*/
19-
abstract class AbstractSortedUserCacheBuilder extends AbstractCacheBuilder
16+
abstract class AbstractSortedUserCacheBuilder extends AbstractLegacyCacheBuilder
2017
{
2118
/**
2219
* default limit value if no limit parameter is provided
@@ -30,11 +27,6 @@ abstract class AbstractSortedUserCacheBuilder extends AbstractCacheBuilder
3027
*/
3128
protected $defaultSortOrder = 'DESC';
3229

33-
/**
34-
* @inheritDoc
35-
*/
36-
protected $maxLifetime = 300;
37-
3830
/**
3931
* if `true`, only positive values of the database column will be considered
4032
* @var bool
@@ -47,32 +39,27 @@ abstract class AbstractSortedUserCacheBuilder extends AbstractCacheBuilder
4739
*/
4840
protected $sortField;
4941

50-
/**
51-
* @inheritDoc
52-
*/
53-
protected function rebuild(array $parameters)
42+
#[\Override]
43+
public function reset(array $parameters = [])
5444
{
55-
$sortOrder = $this->defaultSortOrder;
56-
if (!empty($parameters['sortOrder'])) {
57-
$sortOrder = $parameters['sortOrder'];
58-
}
59-
60-
$userProfileList = new UserList();
61-
if ($this->positiveValuesOnly) {
62-
$userProfileList->getConditionBuilder()->add('user_table.' . $this->sortField . ' > ?', [0]);
63-
}
64-
if (isset($parameters['conditions'])) {
65-
/** @var Condition $condition */
66-
foreach ($parameters['conditions'] as $condition) {
67-
/** @var IObjectListCondition<DatabaseObjectList<DatabaseObject>> $processor */
68-
$processor = $condition->getObjectType()->getProcessor();
69-
$processor->addObjectListCondition($userProfileList, $condition->conditionData);
70-
}
71-
}
72-
$userProfileList->sqlOrderBy = 'user_table.' . $this->sortField . ' ' . $sortOrder;
73-
$userProfileList->sqlLimit = !empty($parameters['limit']) ? $parameters['limit'] : $this->defaultLimit;
74-
$userProfileList->readObjectIDs();
45+
(new SortedUserCache(
46+
$this->sortField,
47+
$parameters['sortOrder'] ?? $this->defaultSortOrder,
48+
$parameters['limit'] ?? $this->defaultLimit,
49+
$this->positiveValuesOnly,
50+
$parameters['conditions'] ?? []
51+
))->rebuild();
52+
}
7553

76-
return $userProfileList->getObjectIDs();
54+
#[\Override]
55+
protected function rebuild(array $parameters): array
56+
{
57+
return (new SortedUserCache(
58+
$this->sortField,
59+
$parameters['sortOrder'] ?? $this->defaultSortOrder,
60+
$parameters['limit'] ?? $this->defaultLimit,
61+
$this->positiveValuesOnly,
62+
$parameters['conditions'] ?? []
63+
))->getCache();
7764
}
7865
}

wcfsetup/install/files/lib/system/cache/builder/MostActiveMembersCacheBuilder.class.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
* @author Marcel Werk
99
* @copyright 2001-2019 WoltLab GmbH
1010
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
*
12+
* @deprecated 6.2 use `SortedUserCache` instead
1113
*/
1214
class MostActiveMembersCacheBuilder extends AbstractSortedUserCacheBuilder
1315
{
14-
/**
15-
* @inheritDoc
16-
*/
17-
protected $maxLifetime = 600;
18-
1916
/**
2017
* @inheritDoc
2118
*/

wcfsetup/install/files/lib/system/cache/builder/MostLikedMembersCacheBuilder.class.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
* @author Matthias Schmidt
99
* @copyright 2001-2019 WoltLab GmbH
1010
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
*
12+
* @deprecated 6.2 use `SortedUserCache` instead
1113
*/
1214
class MostLikedMembersCacheBuilder extends AbstractSortedUserCacheBuilder
1315
{
14-
/**
15-
* @inheritDoc
16-
*/
17-
protected $maxLifetime = 600;
18-
1916
/**
2017
* @inheritDoc
2118
*/

wcfsetup/install/files/lib/system/cache/builder/NewestMembersCacheBuilder.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @author Matthias Schmidt
99
* @copyright 2001-2019 WoltLab GmbH
1010
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11+
*
12+
* @deprecated 6.2 use `SortedUserCache` instead
1113
*/
1214
class NewestMembersCacheBuilder extends AbstractSortedUserCacheBuilder
1315
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace wcf\system\cache\tolerant;
4+
5+
use wcf\data\condition\Condition;
6+
use wcf\data\DatabaseObject;
7+
use wcf\data\DatabaseObjectList;
8+
use wcf\data\user\UserList;
9+
use wcf\system\condition\IObjectListCondition;
10+
11+
/**
12+
* Caches a sorted list of userIDs.
13+
*
14+
* @author Olaf Braun
15+
* @copyright 2001-2025 WoltLab GmbH
16+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17+
* @since 6.2
18+
*
19+
* @extends AbstractTolerantCache<list<int>>
20+
*/
21+
final class SortedUserCache extends AbstractTolerantCache
22+
{
23+
public function __construct(
24+
public readonly string $sortField,
25+
public readonly string $sortOrder = 'DESC',
26+
public readonly int $limit = 5,
27+
public readonly bool $positiveValuesOnly = false,
28+
/** @var Condition[] */
29+
public readonly array $conditions = []
30+
) {
31+
}
32+
33+
#[\Override]
34+
public function getLifetime(): int
35+
{
36+
return 300;
37+
}
38+
39+
#[\Override]
40+
protected function rebuildCacheData(): array
41+
{
42+
$userProfileList = new UserList();
43+
if ($this->positiveValuesOnly) {
44+
$userProfileList->getConditionBuilder()->add('user_table.' . $this->sortField . ' > ?', [0]);
45+
}
46+
47+
foreach ($this->conditions as $condition) {
48+
/** @var IObjectListCondition<DatabaseObjectList<DatabaseObject>> $processor */
49+
$processor = $condition->getObjectType()->getProcessor();
50+
$processor->addObjectListCondition($userProfileList, $condition->conditionData);
51+
}
52+
53+
$userProfileList->sqlOrderBy = 'user_table.' . $this->sortField . ' ' . $this->sortOrder;
54+
$userProfileList->sqlLimit = $this->limit;
55+
$userProfileList->readObjectIDs();
56+
57+
return $userProfileList->getObjectIDs();
58+
}
59+
}

0 commit comments

Comments
 (0)