{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
3

Using the ResponseCache policy responsibly! (or, Don't cache POST requests)  

  • Export to PDF
Dino   created · Mar 10, 2016 at 03:27 PM · 2.5k Views · edited · Jan 24, 2018 at 09:44 PM

Today, I want to discuss the ResponseCache policy.

The ResponseCache policy allows API publishers to add caching to any backend system. This simple capability is really powerful and can help you offload over-burdened backend systems, or more importantly improve response times for snappier front-end performance.

But it's important to use this tool in the right way. The HTTP Spec says that caches in HTTP servers should be used to satisfy only GET or HEAD verbs. This means a system should never return a cached result for a POST or a PUT or a DELETE, for example. But out of the box, the ResponseCache in Apigee Edge allows you to cache *anything*.

What if you don't want to return cached responses for POSTs or PUTs?

It's a simple matter of configuration.

Let's have a look at the policy. In the Administrative Portal for Apigee Edge, if you drag-and-drop a ResponseCache policy into an API Proxy, currently you get a configuration that looks like this:

<ResponseCache name="Response-Cache-1">
    <DisplayName>Response Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <ExpirySettings>
        <ExpiryDate/>
        <TimeOfDay/>
        <TimeoutInSec ref="">3600</TimeoutInSec>
    </ExpirySettings>
    <SkipCacheLookup/>
    <SkipCachePopulation/>
</ResponseCache>

Notice that the SkipCacheLookup and SkipCachePopulate elements are present but empty. What this means is that the cache gets populated and served, for any request, with any verb.

To make sure the cache gets populated and served only for GET or HEAD, you can make a simple change to the configuration, like this:

<ResponseCache name="Response-Cache-1">
    <DisplayName>Response Cache-1</DisplayName>
    <Properties/>
    <CacheKey>
        <Prefix/>
        <KeyFragment ref="request.uri" type="string"/>
    </CacheKey>
    <Scope>Exclusive</Scope>
    <ExpirySettings>
        <ExpiryDate/>
        <TimeOfDay/>
        <TimeoutInSec ref="">3600</TimeoutInSec>
    </ExpirySettings>
  <SkipCacheLookup>NOT (request.verb ~~ \"(GET|HEAD)\")</SkipCacheLookup>
  <SkipCachePopulation>NOT (request.verb ~~ \"(GET|HEAD)\")</SkipCachePopulation>
</ResponseCache>

You can make other changes to this configuration, to set the cache key to include query parameters, or to respect the Accept headers. Read the doc to find out more.

And, here's a video explaining and showing what I just wrote above.

thub.nodes.view.add-new-comment
policiescacheresponse cache
Add comment Show 5
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chockalingam · Jan 24, 2018 at 09:24 PM 1
Link

@Dino How to clear the cache which was created by response cache policy whenever there is an update in the backend data, so that the next time GET requests will hit a cache miss and get the latest value?

avatar image Dino ♦♦ chockalingam   · Jan 24, 2018 at 09:44 PM 0
Link

I can think of two ways

  • allow the cache entry to expire "naturally" according to the ExpirySeconds (eg TimeoutInSec ) that you had configured when populating the cache.
  • Rather than using ResponseCache policy, use the primitive policies PopulateCache and LookupCache. Invoke a PopulateCache policy with an expiry of 1s, and it will overwrite the cache entry and ensure that the cache entry expires within 1 second.
avatar image Mohammad Ilyas Shah Dino ♦♦ · Jan 05 at 02:29 PM 1
Link

@Dino-at-Google

@Dino

In #2, are you suggesting running the Populate Cache policy for this specific case? If yes, then on what req.uri or cache-key, it should run? Can we use a combo of Response Cache and Invalidate Cache policy to achieve this?

Show more comments

Article

Contributors

avatar image

Follow this article

30 People are following this .

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Navigation

Using the ResponseCache policy responsibly! (or, Don't cache POST requests)

Related Articles

Fault Rule and Route Rule processing

Access Control using X-Forwarded-For Header

About patten matching in ExtractVariables policy

Execution time of the policies in a proxy

Using App Attributes to Configure Policies

Tutorial: Using the Quota Policy to enforce access limits on your APIs

How to enforce API Product Quotas - An Example

Apigee 4MV4D - Programming of APIs - Series - Cache & Scale - S01E10

Tutorial: Implementing Response Caching within Edge

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges