redis mybatis 2次キャッシュの実装
16767 ワード
先の記事ではmybatisの2次キャッシュの設定を見ましたが、ここではmybatisの2次キャッシュはmybatis自身のhashMapを使って2次キャッシュを実現できることを改めて学びます.一方、mybatis自体の2次キャッシュを使用すると、マッピングファイルにを追加するのに便利です.mybatisの2次キャッシュを開くと、2次キャッシュのプロパティを簡単に設定できます.
ラベルにはmybatisのこのnamespace(ネーミングスペースの下)の2次キャッシュの収集方法、生存時間、2次キャッシュサイズ、読み取り専用かどうかなどの属性が設定されています.
OKのこれらの構成はmybatis自身の2次キャッシュを使用しています.次に、サードパーティのキャッシュデータベースを使用してmybatisの2次キャッシュを実現する方法を見てみましょう.
mybatisはサードパーティのキャッシュを使用するためにcacheインタフェースを提供してくれました.このインタフェースを実現するだけでいいです.
コードを見てみましょう.
1、cacheインタフェースを実現するコード:(第一種類)
2、cacheインタフェースを実現するコード:(第2種、copyの)
3、シーケンス化逆シーケンス化クラス:
4、Userオブジェクト
5、mybatis sqlマッピングファイル:(最初のcache実装クラスを使用)
5、mybatis sqlマッピングファイル:(第2のcache実装クラスを使用)
6、テストクラス:insert操作を注釈した
次に、1回目と2回目のログ出力を見てみましょう.
最初の実行:
2回目の実行:
結果から、データベースクエリではなくredisから直接取得されたデータが2回目に見られます.
OKこの2回の実行は、insert操作を注釈した後のものです.次に、insert操作の実行結果を見てみましょう.
最初の実行結果:
2回目の実行結果:
2回の実行の結果から、2回の実行はすべて先にredisキャッシュに行ってデータを取得したが、データがなくてデータベースのクエリーを行い、クエリー後にクエリーの結果をredisに置いたが、insertを実行した後mybatisはredisキャッシュのすべてのデータをクリアしたので、2回目のクエリーではデータベースからクエリーを取得した!
簡単なまとめ:
1、mybatisの2次キャッシュの範囲はネーミングスペース(namespace)
2、このネーミングスペースの下にinsert、update、delete mybatisがある限り、このネーミングスペースの下の2級を緩やかにクリアします.
3、同じsqlが異なるネーミングスペースの下にあると、1つのinsert、update、deletedがもう1つのユーザーがデータをキャッシュする可能性があるため、データの不一致が発生します.
4、更新、削除、挿入の頻度が高いと、すべてのキャッシュが削除され、すべてのキャッシュが追加され、すべてのキャッシュが削除されます.これにより、キャッシュのヒット率が低いか、キャッシュの役割を果たせず、リソースが消費されます.
したがって,この問題を解決しない前提では,二次キャッシュの使用は推奨されない.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis1.userMap">
<cache eviction="FIFO" flushInterval="600000" size="1024"
readOnly="true" />
<resultMap id="result" type="com.mybatis1.User">
<result property="name" column="name" />
<result property="password" column="password" />
</resultMap>
<select id="getUser" parameterType="java.lang.String" resultMap="result">
select * from usert where name =#{name}
</select>
<insert id="addUser" parameterType="com.mybatis1.User">
insert into usert values(#{name},#{password})
</insert>
</mapper>
OKのこれらの構成はmybatis自身の2次キャッシュを使用しています.次に、サードパーティのキャッシュデータベースを使用してmybatisの2次キャッシュを実現する方法を見てみましょう.
mybatisはサードパーティのキャッシュを使用するためにcacheインタフェースを提供してくれました.このインタフェースを実現するだけでいいです.
コードを見てみましょう.
1、cacheインタフェースを実現するコード:(第一種類)
package com.mybatis3;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author WHD data 2016 5 22
*/
public class RedisCache implements Cache {
private Jedis redisClient = createReids();
/** The ReadWriteLock. */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
@Override
public String getId() {
return this.id;
}
@Override
public int getSize() {
// key
return Integer.valueOf(redisClient.dbSize().toString());
}
@Override
public void putObject(Object key, Object value) {
System.err.println("put key" + key);
redisClient.set(SerializeUtil.serialize(key.toString()),
SerializeUtil.serialize(value));
}
@Override
public Object getObject(Object key) {
System.out.println("get key");
Object value = SerializeUtil.unserialize(redisClient.get(SerializeUtil
.serialize(key.toString())));
return value;
}
@Override
public Object removeObject(Object key) {
System.err.println("remove key" + key);
return redisClient.expire(SerializeUtil.serialize(key.toString()), 0);
}
// insert update delete
@Override
public void clear() {
System.err.println("clear all data");
redisClient.flushDB();
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
protected static Jedis createReids() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379);
return pool.getResource();
}
}
2、cacheインタフェースを実現するコード:(第2種、copyの)
package com.mybatis3;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
* @author WHD data 2016 5 22
*/
public class MybatisRedisCache implements Cache {
/** The ReadWriteLock. */
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private String id;
public MybatisRedisCache(final String id) {
System.out.println(" ");
if (id == null) {
throw new IllegalArgumentException(" ID");
}
this.id = id;
}
@Override
public String getId() {
return this.id;
}
@Override
public int getSize() {
Jedis jedis = null;
JedisPool jedisPool = null;
int result = 0;
boolean borrowOrOprSuccess = true;
try {
jedis = CachePool.getInstance().getJedis();
jedisPool = CachePool.getInstance().getJedisPool();
result = Integer.valueOf(jedis.dbSize().toString());
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
jedisPool.returnResource(jedis);
}
return result;
}
@Override
public void putObject(Object key, Object value) {
System.out.println("put key");
Jedis jedis = null;
JedisPool jedisPool = null;
boolean borrowOrOprSuccess = true;
try {
jedis = CachePool.getInstance().getJedis();
jedisPool = CachePool.getInstance().getJedisPool();
jedis.set(SerializeUtil.serialize(key.hashCode()),
SerializeUtil.serialize(value));
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
jedisPool.returnResource(jedis);
}
}
@Override
public Object getObject(Object key) {
System.out.println("get key");
Jedis jedis = null;
JedisPool jedisPool = null;
Object value = null;
boolean borrowOrOprSuccess = true;
try {
jedis = CachePool.getInstance().getJedis();
jedisPool = CachePool.getInstance().getJedisPool();
value = SerializeUtil.unserialize(jedis.get(SerializeUtil
.serialize(key.hashCode())));
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
jedisPool.returnResource(jedis);
}
return value;
}
@Override
public Object removeObject(Object key) {
System.out.println("remove key ");
Jedis jedis = null;
JedisPool jedisPool = null;
Object value = null;
boolean borrowOrOprSuccess = true;
try {
jedis = CachePool.getInstance().getJedis();
jedisPool = CachePool.getInstance().getJedisPool();
value = jedis.expire(SerializeUtil.serialize(key.hashCode()), 0);
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
jedisPool.returnResource(jedis);
}
return value;
}
@Override
public void clear() {
System.out.println("clear all data");
Jedis jedis = null;
JedisPool jedisPool = null;
boolean borrowOrOprSuccess = true;
try {
jedis = CachePool.getInstance().getJedis();
jedisPool = CachePool.getInstance().getJedisPool();
jedis.flushDB();
jedis.flushAll();
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
jedisPool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
jedisPool.returnResource(jedis);
}
}
@Override
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
public static class CachePool {
JedisPool pool;
private static final CachePool cachePool = new CachePool();
public static CachePool getInstance() {
return cachePool;
}
private CachePool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(100);
config.setMaxWait(1000l);
/*
* PropertiesLoader pl = new
* PropertiesLoader("classpath:config/redis.properties");
*/
// jedisPool= new JedisPool(config,"127.0.0.1",6379);
pool = new JedisPool(config, "127.0.0.1", 6379);
}
public Jedis getJedis() {
Jedis jedis = null;
boolean borrowOrOprSuccess = true;
try {
jedis = pool.getResource();
} catch (JedisConnectionException e) {
borrowOrOprSuccess = false;
if (jedis != null)
pool.returnBrokenResource(jedis);
} finally {
if (borrowOrOprSuccess)
pool.returnResource(jedis);
}
jedis = pool.getResource();
return jedis;
}
public JedisPool getJedisPool() {
return this.pool;
}
}
}
3、シーケンス化逆シーケンス化クラス:
package com.mybatis3;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* @author WHD data 2016 5 22
*/
public class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
//
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Object unserialize(byte[] bytes) {
if (bytes == null)
return null;
ByteArrayInputStream bais = null;
try {
//
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
4、Userオブジェクト
package com.mybatis3;
import java.io.Serializable;
/**
* @author WHD data 2016 2 28
*/
public class User implements Serializable {
private static final long serialVersionUID = 3574211222602302068L;
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "[name=" + name + ", password=" + password + "]";
}
}
5、mybatis sqlマッピングファイル:(最初のcache実装クラスを使用)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.userMap">
<cache eviction="FIFO" flushInterval="600000" size="1024" readOnly="true"
type="com.mybatis3.RedisCache"/>
<resultMap id="result" type="com.mybatis3.User">
<result property="name" column="name" />
<result property="password" column="password" />
</resultMap>
<select id="userInfo" parameterType="java.lang.String"
resultMap="result" >
select * from usert where name =#{name}
</select>
<insert id="insertInfo" parameterType="com.mybatis3.User">
insert into usert (name,password) values(#{name},#{password})
</insert>
</mapper>
5、mybatis sqlマッピングファイル:(第2のcache実装クラスを使用)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis3.userMap">
<cache eviction="FIFO" flushInterval="600000" size="1024" readOnly="true"
type="com.mybatis3.MybatisRedisCache"/>
<resultMap id="result" type="com.mybatis3.User">
<result property="name" column="name" />
<result property="password" column="password" />
</resultMap>
<select id="userInfo" parameterType="java.lang.String"
resultMap="result" >
select * from usert where name =#{name}
</select>
<insert id="insertInfo" parameterType="com.mybatis3.User">
insert into usert (name,password) values(#{name},#{password})
</insert>
</mapper>
6、テストクラス:insert操作を注釈した
package com.mybatis3;
import java.io.InputStream;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis3.User;
/**
* @author WHD data 2016 5 18
*/
public class Test {
public Test() {
}
public static void main(String[] args) {
SqlSessionFactory factory = null;
factory = getSession(null, null);
SqlSession session = factory.openSession(ExecutorType.REUSE);
SqlSession session1 = factory.openSession(ExecutorType.REUSE);
User user = session.selectOne("com.mybatis3.userMap.userInfo", "whd");
session.commit();
User user2 = new User();
user2.setName("hello");
user2.setPassword("hello");
// session1.insert("com.mybatis3.userMap.insertInfo", user2);
session1.commit();
}
public static SqlSessionFactory getSession(String path, String environment) {
if (null == path || "".equals(path)) {
path = "config.xml";
}
InputStream input = Test.class.getClassLoader().getResourceAsStream(
path);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// environment input
if (null == environment || "".equals(environment)) {
environment = "development";
}
SqlSessionFactory factory = builder.build(input, environment);
return factory;
}
}
次に、1回目と2回目のログ出力を見てみましょう.
最初の実行:
get key
2016-05-23 21:27:07,737 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@22ad520d]
2016-05-23 21:27:07,738 [main] DEBUG [java.sql.Connection] - ==> Preparing: select * from usert where name =?
2016-05-23 21:27:07,811 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
put key
データベースクエリーを行い、クエリーの結果をredisに保存しました.2回目の実行:
get key
結果から、データベースクエリではなくredisから直接取得されたデータが2回目に見られます.
OKこの2回の実行は、insert操作を注釈した後のものです.次に、insert操作の実行結果を見てみましょう.
最初の実行結果:
get key
2016-05-23 21:31:31,390 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@20e5e544]
2016-05-23 21:31:31,391 [main] DEBUG [java.sql.Connection] - ==> Preparing: select * from usert where name =?
2016-05-23 21:31:31,458 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
put key
2016-05-23 21:31:31,517 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@66a7d863]
2016-05-23 21:31:31,517 [main] DEBUG [java.sql.Connection] - ==> Preparing: insert into usert (name,password) values(?,?)
2016-05-23 21:31:31,517 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: hello(String), hello(String)
clear all data
2回目の実行結果:
get key
2016-05-23 21:32:33,390 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@20e5e544]
2016-05-23 21:32:33,391 [main] DEBUG [java.sql.Connection] - ==> Preparing: select * from usert where name =?
2016-05-23 21:32:33,450 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: whd(String)
put key
2016-05-23 21:32:33,511 [main] DEBUG [java.sql.Connection] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@66a7d863]
2016-05-23 21:32:33,511 [main] DEBUG [java.sql.Connection] - ==> Preparing: insert into usert (name,password) values(?,?)
2016-05-23 21:32:33,512 [main] DEBUG [java.sql.PreparedStatement] - ==> Parameters: hello(String), hello(String)
clear all data
2回の実行の結果から、2回の実行はすべて先にredisキャッシュに行ってデータを取得したが、データがなくてデータベースのクエリーを行い、クエリー後にクエリーの結果をredisに置いたが、insertを実行した後mybatisはredisキャッシュのすべてのデータをクリアしたので、2回目のクエリーではデータベースからクエリーを取得した!
簡単なまとめ:
1、mybatisの2次キャッシュの範囲はネーミングスペース(namespace)
2、このネーミングスペースの下にinsert、update、delete mybatisがある限り、このネーミングスペースの下の2級を緩やかにクリアします.
3、同じsqlが異なるネーミングスペースの下にあると、1つのinsert、update、deletedがもう1つのユーザーがデータをキャッシュする可能性があるため、データの不一致が発生します.
4、更新、削除、挿入の頻度が高いと、すべてのキャッシュが削除され、すべてのキャッシュが追加され、すべてのキャッシュが削除されます.これにより、キャッシュのヒット率が低いか、キャッシュの役割を果たせず、リソースが消費されます.
したがって,この問題を解決しない前提では,二次キャッシュの使用は推奨されない.