mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-12 06:30:07 +01:00
fix: speed up paste random function a lot
This commit is contained in:
parent
83f4fa56c4
commit
0b6f54b0d6
1 changed files with 26 additions and 7 deletions
|
@ -104,12 +104,31 @@ class Paste extends Model {
|
|||
->limit($count)->get();
|
||||
}
|
||||
|
||||
public static function getRandom(int $count = 10) : Collection {
|
||||
return Paste::with('user')
|
||||
public static function getRandom(int $count = 10) : array {
|
||||
$total_pastes = Paste::count();
|
||||
$pastes = [];
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$pastes[] = self::getSingleRandom($total_pastes);
|
||||
}
|
||||
|
||||
return $pastes;
|
||||
}
|
||||
|
||||
private static function getSingleRandom(int $total) {
|
||||
$paste = null;
|
||||
|
||||
do {
|
||||
$paste_id = rand(1, $total);
|
||||
|
||||
$paste = Paste::with('user')
|
||||
->where('visible', self::VISIBILITY_PUBLIC)
|
||||
->where('is_hidden', false)
|
||||
->whereRaw("((expiry IS NULL) OR ((expiry != 'SELF') AND (expiry > NOW())))")
|
||||
->orderByRaw('RAND()')
|
||||
->limit($count)->get();
|
||||
->where('id', $paste_id)
|
||||
->first();
|
||||
} while (!$paste);
|
||||
|
||||
return $paste;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue