jOOQ和缓存? [英] jOOQ and Caching?

查看:408
本文介绍了jOOQ和缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑从Hibernate转移到jOOQ,但我不确定如果没有缓存我是否可以做到。 Hibernate有一个一级和二级缓存 。我知道jOOQ确实有支持重复使用准备好的声明

I am considering moving from Hibernate to jOOQ but I am not sure if I can do without caching. Hibernate has a first- and second-level cache. I know that jOOQ does have support for reusing prepared statements.

如果我使用jOOQ,我是否必须自己处理缓存?

Will I have to take care of caching on my own if I use jOOQ?

推荐答案

查询缓存/结果缓存:



我提到这个,因为这种缓存是也可以使用Hibernate ,它可能在某些情况下有意义。

Query caching / result caching:

I'm mentioning this, because this kind of cache is also possible with Hibernate, and it might make sense under certain circumstances.

在Hibernate中,查询缓存与二级缓存密切配合。在jOOQ中,您可以使用jOOQ VisitListener API实现拦截所有查询的查询缓存。有一些关于这个主题的博客文章:

In Hibernate, the query cache works closely with the second-level cache. In jOOQ, you can implement a query cache that intercepts all queries using the jOOQ VisitListener API. There are some blog articles about this topic:

  • Hack up a Simple JDBC ResultSet Cache Using jOOQ’s MockDataProvider
  • Caching in Java with JOOQ and Redis

将来会有更好的支持这种类型的缓存(不是在jOOQ 3.7中),因为这种缓存属于SQL API。请注意,您的JDBC驱动程序可能也支持这种缓存 - 例如ojdbc确实。

There will be better support for this type of cache in the future (not in jOOQ 3.7, yet), as this kind of cache belongs in a SQL API. Note that your JDBC driver might also support this kind of cache already - e.g. ojdbc does.

Hibernate的第一级缓存背后的想法没有意义使用更像SQL的API,如jOOQ。 SQL是一种高度复杂的语言,可以在实际持久化的实体和相同实体的客户端表示之间起作用。这意味着一旦使用SQL,您可能会创建与数据的原始实体表示无关的特殊元组。

The idea behind Hibernate's first level cache doesn't make sense with a more SQL-oriented API like jOOQ. SQL is a highly complex language that works between the actually persisted entities, and your client representation of the same entities. This means that once you use SQL, you will probably create ad-hoc tuples that have nothing to do with the original entity representation of your data.

换句话说:首先只有在限制查询语言的功能和范围时才能实现级别缓存,并且如果您控制 ALL 数据库交互,Hibernate的方式就是这样。 jOOQ明确不这样做。

In other words: First-level caching is a thing that is only possible if you limit the functionality and scope of your query language, and if you take control over ALL your database interactions, the way Hibernate does it. jOOQ expressly doesn't do that.

Hibernate中的二级缓存是一个缓存,主要用于主数据和类似的数据类型,从数据库中提取数据几乎没有意义,因为数据不会改变。

The second-level cache in Hibernate is a cache that is mostly useful with master data and similar types of data where fetching the data from the database hardly ever makes sense, as the data doesn't change.

没有任何理由,为什么ORM应该实现这种缓存,在简单的情况下不方便。但在许多情况下,最好用 @Cacheable ,例如如本春季页面所述。这将是更高层上的缓存,而不是ORM /查询层上的缓存,您可以将缓存与业务逻辑更紧密地关联。

There is no reason at all, why an ORM should implement this kind of cache, short of convenience in simple cases. But in many cases, you're better off annotating a service method with @Cacheable, e.g. as documented here on this Spring page. This would be a cache on a higher layer than on the ORM / query layer, where you will be able to correlate caching much more tightly with your business logic.

这篇关于jOOQ和缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