Skip to main content
Release version: β / RCversion

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].

Image from Gyazo

Click the [Add] button on the top right of the screen.

Image from Gyazo

In the custom function editing screen, make the following settings:

FieldValue
Titleget_topics_list_by_category
Identifierget_topics_list_by_category
ProcessingThe 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}
caution

Please specify the endpoint of Topics::list in your environment.

Image from Gyazo

Image from Gyazo

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].

Image from Gyazo

Click [Add].

Image from Gyazo

Configure the endpoint settings in the dialog box and click [Add] to save.

FieldSetting
Pathtopics/list/wrapper
CategoryAPI
ModelApi
Operationrequest_api
Cache86400
nameIdentifier for custom function (get_topics_list_by_category)
use_path_paramEnable

Image from Gyazo

Operation Verification

Let's verify the functionality of the created endpoint. Click on [Swagger UI] on the API list screen.

Image from Gyazo

Select the created topics/list/wrapper endpoint.

Image from Gyazo

Click on [Try it out].

Image from Gyazo

Enter any category ID in the data_id field and click [Execute] to run.

Image from Gyazo

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.

Image from Gyazo

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].

Image from Gyazo

Click on the [Add] button on the top right of the screen.

Image from Gyazo

In the custom function editing screen, make the following settings:

FieldValue
Titleclear_wrapped_endpont_cash
Identifierclear_wrapped_endpont_cash
Trigger
  • Module: Content
  • Trigger: After content update
  • Target: Any custom structure
ProcessThe 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`"}
caution

Specify the endpoint of Topics::list in your environment.

Image from Gyazo

Image from Gyazo

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.

Image from Gyazo

Ensure that the cache for other categories is not affected.

Image from Gyazo

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.


Support

If you have any other questions, please contact us or check out Our Slack Community.