Skip to content

Commit

Permalink
Disabled cache when the progress is enabled. (systemseed#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalabro authored May 25, 2022
1 parent 06dbff4 commit 64ce4f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
-

## [2.5.6]
- Disabled cache when the progress is enabled.

## [2.5.5]
- Improved page loading performance by adding caching for serialized data.

Expand Down
10 changes: 8 additions & 2 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\anu_lms;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityInterface;
Expand Down Expand Up @@ -145,8 +144,15 @@ public function normalizeEntity(EntityInterface $entity, array $context = []) {
// Normalize recursively given entity.
$normalized_entity = $this->serializer->normalize($entity, 'json_recursive', $context);

/** @var \Drupal\Core\Cache\CacheableMetadata $cacheable_metadata */
$cacheable_metadata = $context[$cacheable_key];
// Saving normalized entity to the cache for further usage.
$this->cache->set($cache_cid, $normalized_entity, Cache::PERMANENT, $context[$cacheable_key]->getCacheTags());
$this->cache->set(
$cache_cid,
$normalized_entity,
$cacheable_metadata->getCacheMaxAge(),
$cacheable_metadata->getCacheTags(),
);

return $normalized_entity;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Normalizer/NodeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public function normalize($entity, $format = NULL, array $context = []) {

if ($entity->bundle() === 'course' && $courseHandler->isLinearProgressEnabled($entity)) {
$normalized['progress'] = $courseProgressHandler->getCourseProgress($entity);

// If progress is enabled, we disable all REST Entity Recursive cache!
// @todo extract progress data in a separate data attribute and enable
// back cache.
if (isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
/** @var \Drupal\Core\Cache\CacheableMetadata $cacheable_metadata */
$cacheable_metadata = $context[static::SERIALIZATION_CONTEXT_CACHEABILITY];
$cacheable_metadata->setCacheMaxAge(0);
}
}

if (
Expand Down

0 comments on commit 64ce4f0

Please sign in to comment.