the browser-facing portion of osu!
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Cache things in memory instead

No need for file caching as long octane is used.

nanaya 48c589b5 d3c6946a

+58 -128
-2
.env.dusk.local.example
··· 11 11 DB_DATABASE_UPDATES=osu_updates_test 12 12 13 13 ES_INDEX_PREFIX=test_ 14 - 15 - CACHE_DRIVER_LOCAL=array
-1
.env.example
··· 36 36 37 37 BROADCAST_DRIVER=log 38 38 CACHE_DRIVER=file 39 - # CACHE_DRIVER_LOCAL=file 40 39 SESSION_DRIVER=file 41 40 # SESSION_DOMAIN= 42 41 # SESSION_SECURE_COOKIE=false
-1
.github/workflows/tests.yml
··· 10 10 APP_KEY: base64:q7U5qyAkedR1F6UhN0SQlUxBpAMDyfHy3NNFkqmiMqA= 11 11 APP_URL: http://localhost:8000 12 12 CACHE_DRIVER: redis 13 - CACHE_DRIVER_LOCAL: array 14 13 DB_HOST: 127.0.0.1 15 14 DB_USERNAME: root 16 15 ES_SOLO_SCORES_HOST: http://localhost:9200
+8 -3
app/Libraries/ChatFilters.php
··· 6 6 namespace App\Libraries; 7 7 8 8 use App\Models\ChatFilter; 9 - use App\Traits\LocallyCached; 9 + use App\Traits\Memoizes; 10 10 use Illuminate\Database\Eloquent\Collection; 11 11 12 12 class ChatFilters 13 13 { 14 - use LocallyCached; 14 + use Memoizes; 15 + 16 + public function __construct() 17 + { 18 + app('local-cache-manager')->registerCallback(fn () => $this->resetMemoized()); 19 + } 15 20 16 21 public function all() 17 22 { 18 - return $this->cachedMemoize(__FUNCTION__, fn () => $this->fetch()); 23 + return $this->memoize(__FUNCTION__, fn () => $this->fetch()); 19 24 } 20 25 21 26 protected function fetch(): Collection
+8 -3
app/Libraries/Groups.php
··· 6 6 namespace App\Libraries; 7 7 8 8 use App\Models\Group; 9 - use App\Traits\LocallyCached; 10 9 use Exception; 11 10 use Illuminate\Database\Eloquent\Collection; 12 11 use Illuminate\Database\Eloquent\ModelNotFoundException; 12 + use App\Traits\Memoizes; 13 13 14 14 class Groups 15 15 { 16 - use LocallyCached; 16 + use Memoizes; 17 + 18 + public function __construct() 19 + { 20 + app('local-cache-manager')->registerCallback(fn () => $this->resetMemoized()); 21 + } 17 22 18 23 /** 19 24 * Get all groups. 20 25 */ 21 26 public function all(): Collection 22 27 { 23 - return $this->cachedMemoize(__FUNCTION__, fn () => $this->fetch()); 28 + return $this->memoize(__FUNCTION__, fn () => $this->fetch()); 24 29 } 25 30 26 31 /**
+38
app/Libraries/LocalCacheManager.php
··· 1 + <?php 2 + 3 + // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4 + // See the LICENCE file in the repository root for full licence text. 5 + 6 + declare(strict_types=1); 7 + 8 + namespace App\Libraries; 9 + 10 + class LocalCacheManager 11 + { 12 + private int $resetTicker = 0; 13 + private int $resetTickerLimit; 14 + private array $callbacks = []; 15 + 16 + public function incrementResetTicker(): void 17 + { 18 + $this->resetTicker++; 19 + $this->resetTickerLimit ??= config('osu.octane.local_cache_reset_requests'); 20 + 21 + if ($this->resetTicker > $this->resetTickerLimit) { 22 + $this->resetTicker = 0; 23 + $this->resetCache(); 24 + } 25 + } 26 + 27 + public function registerCallback(callable $callback) 28 + { 29 + $this->callbacks[] = $callback; 30 + } 31 + 32 + private function resetCache(): void 33 + { 34 + foreach ($this->callbacks as $callback) { 35 + $callback(); 36 + } 37 + } 38 + }
+1 -2
app/Listeners/OctaneResetLocalCache.php
··· 9 9 { 10 10 public function handle($event): void 11 11 { 12 - app('chat-filters')->incrementResetTicker(); 13 - app('groups')->incrementResetTicker(); 12 + app('local-cache-manager')->incrementResetTicker(); 14 13 } 15 14 }
+3 -2
app/Providers/AppServiceProvider.php
··· 12 12 use App\Libraries\CleanHTML; 13 13 use App\Libraries\Groups; 14 14 use App\Libraries\LayoutCache; 15 + use App\Libraries\LocalCacheManager; 15 16 use App\Libraries\Mods; 16 17 use App\Libraries\MorphMap; 17 18 use App\Libraries\OsuAuthorize; ··· 40 41 'clean-html' => CleanHTML::class, 41 42 'groups' => Groups::class, 42 43 'layout-cache' => LayoutCache::class, 44 + 'local-cache-manager' => LocalCacheManager::class, 43 45 'mods' => Mods::class, 44 46 'route-section' => RouteSection::class, 45 47 'score-pins' => ScorePins::class, ··· 60 62 61 63 Queue::after(function (JobProcessed $event) { 62 64 app('OsuAuthorize')->resetCache(); 63 - app('groups')->incrementResetTicker(); 64 - app('chat-filters')->incrementResetTicker(); 65 + app('local-cache-manager')->incrementResetTicker(); 65 66 66 67 Datadog::increment( 67 68 config('datadog-helper.prefix_web').'.queue.run',
-112
app/Traits/LocallyCached.php
··· 1 - <?php 2 - 3 - // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4 - // See the LICENCE file in the repository root for full licence text. 5 - 6 - namespace App\Traits; 7 - 8 - /** 9 - * Nested caching to reduce large request defined in "fetch" function 10 - * of whatever using this trait. 11 - * 12 - * Memoizes trait is included and reset accordingly. 13 - * 14 - * Use cachedMemoize to locally cache the callback's return (in addition to memoizing it). 15 - * Use resetCache to force reset on all instances. 16 - * Use forceVersionCheck to ensure version check on next memoize usage. 17 - */ 18 - trait LocallyCached 19 - { 20 - use Memoizes { 21 - Memoizes::memoize as unversionedMemoize; 22 - } 23 - 24 - private int $resetTicker = 0; 25 - private int $resetTickerLimit; 26 - private ?int $version = null; 27 - private bool $versionCheck = false; 28 - 29 - private static function getVersionCacheKey(): string 30 - { 31 - return 'local_cache_v2:'.static::class.':version'; 32 - } 33 - 34 - private static function getCurrentVersion(): int 35 - { 36 - $cacheKey = static::getVersionCacheKey(); 37 - $cache = cache(); 38 - $version = $cache->get($cacheKey); 39 - 40 - if ($version === null) { 41 - $version = hrtime(true); 42 - $cache->put($cacheKey, $version, 300); 43 - } 44 - 45 - return $version; 46 - } 47 - 48 - public function resetCache(): void 49 - { 50 - cache()->put(static::getVersionCacheKey(), hrtime(true)); 51 - 52 - $this->forceVersionCheck(); 53 - } 54 - 55 - public function forceVersionCheck(): void 56 - { 57 - $this->versionCheck = true; 58 - } 59 - 60 - public function incrementResetTicker(): void 61 - { 62 - $this->resetTicker++; 63 - $this->resetTickerLimit ??= config('osu.octane.local_cache_reset_requests'); 64 - 65 - if ($this->resetTicker > $this->resetTickerLimit) { 66 - $this->forceVersionCheck(); 67 - $this->resetTicker = 0; 68 - } 69 - } 70 - 71 - protected function cachedMemoize(string $memoizeKey, callable $function) 72 - { 73 - return $this->memoize($memoizeKey, function () use ($function, $memoizeKey) { 74 - $cache = cache()->store(config('cache.local')); 75 - $key = 'local_cache:'.static::class.':'.$memoizeKey; 76 - $value = $cache->get($key); 77 - 78 - if ($value === null || $value['version'] !== $this->version) { 79 - $value = [ 80 - 'data' => $function(), 81 - 'version' => $this->version, 82 - ]; 83 - $cache->forever($key, $value); 84 - } 85 - 86 - return $value['data']; 87 - }); 88 - } 89 - 90 - protected function memoize(string $key, callable $function) 91 - { 92 - $this->validateVersion(); 93 - 94 - return $this->unversionedMemoize($key, $function); 95 - } 96 - 97 - private function validateVersion(): void 98 - { 99 - if ($this->version !== null && !$this->versionCheck) { 100 - return; 101 - } 102 - 103 - $this->versionCheck = false; 104 - $currentVersion = static::getCurrentVersion(); 105 - 106 - if ($this->version !== null && $this->version !== $currentVersion) { 107 - $this->resetMemoized(); 108 - } 109 - 110 - $this->version = $currentVersion; 111 - } 112 - }
-1
config/cache.php
··· 14 14 */ 15 15 16 16 'default' => env('CACHE_DRIVER', 'file'), 17 - 'local' => env('CACHE_DRIVER_LOCAL', 'file'), 18 17 19 18 /* 20 19 |--------------------------------------------------------------------------
-1
phpunit.xml
··· 16 16 <env name="BEATMAPSET_REQUIRED_HYPE" value="0"/> 17 17 <env name="BROADCAST_DRIVER" value="log"/> 18 18 <env name="CACHE_DRIVER" value="array"/> 19 - <env name="CACHE_DRIVER_LOCAL" value="array"/> 20 19 <env name="CHAT_PRIVATE_LIMIT" value="2"/> 21 20 <env name="SESSION_DRIVER" value="array"/> 22 21