Organizing Cache Keys In Complex Systems

  • This is a PHP article.
  • Laravel was used to write this article.
  • We'll be able to keep cache keys in the same codebase.
  • Due to this, we'll be able to quickly locate cache key-related code blocks in the same codebase or 3rd party's codebase.
  • We'll make an inventory class to keep all cache keys in one place.
  • The system will generate ghost methods from the inventory class automatically.
  • We'll write a cache-broking service that supports all clear methods through the inventory class.

Creating Cache Key Inventory

To build the cache keys for the project's services or endpoints, let's make a class similar to inventory. This class will contain static methods, and each method will return a string value for the cache key it is associated with.

Using "CacheKeysInventory" to Create Ghost Clear Methods

We need an auto-generation mechanism or something similar to keep code simple and maintain methods in sync. We'll use PHP's classes' __callstatic magic method.

Cache-Broking Service Endpoint

We need a service that allows 3rd parties to clear cache through our codebase to retain all caching and clearing functionalities in the same codebase.

Creating Endpoint In API

Let's create two endpoints in routes/api.php

/cache-broking endpoint

This endpoint displays all of the ways that may be used to clear key specific caches using the API.

/cache-broking/{cacheKeyMethod} endpoint

This endpoint attempts to clear the cache for specific cache keys that match the method name.

Validating Clear Cache Requests That Came Over Endpoint

I created a request class that tries to check parameters for "CacheKeysInventory" class methods' ghost methods. This will validate as many parameters for ghost methods as possible.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store