Keep your cached content up to date
Once you’ve picked your caching strategy, handling cache dependencies becomes crucial.That way you always know which part of your cache to invalidate when something in your Kontent.ai project changes.
GetPost() action, you get a JSON response with the specified blog post. Whenever you get a response from the API, you store the response in the cache.<action_name>|<action_parameters> for its key. Use this pattern to uniquely identify the responses within the cache for each of your custom actions.GetPost|my_blog_post. You can adjust the pattern to suit your needs and naming conventions.GetPost("my_blog_post"), it retrieves the blog post from the cache without having to make any request to the API.item object, you find a content item representing the blog post itself.modular_content object property, you find the components and linked items as separate object properties.<object_type>:<object_codename>. Using the content item from the simplified JSON, the dependency name would be item:guest_blog_post. This way, you can invalidate the correct dependencies whenever there’s a change in your content items.modular_content object in the API response contains both content items and componentsmodular_content object property is below 30, add cache dependencies for all the objects.modular_content property contains too many objects to define as separate dependencies, add a special general dependency for any content item. In such case, your app invalidates the cached response whenever any other content item is invalidated.// Without custom actions, you use the Delivery client directly, specifying its parameters each time.
Client = DeliveryClient("<YOUR_PROJECT_ID>")
response = Client.items()
.type("<type_codename>")
.limit(10)
.skip(20)
// With custom actions, you use named action to perform business logic.
// GetPost action retrieves a blog post by its codename.
GetPost("<post_codename>")
// GetPosts action retrieves a list of blog posts and provides paging.
GetPosts( <skip>, <limit>)
// GetPostsByCategory action retrieves a list of blog posts tagged with a specific category, and provides paging.
GetPostsByCategory("<category_codename>", <skip>, <limit>){
"item": {
"system": {
"id": "f4b3fc05-e988-4dae-9ac1-a94aba566474",
"name": "My blog post",
"codename": "my_blog_post",
"language": "default",
"type": "blog_post",
"sitemap_locations": [],
"last_modified": "2022-10-20T12:03:48.4628352Z"
},
"elements": { ... }
},
"modular_content": {
"guest_blog_post": {
"system": { ... },
"elements": { ... }
},
"n2dfcbed2_d7a1_0183_4324_a2282f735f48": {
"system": { ... },
"elements": { ... }
}
}
}