This is the 23rd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

From PageHelper chapter 11, we began to analyze the file structure and class structure of PageHelper.

In yesterday’s article, we looked at several classes in the package com.github. PageHelper.

Today we’ll look at classes in several subpackages of PageHelper.

Let’s take a look at some of the pageHelper subpackages:

com.github.pagehelper.cache, 
com.github.pagehelper.dialect, 
com.github.pagehelper.page, 
com.github.pagehelper.parser, 
com.github.pagehelper.util`
Copy the code

Cache package

As the name suggests, this package should be about caching. These include: Cache

, abstract factory class CacheFactory, SimpleCache

, GuavaCache

which implements the Cache

interface.
,>
,>
,>
,>

Interface Cache

defines two basic methods: get and set. The abstract factory class CacheFactory has a static method createCache that returns a value of type interface Cache

. A common Java method, class.forname (full-className), is used in createCache.
,>
,>

SimpleCache

defines an unmodifiable variable CACHE, a constructor SimpleCache(Properties Properties, String prefix), and the implementation interface CACHE

get. Set methods.
,>
,>

GuavaCache

also defines an unmodifiable variable CACHE, a constructor GuavaCache(Properties Properties, String prefix), and the implementation of the interface CACHE

get, Set methods.
,>
,>

The difference of the two CACHE implementation class SimpleCache CACHE type: org. Apache. Ibatis. CACHE. The CACHE > < K, V, and its type in GuavaCache as follows: Com.google.com mon. Cache. The cache (K, V >.

Let’s first look at the createCache method in the CacheFactory class:

/** * Create SQL cache **@param sqlCacheClass
     * @return* /
    public static <K, V> Cache<K, V> createCache(String sqlCacheClass, String prefix, Properties properties) {
        if (StringUtil.isEmpty(sqlCacheClass)) {
            try {
                Class.forName("com.google.common.cache.Cache");
                return new GuavaCache<K, V>(properties, prefix);
            } catch (Throwable t) {
                return newSimpleCache<K, V>(properties, prefix); }}else {
            try {
                Class<? extends Cache> clazz = (Class<? extends Cache>) Class.forName(sqlCacheClass);
                try {
                    Constructor<? extends Cache> constructor = clazz.getConstructor(Properties.class, String.class);
                    return constructor.newInstance(properties, prefix);
                } catch (Exception e) {
                    returnclazz.newInstance(); }}catch (Throwable t) {
                throw new PageException("Created Sql Cache [" + sqlCacheClass + "] Error", t); }}}Copy the code

Determine both logic by whether or not sqlCacheClass is passed.

If sqlCacheClass is not specified, try to create GuavaCache first, and then SimpleCache if the creation fails. Why execute the following line of code before creating GuavaCache?

Class.forName("com.google.common.cache.Cache");
Copy the code

Static Class
< span style = “box-sizing: border-box; color: RGB (74, 74, 74);

Returns the Class object associated with the Class or interface with the given string name.

This is especially true if we are familiar with database linking in Java.

When we connect to Mysql, there will be several lines:

Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password);
Copy the code

Com.mysql.jdbc.driver has this static code:

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    static { 
        try { 
            java.sql.DriverManager.registerDriver(new Driver()); 
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!"); }}public Driver(a) throws SQLException { 
    // Required for Class.forName().newInstance() }}Copy the code

The class initialization process executes the static statements of the class, including static variable declarations, static code blocks, and so on.

So the question comes, com.google.com mon. Cache. The cache is what kind of?