Tomek Bieniek
Tomek Bieniek

Reputation: 13

How to get acces to Spring @Cacheable data?

I have following code and I want to get data in "instagramSearchResultsCache". How can I do this using Spring Cache for example to print it ?

@Cacheable(value = "instagramSearchResultsCache", key = "#tagName")
    public ArrayList<SingleInstagramDTO> getInstagramData(String tagName) {

        JSONObject jsonObject = sendGETRestTemplate(tagName);
        if (jsonObject == null) {
            return null;
        }

        JSONArray arr = jsonObject.optJSONArray("data");
        ArrayList<SingleInstagramDTO> instagramRestObjectsList = new ArrayList<>();

        for (int i = 0; i < arr.length(); i++) {

            JSONObject jsonElement = arr.optJSONObject(i);

            InstagramFormatter formatter = new InstagramFormatter(jsonElement);
            JSONObject instagramJSONObject = formatter.getResultInstagramObject();

            instagramRestObjectsList.add(new SingleInstagramDTO(instagramJSONObject));
        }

        return instagramRestObjectsList;
    }

Upvotes: 0

Views: 1038

Answers (1)

Justin ross
Justin ross

Reputation: 79

You should not be accessing this data from code in a prod setting as that would defeat the purpose of the abstraction. If you just wanted to see inside for testing, it would depend on your cache provider. For example with redis you could use a RedisConnection to search keys by some pattern. But again this is highly dependent on provider and is never recommended or needed as the propose of the annotation is so you do not have to worry about accessing the data location manually. If the data already exists, method invocation will not occur and your data will be returned from the cache.

Upvotes: 0

Related Questions