Hibernate 再接触 一级缓存 二级缓存 查询缓存

缓存 就是把本来应该放在硬盘里的东西放在内存里  将来存内存里读

 

一级缓存:

session缓存

二级缓存: sessionFactory级别的   (适合经常访问,数据量有限,改动不大)

很多的session缓存  找不到再去数据库拿

开启二级缓存

查询缓存 比如说 我查的是2-8的数据 然后呢我又查了3-9的数据 则3-8的数据就是查询缓存,三级缓存

缓存算法

内存对象满了 拿走什么

 

Lru Lfu fifo

1.最近很少被使用

2.最近不常被使用

3.缓存对象设置一个数组 来一个把第一个推走

例子

Category.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
//@BatchSize(size=5)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Category {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Msg.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Msg {
    private int id;
    private String cont;
    private Topic topic;
    @ManyToOne
    public Topic getTopic() {
        return topic;
    }
    public void setTopic(Topic topic) {
        this.topic = topic;
    }
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    public String getCont() {
        return cont;
    }
    public void setCont(String cont) {
        this.cont = cont;
    }
    
}

Msginfo.java

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;


public class MsgInfo {
    private int id;
    private String cont;
    private String topicName;
    private String categoryName;
    public MsgInfo(int id, String cont, String topicName, String categoryName) {
        super();
        this.id = id;
        this.cont = cont;
        this.topicName = topicName;
        this.categoryName = categoryName;
    }
    public String getTopicName() {
        return topicName;
    }
    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    
    public String getCont() {
        return cont;
    }
    public void setCont(String cont) {
        this.cont = cont;
    }
    
}

Topic.java

package com.bjsxt.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.hibernate.annotations.BatchSize;

@Entity
@NamedQueries(
        {
            @NamedQuery(name="topic.selectCertainTopic", query="from Topic t where t.id = :id")
        }
        )
        /*
@NamedNativeQueries(
        {
            @NamedNativeQuery(name="topic.select2_5Topic", query="select * from topic limit 2, 5")
        }
        )
        */

public class Topic {
    private int id;
    private String title;
    private Category category;
    private Date createDate;
    private List<Msg> msgs = new ArrayList<Msg>();
    @OneToMany(mappedBy="topic")
    public List<Msg> getMsgs() {
        
        return msgs;
    }
    public void setMsgs(List<Msg> msgs) {
        this.msgs = msgs;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    @ManyToOne    
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
}
@Test
    public void testCache1() {
        Session session = sf.openSession();
        session.beginTransaction();
        Category c = (Category)session.load(Category.class, 1);
        System.out.println(c.getName());
        
        Category c2 = (Category)session.load(Category.class, 1);   //一级缓存
        System.out.println(c2.getName());
        session.getTransaction().commit();
        session.close();
        
    }
    
    //join fetch
    @Test
    public void testCache2() {
        Session session = sf.openSession();
        session.beginTransaction();
        Category c = (Category)session.load(Category.class, 1);
        System.out.println(c.getName());
        
        
        session.getTransaction().commit();
        session.close();
        
        Session session2 = sf.openSession();
        session2.beginTransaction();
        Category c2 = (Category)session2.load(Category.class, 1); //两个session 打开二级缓存 也只发送一次select
        System.out.println(c2.getName());
        
        
        session2.getTransaction().commit();
        session2.close();
    }
    
    //join fetch
    @Test
    public void testQueryCache() {
        Session session = sf.openSession();
        session.beginTransaction();
        List<Category> categories = (List<Category>)session.createQuery("from Category")
                                    .setCacheable(true).list();
        
        
        
        session.getTransaction().commit();
        session.close();
        
        Session session2 = sf.openSession();
        session2.beginTransaction();
        List<Category> categories2 = (List<Category>)session2.createQuery("from Category")  //三级缓存,List也不会重新发select 把共用的存储起来
        .setCacheable(true).list();
        
        session2.getTransaction().commit();
        session2.close();
    }

 

 

posted @ 2018-09-07 21:22  橘柑之味  阅读(124)  评论(0编辑  收藏  举报