【问题标题】:what is the StandardQueryCache alternative in Hibernate 5.4.4Hibernate 5.4.4 中的 StandardQueryCache 替代方案是什么
【发布时间】:2020-01-09 05:50:55
【问题描述】:

我正在使用以下代码进行缓存。

 public class SuiteStandardQueryCache extends StandardQueryCache {

    public SuiteStandardQueryCache(Settings settings, Properties props, UpdateTimestampsCache updateTimestampsCache, String regionName)
                                                                                                                                       throws HibernateException {
        super(settings, props, updateTimestampsCache, regionName);
    }

    public boolean put(QueryKey key, Type[] returnTypes, List result, boolean isNaturalKeyLookup, SessionImplementor session) throws HibernateException {
        if (isNaturalKeyLookup && result.size() == 0) {
            return false;
        } else {
            Long ts = new Long(session.getTimestamp());
            List cacheable = new ArrayList(result.size() + 1);
            cacheable.add(ts);
            for (int i = 0; i < result.size(); i++) {
                if (returnTypes.length == 1) {
                    // cacheable.add( returnTypes[0].disassemble( result.get( i
                    // ), session, null ) );
                    Object resultObj = result.get(i);
                    if (resultObj instanceof Object[]) {
                        resultObj = ((Object[]) result.get(i))[0];
                    }
                    cacheable.add(returnTypes[0].disassemble(resultObj, session, null));
                } else {
                    cacheable.add(disassemble((Object[]) result.get(i), returnTypes, null, session, null));
                 }
            }
            getRegion().put(key, cacheable);
            return true;
        }
    }


    public static Serializable[] disassemble(final Object[] row,

        return disassembled;
    }
}

这段代码是 Hibernate 3 的休眠代码,我将把它升级到 Hibernate 5.4.4,其中没有这个类。

请帮助找出相同的替代方案。

【问题讨论】:

  • 看起来这段代码是为了解决特定问题或错误而编写的,可能是在 Hibernate 的早期版本中。您首先需要找出为什么要写这个
  • @Guillaume 有可能,但我只是在寻找 StandardQueryCache 类,这个类的替代品是什么。

标签: java hibernate caching


【解决方案1】:

可以使用您选择的任何二级缓存提供程序来配置查询缓存。 Hibernate 默认没有二级缓存,你必须配置并提供一个(Default cache used by Hibernate?)。

例如,我使用 EhCache 作为二级缓存。我通过 XML 配置如下:

<cache-template name="default">
    <key-type>java.lang.Object</key-type>
    <value-type>java.lang.Object</value-type>

    <expiry>
      <none/>
    </expiry>

    <heap unit="entries">100000</heap>
</cache-template>

<!-- Configure the caches that Hibernate uses by default. -->
<cache alias="default-query-results-region" uses-template="default" />
<cache alias="default-update-timestamps-region" uses-template="default" />

我相信这个缓存使用的 Java 类最终是 org.ehcache.jsr107.Eh107Cache.Eh107Cache,因为我的缓存配置。

如果您想按照上述添加自定义逻辑,我相信您要做的是为 hibernate.javax.cache.provider 配置属性设置自定义缓存提供程序。我当前的值为org.ehcache.jsr107.EhcacheCachingProvider。覆盖该类以提供一个缓存管理器,它可以根据您的需要构建缓存。

【讨论】:

  • 感谢您的回答,但我想继续使用以前的代码,只是不想更改我的代码可能会影响其他应用程序领域,我正在寻找一些代码库StandardQueryCache 的替代解决方案,不幸的是我没有得到任何相同的线索。
  • 我的意思是,据我所知,没有替代品——它只是使用标准缓存(无论你告诉它)。没有专门的查询缓存类。
  • 好的,我应该在哪里配置这个,意味着它会去哪个 xml 文件,我是 Hibernate 的新手 :) 另外请告诉我 QueryCacheFactory 接口,这也是从 hibernate 5.4.4 中提取的
猜你喜欢
  • 2021-11-13
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多