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,082
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

HI @nsghir

Are you storing each array item into Cache with key as their id ?

My first answer will be Yes, you can. However, can you explain the whole use case as to why you want to store them, we could probably discuss other possible solutions as well

Yeah that's what I want to do.

Daily, the api will receive a full of JSON objects, and I want to save them in cache to facilitate access with get queries.

I don't want to use dbs to store them, because the lifetime of this data is 24 hours only.

Not applicable

Hello @nsghir,

First you need to create objects with id, and then you need to put each of the objects in the cache with the Key as the id.

You can use a javascript to write your own code to construct the object, Populate cache policy to store those objects and Lookup cache policy to retreive the objects (if business case needs it).

Hope this helps.!

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.

thank you very much @Anil Sagar

Anytime 🙂 Glad it helped.

Hi again Anil,

I have a little problem that I can't resolve:

I am using a Populate Cache Policy to store a JSON object in Cache. This seems to work good.

When I try to retrieve it with a LookupCache policy, only one in two calls returns a value (same request).

Here are my 2 policies:

Populate Cache Policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><PopulateCacheasync="false"continueOnError="false"enabled="true"name="Populate-Cache"><DisplayName>Populate Cache</DisplayName><Properties/><CacheKey><KeyFragment>mykey</KeyFragment></CacheKey><CacheResource>MyFirstCacheResource</CacheResource><Scope>Exclusive</Scope><ExpirySettings><TimeoutInSec>172800</TimeoutInSec></ExpirySettings><Source>MyJSONObjectVariable</Source></PopulateCache>

LookupCache Policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><LookupCacheasync="false"continueOnError="false"enabled="true"name="Lookup-Cache"><DisplayName>Lookup Cache</DisplayName><Properties/><CacheKey><KeyFragment>mykey</KeyFragment></CacheKey><CacheResource>MyFirstCacheResource</CacheResource><Scope>Exclusive</Scope><AssignTo>MyJSONVariable</AssignTo></LookupCache>

In the trace tool, for the same query:

CALL 1 :

3632-ok.png

CALL 2:

3634-ko.png

Recap:

3635-problem.png

I find that a little bit weird.

Can you help me to fix the problem please?

@Nader Sghir, Onprem / oncloud ? Looks like mp issue !

sorry @Anil Sagar,

I don't understand your question