将整个表加载到缓存 Grails 中
是否可以在 Grails 启动时将整个表加载到缓存中?
例如,我有 2 个表,每个表有 5000 条记录,用作静态只读数据。不过,该数据受到的打击最为严重,因为其他表上的所有信息都源自该只读表。
我知道 grails 有一个缓存使用场景,但是在很短的时间后,这些信息会不断地从缓存中被逐出,并且它也只会在下一个请求时重新缓存。
基本上试图通过不必访问该静态数据的数据库来减少响应时间。
谢谢
Is it possible to load an entire table into cache on Grails startup?
For example I have a 2 tables with 5000 records each that is used as static read only data. This data is the most hard hit though since all information on other tables is derived from this read-only table.
I know grails has a cache usage scenario but this infomation is constantly being evicted from the cache after a short amount of time and it also only gets re-cached on the next request.
Basically trying to reduce response times by not having to access the database for this static data.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 ehcache.xml 配置缓存行为。如果您没有,缓存将配置为默认值,但如果您有,则会使用它。将其放入
grails-app/conf
中,它将被复制到类路径中。假设您的域类是
com.yourcompany.yourapp.YourDomainClass
,您可以指定要缓存的元素数量并设置永恒= true,这样它们就不会被丢弃:有关如何配置的更多信息
ehcache.xml
请参阅 http://ehcache.org/ehcache.xml ,其中有很多文档评论。完成此操作后,您的
BootStrap.groovy
应该如下所示:为每个实例调用
get()
后,将来调用get()
将使用二级缓存。You can configure the cache behavior with ehcache.xml. If you don't have one the caches are configured with default values but if you do it's used instead. Put it in
grails-app/conf
and it will be copied to the classpath.Assuming your domain class is
com.yourcompany.yourapp.YourDomainClass
, you can specify the number of elements to cache and set eternal = true so they aren't discarded:For more information about how to configure
ehcache.xml
see http://ehcache.org/ehcache.xml which has a lot of documentation in the comments.Having done that, your
BootStrap.groovy
should look something like this:Having called
get()
for each instance, future calls toget()
will use the 2nd-level cache.