feat:(omkar_cache_api):explored cache api to fetch latest added blogs #42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).