• hibernate缓存清除(转)


    文章有点杂,这不是原文,谢谢贡献者

    http://www.360doc.com/content/16/0413/16/32415095_550307388.shtml

    一、hibernate一级缓存
    (1)hibernate支持两个级别的缓存,默认只支持一级缓存;

    (2)每个Session内部自带一个一级缓存;
    (3)某个Session被关闭时,其对应的一级缓存自动清除;
    (4)save、update、saveOrupdate、load、get、list、iterate、lock方法都会向缓存中存对象.
    (5)可以从缓存中读数据的只有: get、load、iterate
    (6)Query对象默认情况下不读缓存,如果要使其支持缓存,则要通过语法:
    query.setCacheable(true);
    <property name="cache.use_query_cache">true</property>
    (7)打开query缓存后,只有查询条件与以前的查询完全相同时,才会在缓存中匹配成功.
    (8)Criteria对缓存支持不足;
    (9)一级缓存不能控制缓存中的对象数量,要注意大批量操作数据时可能造成的内存溢出,可以利用清除缓存.
         session.clear()  清除缓存中所有对象
         session.evict(user) 清除指定对象


    二、hibernate二级缓存
    * save、update、saveOrupdate、load、get、list、query、Criteria方法都会填充二级缓存
    * get、load、iterate会从二级缓存中取数据
    * session.save(user)   如果user主键使用“native”生成,则不放入二级缓存.

    (1)开启二级缓存
    <property name="cache.use_second_level_cache">true</property>

    (2)为hibernate指定二级缓存的实现类
    <property name="cache.provider_class">
        org.hibernate.cache.OSCacheProvider
    </property>

    (3)为OSCache缓存创建配置文件(需要hibernate_Advance_Surpport_lib)
    src/oscache.properties
    修改配置中的:
    cache.capacity=1000  指定缓存可以容纳多少对象


    (4)指明哪些类需要放入二级缓存,需要长期使用到的对象才有必要放入二级缓存
    <class-cache class="entity.PetInfo" usage="read-only" /> //不允许更新缓存中的对象
    <class-cache class="entity.PetInfo" usage="read-write" />  //允许更新缓存中的对象

    或在orm文件中:
    <class name="entity.PetInfo" table="PetInfo" schema="dbo" catalog="epet">
       <cache usage="read-only"/>
       ...
    </class>

    (5)如果需要清除二级缓存,使用下面语法
        sessionFactory.evict(User.class)  清除所有user
        sessionFactory.evict(User.class,Id)  清除指定user
  • 相关阅读:
    [Visual Studio] [Config] [Transformation] [SlowCheetah] 在非Web工程中使用Transformation
    [SQLSERVER] 把TransactionLog截断
    [Windows] [Firewall] 增加进入规则
    [POWERSHELL] [.net 3.5] [Windows Server] 在Windows Server上安装.NET3.5
    杂碎
    VSCode 使用Settings Sync同步配置(最新版教程,非常简单)
    JavaScript:ES2019 的新特性
    重新认识构造函数、原型和原型链
    如何实现 React 中的状态自动保存?
    深拷贝
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5387567.html
Copyright © 2020-2023  润新知