Create an endpoint that clears cache for each content category
Overview
When updating content from the management screen, the cache of the Topics endpoint with the same custom structure ID is cleared. Therefore, the Topics::list endpoint clears the cache every time the content is updated, ensuring that the latest data is always retrieved.
However, in cases where frequent cache clearing is not necessary or when you only want to clear the cache within the same category, wrapping Topics::list in the API::api_request endpoint can be effective. This prevents the cache from being cleared when content is updated, thus increasing cache hit rate. (However, if you choose this method, please note that you need to design and set up cache management on your own.)
For example, even if you specify a category (content_type) using Topics::list, the cache can still be cleared by updating content in a different category as long as the custom structure ID is the same.
This tutorial explains how to create a list endpoint with a mechanism to clear the cache for each content category by wrapping Topics::list in the API::api_request endpoint.
What You Will Learn
You will create an endpoint that controls the cache independently using the following steps:
Prerequisites
- If Topics::list endpoint does not exist, please create it beforehand.
- It is assumed that one content category is set for each content.
Create an endpoint that wraps Topics::list
Create custom function
First, create custom function to wrap Topics::list. Click [Operation] -> [Custom function].
Click the [Add] button on the top right of the screen.
In the custom function editing screen, make the following settings:
Field | Value |
---|---|
Title | get_topics_list_by_category |
Identifier | get_topics_list_by_category |
Processing | The following content |
{api_internal
endpoint='/rcms_api/1/topics/list/1'
method='GET'
query="contents_type[]=`$smarty.request.data_id`"
direct=true
var='response'
status_var='status'}
{assign var="data" value=$response}
Please specify the endpoint of Topics::list in your environment.
Click [Add] to add the custom function.
Create an endpoint
Next, create an API::api_request endpoint that calls the custom function you created.
Navigate to your list of endpoints. Here, click [API] -> [Default].
Click [Add].
Configure the endpoint settings in the dialog box and click [Add] to save.
Field | Setting |
---|---|
Path | topics/list/wrapper |
Category | API |
Model | Api |
Operation | request_api |
Cache | 86400 |
name | Identifier for custom function (get_topics_list_by_category) |
use_path_param | Enable |
Operation Verification
Let's verify the functionality of the created endpoint. Click on [Swagger UI] on the API list screen.
Select the created topics/list/wrapper
endpoint.
Click on [Try it out].
Enter any category ID in the data_id field and click [Execute] to run.
Verify that the response code is 200 and a successful response is received, also check that for subsequent requests, the Age value is set in the response header, indicating that the cache is being utilized.
Setting a Trigger to Clear Cache
Creating a Custom Function
Create a custom function trigger to clear cache only for specific URL endpoints when content is updated.
Click on [Operation] -> [Custom function].
Click on the [Add] button on the top right of the screen.
In the custom function editing screen, make the following settings:
Field | Value |
---|---|
Title | clear_wrapped_endpont_cash |
Identifier | clear_wrapped_endpont_cash |
Trigger |
|
Process | The following content |
{assign_topics_detail var='topics_data' topics_id=$topics_id }
{purge_cdn_cache api_endpoint="/rcms-api/1/topics/list/wrapper/`$topics_data.contents_type`"}
Specify the endpoint of Topics::list in your environment.
Click the [Add] button to add the custom function.
Verification
Verify the trigger operation.
Update the content in the target custom structure.
After the update, confirm that the cache for the corresponding category endpoint has been cleared.
Ensure that the cache for other categories is not affected.
This completes the creation of a list endpoint that wraps Topics::list and clears the cache for each content category in your environment. In this tutorial, cache clearing is only performed when content is updated. If you want to clear the cache at other times, such as when adding content, create a custom function with the necessary triggers.
Related Documents
Support
If you have any other questions, please contact us or check out Our Slack Community.