How to realize two-level cache by combining mybatis with redis
This article will explain in detail how mybatis combines redis to achieve secondary caching. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
One: custom mybatis cache
We know that any mybatis secondary cache needs to implement an interface, which is called org.apache.ibatis.cache.Cache. The code is as follows:
Package com.demo.spring.mybatis.cache
Import java.util.concurrent.locks.ReadWriteLock
Import java.util.concurrent.locks.ReentrantReadWriteLock
Import org.apache.ibatis.cache.Cache
Import org.slf4j.Logger
Import org.slf4j.LoggerFactory
Import com.demo.spring.mybatis.util.SerializeUtil
Import redis.clients.jedis.Jedis
Import redis.clients.jedis.JedisPool
Public class MybatisRedisCache implements Cache {
Private static Logger logger = LoggerFactory.getLogger (MybatisRedisCache.class)
Private Jedis redisClient = createReids ()
Private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock ()
Private String id
Public MybatisRedisCache (final String id) {
If (id = = null) {
Throw new IllegalArgumentException ("Cache instances require an ID")
}
Logger.debug ("> MybatisRedisCache:id=" + id)
This.id = id
}
@ Override
Public String getId () {
Return this.id
}
@ Override
Public int getSize () {
Return Integer.valueOf (redisClient.dbSize () .toString ())
}
@ Override
Public void putObject (Object key, Object value) {
Logger.debug ("> putObject:" + key + "=" + value)
RedisClient.set (SerializeUtil.serialize (key.toString ()), SerializeUtil.serialize (value))
}
@ Override
Public Object getObject (Object key) {
Object value = SerializeUtil.unserialize (redisClient.get (SerializeUtil.serialize (key.toString ()
Logger.debug ("> getObject:" + key + "=" + value)
Return value
}
@ Override
Public Object removeObject (Object key) {
Return redisClient.expire (SerializeUtil.serialize (key.toString ()), 0)
}
@ Override
Public void clear () {
RedisClient.flushDB ()
}
@ Override
Public ReadWriteLock getReadWriteLock () {
Return readWriteLock
}
Protected static Jedis createReids () {
JedisPool pool = new JedisPool ("127.0.0.1", 6379)
Return pool.getResource ()
}
}
The above code is simply a basic Cache implementation, defining a serialized utility class
Package com.demo.spring.mybatis.util
Import java.io.ByteArrayInputStream
Import java.io.ByteArrayOutputStream
Import java.io.ObjectInputStream
Import java.io.ObjectOutputStream
Public class SerializeUtil {
Public static byte [] serialize (Object object) {
ObjectOutputStream oos = null
ByteArrayOutputStream baos = null
Try {
/ / Serialization
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) {
ByteArrayInputStream bais = null
Try {
/ / deserialization
Bais = new ByteArrayInputStream (bytes)
ObjectInputStream ois = new ObjectInputStream (bais)
Return ois.readObject ()
} catch (Exception e) {
}
Return null
}
}
Then configure it in mapper.xml
Of course, the following code needs to be configured in the main configuration file, which means to enable secondary cache. It is disabled by default.
So the configuration and code have been completed and the results are as follows:
Why does the second time go to the first-level cache?
This is analyzed when talking about the second-level cache source code, and the results of the previous query are cached only when commit is executed.
Open it? redis check it below, because the result of serialization is stored, but we can still vaguely see some information to the following figure.
This is the end of this article on "how mybatis combines redis to achieve secondary cache". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.