在我的 grails 应用程序中,我的一些域类永远不会被用户更改。
但是,有时需要进行一些维护工作,并且管理员应该能够不时创建/编辑几个实例(假设每年两次)。
我想为这些域类设置只读二级缓存策略(静态映射= {缓存使用:'只读'})并且我希望能够“禁用” (在非常特殊的情况下)只读策略,以便通过 Grails 脚手架编辑视图更新某些实例。
是否可以?你建议我做什么?
编辑:我正在实施的解决方案是 Pascal 和 Burt 答案的混合(请参阅评论)。这两个答案都很棒并且很有帮助。所以我在选择接受的答案时遇到了两难!不管怎样,谢谢你。
In my grails application, some of my domain classes will never be changed by Users.
However, some maintenance work is sometimes necessary, and administrator should be able to create/edit few instances from time to time (let's say twice a year).
I would like to set a read-only 2nd level cache strategy for these domain classes (static mapping = { cache usage: 'read-only' }
) AND I would like to be able to 'disable' (in very particular situations) the read-only strategy in order to udate some instances via Grails scaffolding edit view.
Is it possible? What do you advise me to do?
EDIT: The solution I am implementing is a mix of Pascal and Burt answers (see comments). Both answers are great and helpful. So I got a dilemna for choosing the accepted answer! Anyway, thank you.
发布评论
评论(2)
这可能是可能的,但很可能不是微不足道的。我会使用 groovy.sql.Sql 直接插入。您失去了验证,但您可以创建实例并验证它们,但不能调用 save()。如果没问题的话就进行 SQL 插入,例如
It's probably possible but most likely non-trivial. I'd go with direct inserts using groovy.sql.Sql. You lose validation, but you could create instances and validate them but not call save(). Then do the SQL inserts if they're ok, e.g.
我将使用纯 SQL 对给定实体执行更新,然后对 evict() 方法调用.org/hib_docs/v3/api/org/hibernate/SessionFactory.html" rel="nofollow noreferrer">
SessionFactory
从二级缓存中删除特定实体。请注意,您可能还必须使用evictCollection()
从集合中删除实体。查看这篇不错的博客文章 有关驱逐的详细信息。将所有这些封装在服务中(例如
wipeBooksFromGlobalCache()
),以便管理员可以在您提到的非常特殊的情况下调用。I would perform the update on the given entities using pure SQL and then make the required
evict()
method calls on theSessionFactory
to remove the specific entities from the 2nd level cache. Note that you may have to remove entities from collections also withevictCollection()
. Check this nice blog post for details on eviction.Encapsulate all this in services (e.g.
wipeBooksFromGlobalCache()
) than admins can call in the very particular situations you are mentioning.