Grails加载数据一次

Grails加载数据一次,grails,gorm,Grails,Gorm,我有这两个域名 class Country { String name static hasMany = [cities:City] } class City { String name; static belongsTo = [country: Country] } 这两个表中包含的数据相对较大,它们被用于所有屏幕, 每次我选择一个国家,我都要重新装填它所有的城市 如何在内存中只加载一次数据,以便在所有屏幕中更快地访问数据 我试着把城市放在渴望抓取的

我有这两个域名

class Country {
    String name
    static hasMany = [cities:City]
}

class City {

    String name;
    static belongsTo = [country: Country]
   }
这两个表中包含的数据相对较大,它们被用于所有屏幕, 每次我选择一个国家,我都要重新装填它所有的城市

如何在内存中只加载一次数据,以便在所有屏幕中更快地访问数据

我试着把城市放在渴望抓取的地方,并尝试使用缓存插件


谢谢

您可以将这两个域类配置为自动缓存,也可以将
城市
关系缓存到
国家

class Country {
    String name
    static hasMany = [cities:City]

    static mapping = {
        cache true
        cities cache:true
    }
}

class City {
    String name
    static belongsTo = [country: Country]

    static mapping = {
        cache true
    }
}

缓存通常是一个很好的策略,但请记住缓存有过期参数,因此如果闲置,您的应用程序可能会再次从数据库重新加载。根据您的缓存提供程序,您必须对此进行调优,例如,对于ehcache,在grails config文件夹中编辑您的ehcache.xml并设置(缓存名称与包含包名的域类相同):

用同样的方法取回它们(如

在控制器中:

List<Country> countries = servletContext.countries
List countries=servletContext.countries
或普惠制:

<g:each var="country" in="${application.countries}">
   ${country}
</g:each>

${country}

如果启用了“急切抓取”,则不会看到数据库命中。

选择数据时使用的是什么?如果是条件或列表,请尝试使用
cache:true
,这样数据将从hibernate缓存加载,而不是从databaseno加载。这是一个列表,我如何设置cache:true,您有关于它的链接吗?谢谢。
List<Country> countries = servletContext.countries
<g:each var="country" in="${application.countries}">
   ${country}
</g:each>