Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Cache JSON Array with keys

Not applicable

Hello,

I would like to know if it is possible to use Apigee Cache to store JSON objects.

The key for the cache is the id:

Example:

{
  "items": [
    {
      "id": "413711000",
      "value1": "aaa",
      "value2": "bbb"
     
    },
    {
      "id": "213219010",
      "value1": "ccc",
      "value2": "ddd"
    },
    {
      "id": "759629010",
      "value1": "eee",
      "value2": "ffff"
    }
  ]
}

Thank you

Solved Solved
0 9 3,521
1 ACCEPTED SOLUTION

Dear @nsghir ,

Great Question & Interesting use case,

Yes, It's definitely possible & quick to implement same in Apigee Edge. It's tricky to create individual cache entries in Apigee Edge for each id. Find out why below,

  • Target response contains JSON with multiple items. Even though you can retrieve each item using javascript, you cannot run policy like Populate cache in a loop to store different id's (items) into different cache entries. As of today, Apigee doesn't support looping policy.
  • One more option is, Use a service callout to populate cache & call same using Javascript in a loop. Not a great solution since if your response has 100 items , you will make 100 API calls to the proxy which populates the cache. Not a great way to design same.

A simple way to solve above query is, Store the entire response as cache in Apigee & filter results on a get call using the Extract Variable Policy & Javascript Policy. Take a look at below video that explains how above problem is solved using different policies in Apigee Edge. Attached different proxies used in the video below for your reference.

Mock Target - jsonkeys-rev1-2016-10-01.zip

Populate Cache - populatecache-rev2-2016-10-01.zip

Get Json Response By Key - getvaluesbykey-rev1-2016-10-01.zip

Additionally, You can enable response cache on above proxy to cache individual items in Apigee cache so that you don't need to retrieve full payload & parse JSON response every time. I missed this in above video, but you can quickly implement same. Hope it helps. Keep us posted if any.

View solution in original post

9 REPLIES 9
Top Solution Authors