Home > Software design >  How should I cache the response from another API?
How should I cache the response from another API?

Time:04-14

Right now, I'm making three requests for an external API, and each time, I have to iterate through the responses and find what I have to. How can I cache the response from this external API, that has 3 different end points that I use?

Here's the iteration:

      if(list.getData() != null) {
        list.getData().stream().forEach(listItems -> {
        if(items.getAttributes().getItems() != null)
          items.getAttributes().getItems().forEach(listItem -> {
          if(item.getListItems() != null)
            item.getListItems().forEach(k -> {
              if(k.getItems() != null)
              k.getItems().forEach( l -> {
                if (l.getName().equalsIgnoreCase(name)) {
                  description.set(l.getDescription());
                  return;
                }});
              });
            });
          });
      }

where "list" is the actual response from the API.

Thanks

CodePudding user response:

Try using Springs Cache Abstraction, https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache.

CodePudding user response:

try using @Cachable which can be used on method or class level

  • Related