0

In Grails, I have the following code snippet.

class AutoCompleteController {
   def getAutoCompleteData() {
       // Open db connection and get the data
       // and do other long and heavy computation
       jsonString(data: result)
   }
}

I want to implement a debounce algorithm for any incoming request if it comes from the same user, the same request data, and the same user's IP address.

I have been looking at implementing debounce in Java However, I don't have any idea how to integrating it with my grails controller.

Please help me how to implement it in grails? I am looking for implementation with java annotation something like this:

class AutoCompleteController {
   @Debounce(delay = 1000) // ------------>>>>>> How to implement this annotation?
   def getAutoCompleteData() {
       // heavy logic
       jsonString(data: result)
   }
}
Community
  • 1
  • 1
rhzs
  • 516
  • 7
  • 24

1 Answers1

0

Put the last request data hash into the session and compare it to the actual data. The simplest approach would be:

def getAutoCompleteData() {
   int hash = hashTheRelevantParams()
   if( null == session.cache ) session.cache = [:]
   def result = session.cache[ hash ] ?: getResultTheHardWay() 
   session.cache.clear()
   session.cache[ hash ] = result
   jsonString(data: result)
}

If the thing should be used in many locations, pack it into a controller interceptor or a filter. If you need the timeout-driven cache, add a timestampt into your data, that is stored in the session or use some smart caching solution, like Google's Guava

injecteer
  • 20,038
  • 4
  • 45
  • 89
  • I don't think this is what I am looking for. Your answer will just add more complexity in my current implementation. In addition, your answer is not the way how debounce algorithm works! – rhzs Jul 12 '15 at 06:24