简体   繁体   English

如何获取对Spring @Cacheable数据的访问权限?

[英]How to get acces to Spring @Cacheable data?

I have following code and I want to get data in "instagramSearchResultsCache". 我有以下代码,我想在“ instagramSearchResultsCache”中获取数据。 How can I do this using Spring Cache for example to print it ? 例如,如何使用Spring Cache进行打印?

@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;
    }

You should not be accessing this data from code in a prod setting as that would defeat the purpose of the abstraction. 您不应该在prod设置中从代码访问此数据,因为那样会违背抽象的目的。 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. 例如,对于redis,您可以使用RedisConnection按某种模式搜索键。 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. 如果数据已经存在,则不会发生方法调用,并且将从缓存中返回您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM