gpt4 book ai didi

grails - 应用范围图

转载 作者:行者123 更新时间:2023-12-02 14:34:17 24 4
gpt4 key购买 nike

我希望 quartz 作业定期填充 map (通过Web服务调用),然后从Web层访问该 map (以显示给用户)。

我本来只是想做一项服务(假设一项服务是单例的),但我对the recommendation not to store state in a service有点担心。

class MapService {
def map = [:]
}

做这个的最好方式是什么?

最佳答案

如果您不担心在应用程序运行时将数据保留在内存中,则可以在服务中使用ConcurrentHashMap并存储所需的数据。虽然将其视为高速缓存而不是可靠的存储。

就像您还说的那样,服务是单例only one instance of the service ever exists,而concurrentMapA hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.
例如:

calss MyService() {

ConcurrentHashMap cacheMap = [:]

def retrieveCache(String key) {
cacheMap[(key)]
}

def resetCache(){
cacheMap = [:]
}

def doSomething(){
..
cacheMap.put(key,value)
}

}

类似的 post

关于grails - 应用范围图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963383/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com