Skip to content

Conversation

@omkartidke42
Copy link
Contributor

This PR introduces a new custom Drupal module, omkar_cache_api, which caches the latest published blog posts to enhance performance and reduce database load. The module provides a service for fetching blog posts with caching support, an event subscriber to invalidate the cache when blog posts are updated, and a controller to display the cached data on a custom route.

📦 Features Included
Custom Service:
BlogCacheService fetches and caches the latest published blog posts using cache.default.

Controller:
BlogController exposes the /latest-blogs route to display cached blog post titles as links.

Event Subscriber:
BlogEventSubscriber listens to node.insert, node.update, and node.delete events for the blog content type and clears the cache accordingly.

Dependency Injection:
All services (logger, entity type manager, cache) are injected properly using constructor injection to follow Drupal best practices.

Custom Route:
/latest-blogs displays the latest blog posts using a themed item list.

🧩 Files Added
omkar_cache_api.services.yml – Registers services and dependencies.

BlogCacheService.php – Core logic for caching.

BlogController.php – Handles route and display logic.

BlogEventSubscriber.php – Automatically clears cache on blog entity changes.

omkar_cache_api.routing.yml – Adds custom route for latest blogs.

omkar_cache_api.info.yml – Module metadata.

omkar_cache_api.module – Empty stub file, currently unused.

🧪 How to Test
Enable the module:
drush en omkar_cache_api

Visit /latest-blogs — should show latest blog posts (or a "No blogs available" message).

Create, update, or delete a blog post — the list should refresh due to cache invalidation.

📌 Notes
Uses @logger.channel.omkar_cache_api for scoped logging.

Cache uses tag node_list for proper invalidation.

Designed for Drupal 11 (core_version_requirement: ^11).

@shriaas-qed
Copy link
Contributor

This implementation for caching is incorrect, you don't need to create your own service. Check official documentation: https://www.drupal.org/docs/drupal-apis/cache-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants