如何在 Grails 中暂时禁用只读二级缓存休眠策略?

发布于 2024-08-20 22:53:55 字数 326 浏览 14 评论 0 原文

在我的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄟ。诗瑗 2024-08-27 22:53:55

这可能是可能的,但很可能不是微不足道的。我会使用 groovy.sql.Sql 直接插入。您失去了验证,但您可以创建实例并验证它们,但不能调用 save()。如果没问题的话就进行 SQL 插入,例如

def thing = new Thing(params)
if (thing.validate()) {
   new Sql(dataSource).executeInsert(
             "insert into thing(name) values(?)", [params.name])
}
else {
   // report validation error
}

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.

def thing = new Thing(params)
if (thing.validate()) {
   new Sql(dataSource).executeInsert(
             "insert into thing(name) values(?)", [params.name])
}
else {
   // report validation error
}
不乱于心 2024-08-27 22:53:55

我将使用纯 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 the SessionFactory to remove the specific entities from the 2nd level cache. Note that you may have to remove entities from collections also with evictCollection(). 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文