简体   繁体   English

使用hibernate和ehcache进行本机SQL缓存

[英]Native SQL Caching with hibernate and ehcache

如何缓存已执行的本机SQL的结果,查询本身的结果可以是数字或简单字符串,因此没有匹配的实体。

In you queries you should use query.setHint("org.hibernate.cacheable", "true"); 在查询中,应使用query.setHint(“ org.hibernate.cacheable”,“ true”); for example: 例如:

javax.persistence.Query query =  entityManager.createQuery("...");
query.setHint("org.hibernate.cacheable", "true");

also having ehcache.xml eg 也有ehcache.xml例如

<ehcache>
    <diskStore path="java.io.tmpdir"/>

    <defaultCache 
        maxElementsInMemory="100"
        maxElementsOnDisk="500"
        external="false"
        overflowToDisk="true" 
        memoryStoreEvictionPolicy="LRU"
    />

.....
</ehcache>

and of course persistence.xml eg 当然还有persistence.xml例如

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>...</class>
        <properties>
            <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.provider_configuration_file_resource_path" value="/ehcache.xml"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>

.....
        </properties>
    </persistence-unit>
</persistence>

I use Heimdall Data. 我使用Heimdall Data。 It acts as a transparent sql cache. 它充当透明的sql缓存。 The product offers caching recommendations leaving the guesswork out for the developer. 该产品提供了缓存建议,将猜测工作留给了开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM