【问题标题】:Grails - Implement Debounce Algorithm for AutoComplete Request in ControllerGrails - 在控制器中实现自动完成请求的去抖动算法
【发布时间】:2015-07-11 04:48:15
【问题描述】:

在 Grails 中,我有以下代码 sn-p。

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

我想为来自相同用户、相同请求数据和相同用户 IP 地址的任何传入请求实施去抖动算法。。 p>

我一直在查看implementing debounce in Java 但是,我不知道如何将它与我的 grails 控制器集成。

请帮助我如何在 grails 中实现它?我正在寻找这样的 java 注释实现:

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

【问题讨论】:

    标签: java servlets grails groovy request


    【解决方案1】:

    将最后一次请求数据哈希放入session,并与实际数据进行比较。最简单的方法是:

    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)
    }
    

    如果该东西要在许多地方使用,请将其打包成controller interceptorfilter。如果您需要超时驱动的缓存,请将时间戳添加到您的数据中,该时间戳存储在会话中或使用一些智能缓存解决方案,例如 Google's Guava

    【讨论】:

    • 我认为这不是我想要的。您的回答只会在我当前的实现中增加更多的复杂性。此外,您的答案不是去抖算法的工作方式!
    猜你喜欢
    • 2020-01-26
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2017-07-01
    • 1970-01-01
    • 2022-06-16
    相关资源
    最近更新 更多