MyBatis-RedisCache源碼分析

回顧

在前面,我們通過 redis​ 集成了 MyBatis​ 的二級緩存,440.MyBatis的二級緩存整合redis ,接下來,我們來分析一下 RedisCache​ 的源碼。

源碼分析

RedisCache 主要是通過實現 Cache 接口來做的。數據存儲和獲取主要是通過操作 jedis 來實現。

public final class RedisCache implements Cache {
    private final ReadWriteLock readWriteLock = new DummyReadWriteLock();
    private String id;
    private static JedisPool pool;

    public RedisCache(String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        } else {
            this.id = id;
            RedisConfig redisConfig = RedisConfigurationBuilder.getInstance().parseConfiguration();
            pool = new JedisPool(redisConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getConnectionTimeout(), redisConfig.getSoTimeout(), redisConfig.getPassword(), redisConfig.getDatabase(), redisConfig.getClientName());
        }
    }
}

RedisCache 在 MyBatis 啓動的時候由 MyBatis 的 CacheBuilder​ 構建,構建的方式就是調用 Cache​ 實現類的帶 id​ 參數的構造方法。

// CacheBuilder.java

public Cache build() {
    setDefaultImplementations();
    Cache cache = newBaseCacheInstance(implementation, id);
    setCacheProperties(cache);
}

private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
    Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
    try {
        return cacheConstructor.newInstance(id);
    } catch (Exception e) {
        throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
    }
}

RedisCache​ 的構造方法中,調用了 RedisConfigurationBuilder​ 來常見 RedisConfig​ 對象,並通過 RedisConfig​ 對象來創建 Jedis​ 。

RedisConfig​ 繼承了 JedisPoolConfig​ ,並定義了一些屬性來讀取配置。

public class RedisConfig extends JedisPoolConfig {
    private String host = "localhost";
    private int port = 6379;
    private int connectionTimeout = 2000;
    private int soTimeout = 2000;
    private String password;
    private int database = 0;
    private String clientName;
}

RedisConfig​ 是由 RedisConfigurationBuilder​​ 構建的,這個類的主要方法是 parseConfiguration

public RedisConfig parseConfiguration(ClassLoader classLoader) {
    Properties config = new Properties();
    InputStream input = classLoader.getResourceAsStream(this.redisPropertiesFilename);
    if (input != null) {
        try {
            config.load(input);
        } catch (IOException var12) {
            throw new RuntimeException("An error occurred while reading classpath property '" + this.redisPropertiesFilename + "', see nested exceptions", var12);
        } finally {
            try {
                input.close();
            } catch (IOException var11) {
            }

        }
    }

    RedisConfig jedisConfig = new RedisConfig();
    this.setConfigProperties(config, jedisConfig);
    return jedisConfig;
}

該方法從 Resource​ 讀取一個 redis.properties​ 文件,其結構如下:

host=localhost
port=6379
password=123456
database=0

讀取完成之後將內容設置到 RedisConfig 對象中。

接下來,RedisCache 使用 RedisCo 創建 Jedis。在 RedisCache 中,實現了一個簡單的模板方法來操作 redis:

private Object execute(RedisCallback callback) {
    Jedis jedis = pool.getResource();

    Object var3;
    try {
        var3 = callback.doWithRedis(jedis);
    } finally {
        jedis.close();
    }

    return var3;
}

目標接口爲 RedisCallback,該接口定義了一個簡單的 doWithRedis 方法用來進行 redis 相關操作:

public interface RedisCallback {
    Object doWithRedis(Jedis var1);
}

接下來,我們分析一下 Cache 中的兩個重要方法 putObject()和 getObject()

public void putObject(final Object key, final Object value) {
    this.execute(new RedisCallback() {
        public Object doWithRedis(Jedis jedis) {
            jedis.hset(RedisCache.this.id.toString().getBytes(), key.toString().getBytes(), SerializeUtil.serialize(value));
            return null;
        }
    });
}
public Object getObject(final Object key) {
    return this.execute(new RedisCallback() {
        public Object doWithRedis(Jedis jedis) {
            return SerializeUtil.unserialize(jedis.hget(RedisCache.this.id.toString().getBytes(), key.toString().getBytes()));
        }
    });
}

可以看出來,MyBatis-RedisCache 採用的是 redis 的 hash​ 結構來存儲數據,把 Cache 的 id​ 作爲 hash 的 key​(Cache 的 id 在 Mapper 中是 namspace),緩存和讀取之前通過 SerializeUtil 進行序列化或者反序列化

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章