Skip to content

Commit

Permalink
fix: attempt at fixing caching of most popular elements
Browse files Browse the repository at this point in the history
  • Loading branch information
OptimusCrime committed Oct 16, 2024
1 parent 92a7ce3 commit 42bfcd8
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 27 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Youkok2 - 6.0.2-pl (17. oktober 2024)
=========================================

- [Info] Fixxxes
- [Fix] Problemer med caching av mest populære filer.
- [Improvement] Fjernet litt ubrukt kode og ryddet opp litt.

Youkok2 - 6.0.1-pl (5. oktober 2024)
=========================================

Expand All @@ -18,7 +25,7 @@ Youkok2 - 6.0.0-pl (23. september 2024)
- [Removal] Alle spørringer henter nå alle attributter i `elements`-tabellen.
- [Removal] Variabler på model-klassene, som bare lagde problemer.
- [Improvement] Forbedret logging av feilmeldinger i stede for å legge exceptions til default error
- handler.
handler.
- [Improvement] Forenklet caching drastisk.
- [Improvement] Caching av hele output-payload, for å redusere unødvendig arbeid.
- [Change] Drastisk forenklet og redusert hvordan vi logger nedlastninger, som gjør at databasen
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.0.1-pl
6.0.2-pl
12 changes: 1 addition & 11 deletions youkok2/src/Biz/Services/CacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use RedisException;
use Youkok\Common\Utilities\CacheKeyGenerator;
use Youkok\Enums\MostPopularCourse;
use Youkok\Enums\MostPopularElement;

class CacheService
Expand All @@ -26,15 +25,6 @@ public function getMostPopularElementsSetFromDelta(MostPopularElement $delta): a
return $this->getSortedRangeByKey($key);
}

/**
* @throws RedisException
*/
public function getMostPopularCoursesSetFromDelta(MostPopularCourse $delta): array
{
$key = CacheKeyGenerator::keyForMostPopularCoursesSetForDelta($delta);
return $this->getSortedRangeByKey($key);
}

