Spring Boot Kotlin Coroutine Caching of Http Calls












0















i am trying to use the spring-boot caching mechanism to cache a http response which i get. Currently i have an http client which makes an
rest call for me. I want the result to be cached.



So i created a service which adds a cachable layer:



@Cacheable("logicalTime", sync = true)
open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
return logicalTimeResource.getById(dsId, idLogicalTimes)
}


logicalTimeResource is a proxy to the rest api.



The application framework looks like this:




  • a sheduler which is called every second

  • iterates through a List with entries


  • Calls getLogicalTimeById for each entry in a Coroutine:



    GlobalScope.launch {
    getLogicalTime(element.dsId, element.idLogicalTime
    }




i'd expect that the cache is looked up for the dsId and idLogicalTimes, and when there is an entry for this pair of values, it is returned from the cache. Otherwise logicalTimeResource.getById(dsId, idLogicalTimes)



Now the problem is that logicalTimeResource.getById(dsId, idLogicalTimes) is called for every element in the List.
Yes i have @EnableCaching at my Applicaiton.kt



Edit:



I created a small gist which shows that the cache is not working:
https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8



The main Application looks like this:



package com.example.cachetest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching

@SpringBootApplication
@EnableCaching
class CachetestApplication

fun main(args: Array<String>) {
runApplication<CachetestApplication>(*args)
}


I see both requests hitting my api service on localhost.



Edit v2:



Thanks to Stephane Nicoll pointing out that caching in the same class doesn't work, i updated the gist and moved all calls to a repo class. This looks like this: https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94










share|improve this question

























  • I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

    – Stephane Nicoll
    Nov 20 '18 at 13:31











  • thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

    – H3npi
    Nov 20 '18 at 13:56













  • Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

    – Stephane Nicoll
    Nov 21 '18 at 7:43











  • i'll set up a sample project and send you the link!

    – H3npi
    Nov 21 '18 at 8:29











  • I just added a small example to the main post

    – H3npi
    Nov 21 '18 at 9:09
















0















i am trying to use the spring-boot caching mechanism to cache a http response which i get. Currently i have an http client which makes an
rest call for me. I want the result to be cached.



So i created a service which adds a cachable layer:



@Cacheable("logicalTime", sync = true)
open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
return logicalTimeResource.getById(dsId, idLogicalTimes)
}


logicalTimeResource is a proxy to the rest api.



The application framework looks like this:




  • a sheduler which is called every second

  • iterates through a List with entries


  • Calls getLogicalTimeById for each entry in a Coroutine:



    GlobalScope.launch {
    getLogicalTime(element.dsId, element.idLogicalTime
    }




i'd expect that the cache is looked up for the dsId and idLogicalTimes, and when there is an entry for this pair of values, it is returned from the cache. Otherwise logicalTimeResource.getById(dsId, idLogicalTimes)



Now the problem is that logicalTimeResource.getById(dsId, idLogicalTimes) is called for every element in the List.
Yes i have @EnableCaching at my Applicaiton.kt



Edit:



I created a small gist which shows that the cache is not working:
https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8



The main Application looks like this:



package com.example.cachetest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching

@SpringBootApplication
@EnableCaching
class CachetestApplication

fun main(args: Array<String>) {
runApplication<CachetestApplication>(*args)
}


I see both requests hitting my api service on localhost.



Edit v2:



Thanks to Stephane Nicoll pointing out that caching in the same class doesn't work, i updated the gist and moved all calls to a repo class. This looks like this: https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94










share|improve this question

























  • I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

    – Stephane Nicoll
    Nov 20 '18 at 13:31











  • thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

    – H3npi
    Nov 20 '18 at 13:56













  • Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

    – Stephane Nicoll
    Nov 21 '18 at 7:43











  • i'll set up a sample project and send you the link!

    – H3npi
    Nov 21 '18 at 8:29











  • I just added a small example to the main post

    – H3npi
    Nov 21 '18 at 9:09














0












0








0








i am trying to use the spring-boot caching mechanism to cache a http response which i get. Currently i have an http client which makes an
rest call for me. I want the result to be cached.



So i created a service which adds a cachable layer:



@Cacheable("logicalTime", sync = true)
open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
return logicalTimeResource.getById(dsId, idLogicalTimes)
}


logicalTimeResource is a proxy to the rest api.



