Use HashMap to simulate a Session cache (simple version)

Posted by bluestar on Sat, 04 Apr 2020 15:28:27 +0200

This article records the characteristics of the first level cache in Hibernate.

Details of L1 cache: what operations will be directed to

1. Level cache put data

save,update,saveOrUpdate,load,get,list,iterate,lock

2. What operation will fetch data from the first level cache.
get / load / list

get / load will first get from the first level cache. If it doesn't, there will be different operations [get will immediately send a request to the database, and load will return a proxy object, and the request will not be sent to the database until the user really uses the data.
Will list fetch data from session cache?

3. The first level cache can be used without configuration. It has no protection mechanism, so we programmers should consider this problem. We can use evict or clear to clear the objects in the session cache.

evict is to clear an object, and clear is to clear all session cache objects

4. The life cycle of the object in the session level cache is automatically destroyed when the session is closed

5. Use HashMap to simulate a Session cache to deepen the cache.

package dome;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyCache {
    // Use map To simulate caching
    static Map<Integer, Student> maps = new HashMap<Integer, Student>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        getStudent(1);
        getStudent(1);
        getStudent(1);
        getStudent(1);
        getStudent(3);
        getStudent(3);

    }

    public static Student getStudent(Integer id) { // s.get()

        // Go to cache first
        if (maps.containsKey(id)) {
            // In cache
            System.out.println("Fetch from cache");
            return maps.get(id);
        } else {
            System.out.println("Get from database");
            // Access to database
            Student stu = MyDB.getStudentFromDB(id);
            // Put in cache
            maps.put(id, stu);
            return stu;
        }

    }

}

// My database
class MyDB {

    static List<Student> lists = new ArrayList<Student>();

    // Initialize database,Suppose there are three students
    static {
        Student s1 = new Student();
        s1.setId(1);
        s1.setName("aaa");
        Student s2 = new Student();
        s2.setId(2);
        s2.setName("bbb");
        Student s3 = new Student();
        s3.setId(3);
        s3.setName("ccc");
        lists.add(s1);
        lists.add(s2);
        lists.add(s3);

    }

    public static Student getStudentFromDB(Integer id) {
        for (Student s : lists) {
            if (s.getId().equals(id)) {
                return s;
            }
        }
        return null;// Not in database.

    }
}

class Student {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Topics: Java Database Session Hibernate