/**
* @throws RedisException
*/
Expand Down Expand Up @@ -80,7 +70,7 @@ public function insertIntoSet(string $setKey, int $value, string $id): void
/**
* @throws RedisException
*/
public function updateValueInSet($setKey, $increase, $id): void
public function updateValueInSet(string $setKey, int $increase, string $id): void
{
$this->cache->zIncrBy($setKey, $increase, $id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function addAndFlushDownloadForElementInMostPopularSets(Element $element
foreach (MostPopularElement::collection() as $delta) {
$setKey = CacheKeyGenerator::keyForMostPopularElementsSetForDelta($delta);

$this->cacheService->updateValueInSet($setKey, 1, $element->id);
$this->cacheService->updateValueInSet($setKey, 1, (string) $element->id);

$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularElementsForDelta($delta));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ private function getAllCoursesToUpdate(): Collection
*/
private function clearMostPopularCaches(): void
{
// Clear set before clearing the actual output cache, so that we don't get a race condition
foreach (MostPopularElement::collection() as $delta) {
$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularElementsSetForDelta($delta));
}

foreach (MostPopularElement::collection() as $delta) {
$this->cacheService->delete(CacheKeyGenerator::keyForMostPopularElementsForDelta($delta));
}

Expand Down
9 changes: 7 additions & 2 deletions youkok2/src/Biz/Services/Models/DownloadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Illuminate\Support\Collection;
use Youkok\Biz\Exceptions\ElementNotFoundException;
use Youkok\Biz\Exceptions\InvalidValueException;
use Youkok\Common\Models\Element;
use Youkok\Common\Utilities\SelectStatements;
use Youkok\Enums\MostPopularCourse;
Expand Down Expand Up @@ -58,13 +59,15 @@ public function getLatestDownloads(int $limit): array

/**
* @throws Exception
* @throws InvalidValueException
*/
public function getMostPopularElementsFromDelta(MostPopularElement $delta): Collection
{
$query = Element::select(Element::ALL_FIELDS)
->where('deleted', false)
->where('pending', false)
->where('requested_deletion', false)
->where('directory', false)
->whereNotNull('parent');

switch ($delta->getValue()) {
Expand All @@ -81,9 +84,10 @@ public function getMostPopularElementsFromDelta(MostPopularElement $delta): Coll
$query = $query->orderBy('downloads_year', 'DESC');
break;
case MostPopularElement::ALL()->getValue():
default:
$query = $query->orderBy('downloads_all', 'DESC');
break;
default:
throw new InvalidValueException('Unexpected most popular element value: ' . $delta->getValue());

}

Expand Down Expand Up @@ -115,9 +119,10 @@ public function getMostPopularCursesFromDelta(MostPopularCourse $delta, int $lim
$query = $query->orderBy('downloads_year', 'DESC');
break;
case MostPopularCourse::ALL()->getValue():
default:
$query = $query->orderBy('downloads_all', 'DESC');
break;
default:
throw new InvalidValueException('Unexpected most popular course value: ' . $delta->getValue());
}

return $query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RedisException;
use Slim\Interfaces\RouteParserInterface;
use Youkok\Biz\Exceptions\ElementNotFoundException;
use Youkok\Biz\Exceptions\InvalidValueException;
use Youkok\Biz\Services\CacheService;
use Youkok\Biz\Services\Mappers\CourseMapper;
use Youkok\Biz\Services\Models\DownloadService;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function __construct(
/**
* @throws RedisException
* @throws Exception
* @throws InvalidValueException
*/
public function getMostPopularCourses(RouteParserInterface $routeParser, MostPopularCourse $delta, int $limit): array
{
Expand All @@ -55,8 +57,8 @@ public function getMostPopularCourses(RouteParserInterface $routeParser, MostPop
}

/**
* @throws RedisException
* @throws Exception
* @throws InvalidValueException
*/
private function mapMostPopularCourse(RouteParserInterface $routeParser, Collection $elements, MostPopularCourse $delta, int $limit): array
{
Expand All @@ -77,9 +79,10 @@ private function mapMostPopularCourse(RouteParserInterface $routeParser, Collect
$fields[] = CourseMapper::DOWNLOADS_YEAR;
break;
case MostPopularCourse::ALL()->getValue():
default:
$fields[] = CourseMapper::DOWNLOADS_ALL;
break;
default:
throw new InvalidValueException('Unexpected most popular course value: ' . $delta->getValue());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RedisException;
use Slim\Interfaces\RouteParserInterface;
use Youkok\Biz\Exceptions\ElementNotFoundException;
use Youkok\Biz\Exceptions\InvalidValueException;
use Youkok\Biz\Services\CacheService;
use Youkok\Biz\Services\Mappers\ElementMapper;
use Youkok\Biz\Services\Models\DownloadService;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function __construct(

/**
* @throws RedisException
* @throws InvalidValueException
* @throws Exception
*/
public function getMostPopularElements(RouteParserInterface $routeParser, MostPopularElement $delta, int $limit): array
Expand All @@ -60,6 +62,10 @@ public function getMostPopularElements(RouteParserInterface $routeParser, MostPo

$elements = [];
foreach ($mostPopularSet as $id => $downloads) {
if ((int)$downloads === 0) {
break;
}

try {
$element = $this->elementService->getElement(
new SelectStatements('id', $id),
Expand Down Expand Up @@ -140,6 +146,7 @@ private function mapMostPopularElements(RouteParserInterface $routeParser, array

/**
* @throws RedisException
* @throws InvalidValueException
* @throws Exception
*/
private function buildMostPopularCacheSet(MostPopularElement $delta): void
Expand All @@ -149,10 +156,13 @@ private function buildMostPopularCacheSet(MostPopularElement $delta): void

foreach ($elements as $element) {
$downloads = static::getDownloadsFromElement($element, $delta);
$this->cacheService->insertIntoSet($key, (int)$downloads, (string)$element->id);
$this->cacheService->insertIntoSet($key, $downloads, (string)$element->id);
}
}

/**
* @throws InvalidValueException
*/
private static function getDownloadsFromElement(Element $element, MostPopularElement $delta): int
{
switch ($delta->getValue()) {
Expand All @@ -165,8 +175,9 @@ private static function getDownloadsFromElement(Element $element, MostPopularEle
case MostPopularElement::YEAR()->getValue():
return $element->downloads_year;
case MostPopularElement::ALL()->getValue():
default:
return $element->downloads_all;
default:
throw new InvalidValueException('Unexpected most popular element value: ' . $delta->getValue());
}
}
}
9 changes: 2 additions & 7 deletions youkok2/src/Common/Utilities/CacheKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ public static function keyForMostPopularElementsForDelta(MostPopularElement $del
return 'most_popular_elements_' . $delta->getValue();
}

public static function keyForMostPopularCoursesSetForDelta(MostPopularCourse $delta): string
public static function keyForMostPopularCoursesForDelta(MostPopularCourse $delta): string
{
return 'most_popular_courses_set_' . $delta->getValue();
}

public static function keyForMostPopularCoursesForDelta(string $delta): string
{
return 'most_popular_courses_' . $delta;
return 'most_popular_courses_' . $delta->getValue();
}

public static function keyForTotalNumberOfDownloads(): string
Expand Down

0 comments on commit 42bfcd8

Please sign in to comment.