The application framework looks like this:




  • a sheduler which is called every second

  • iterates through a List with entries


  • Calls getLogicalTimeById for each entry in a Coroutine:



    GlobalScope.launch {
    getLogicalTime(element.dsId, element.idLogicalTime
    }




i'd expect that the cache is looked up for the dsId and idLogicalTimes, and when there is an entry for this pair of values, it is returned from the cache. Otherwise logicalTimeResource.getById(dsId, idLogicalTimes)



Now the problem is that logicalTimeResource.getById(dsId, idLogicalTimes) is called for every element in the List.
Yes i have @EnableCaching at my Applicaiton.kt



Edit:



I created a small gist which shows that the cache is not working:
https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8



The main Application looks like this:



package com.example.cachetest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching

@SpringBootApplication
@EnableCaching
class CachetestApplication

fun main(args: Array<String>) {
runApplication<CachetestApplication>(*args)
}


I see both requests hitting my api service on localhost.



Edit v2:



Thanks to Stephane Nicoll pointing out that caching in the same class doesn't work, i updated the gist and moved all calls to a repo class. This looks like this: https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94










share|improve this question
















i am trying to use the spring-boot caching mechanism to cache a http response which i get. Currently i have an http client which makes an
rest call for me. I want the result to be cached.



So i created a service which adds a cachable layer:



@Cacheable("logicalTime", sync = true)
open fun getLogicalTimeById(dsId: String, idLogicalTimes: String): LogicalTime {
return logicalTimeResource.getById(dsId, idLogicalTimes)
}


logicalTimeResource is a proxy to the rest api.



The application framework looks like this:




  • a sheduler which is called every second

  • iterates through a List with entries


  • Calls getLogicalTimeById for each entry in a Coroutine:



    GlobalScope.launch {
    getLogicalTime(element.dsId, element.idLogicalTime
    }




i'd expect that the cache is looked up for the dsId and idLogicalTimes, and when there is an entry for this pair of values, it is returned from the cache. Otherwise logicalTimeResource.getById(dsId, idLogicalTimes)



Now the problem is that logicalTimeResource.getById(dsId, idLogicalTimes) is called for every element in the List.
Yes i have @EnableCaching at my Applicaiton.kt



Edit:



I created a small gist which shows that the cache is not working:
https://gist.github.com/H3npi/799df85d4570e3cbe8b02ede24bea5a8



The main Application looks like this:



package com.example.cachetest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching

@SpringBootApplication
@EnableCaching
class CachetestApplication

fun main(args: Array<String>) {
runApplication<CachetestApplication>(*args)
}


I see both requests hitting my api service on localhost.



Edit v2:



Thanks to Stephane Nicoll pointing out that caching in the same class doesn't work, i updated the gist and moved all calls to a repo class. This looks like this: https://gist.github.com/H3npi/af37ea97ea3450deeca2ab8933072c94







spring-boot kotlin spring-cache kotlin-coroutines






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:26







H3npi

















asked Nov 20 '18 at 12:34









H3npiH3npi

308




308













  • I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

    – Stephane Nicoll
    Nov 20 '18 at 13:31











  • thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

    – H3npi
    Nov 20 '18 at 13:56













  • Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

    – Stephane Nicoll
    Nov 21 '18 at 7:43











  • i'll set up a sample project and send you the link!

    – H3npi
    Nov 21 '18 at 8:29











  • I just added a small example to the main post

    – H3npi
    Nov 21 '18 at 9:09



















  • I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

    – Stephane Nicoll
    Nov 20 '18 at 13:31











  • thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

    – H3npi
    Nov 20 '18 at 13:56













  • Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

    – Stephane Nicoll
    Nov 21 '18 at 7:43











  • i'll set up a sample project and send you the link!

    – H3npi
    Nov 21 '18 at 8:29











  • I just added a small example to the main post

    – H3npi
    Nov 21 '18 at 9:09

















I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

– Stephane Nicoll
Nov 20 '18 at 13:31





I'd remove sync, you don't want to pay the cost of the synchronization just to avoid two concurrent HTTP calls on the same tuple. It's impossible to say what is going on with the details you've given. What is the cache library? Have you looked at the auto-configuration report that caching is active?

– Stephane Nicoll
Nov 20 '18 at 13:31













thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

– H3npi
Nov 20 '18 at 13:56







thanks for the sync note! Currently i am using the default cache library, no additional configuration done. The cache should be working, because i see a ] o.s.c.a.AnnotationCacheOperationSource : Adding cacheable method 'getLogicalTimeById' with attribute: in my log

– H3npi
Nov 20 '18 at 13:56















Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

– Stephane Nicoll
Nov 21 '18 at 7:43





Sorry I can't help you with the details you've shared. Perhaps you can share a small sample on github that reproduces the problem. If the cache isn't working, it should be quite easy to replicate.

– Stephane Nicoll
Nov 21 '18 at 7:43













i'll set up a sample project and send you the link!

– H3npi
Nov 21 '18 at 8:29





i'll set up a sample project and send you the link!

– H3npi
Nov 21 '18 at 8:29













I just added a small example to the main post

– H3npi
Nov 21 '18 at 9:09





I just added a small example to the main post

– H3npi
Nov 21 '18 at 9:09












1 Answer
1






active

oldest

votes


















0














The problem was: My Component was not autowired. Autowiring fixed the issue.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393116%2fspring-boot-kotlin-coroutine-caching-of-http-calls%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The problem was: My Component was not autowired. Autowiring fixed the issue.






    share|improve this answer




























      0














      The problem was: My Component was not autowired. Autowiring fixed the issue.






      share|improve this answer


























        0












        0








        0







        The problem was: My Component was not autowired. Autowiring fixed the issue.






        share|improve this answer













        The problem was: My Component was not autowired. Autowiring fixed the issue.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 13:36









        H3npiH3npi

        308




        308






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393116%2fspring-boot-kotlin-coroutine-caching-of-http-calls%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]