MyBatis combined with Redis combat level two cache (vi)

Source: Internet
Author: User

In the previous article we analyzed the first level cache, level two cache related source code and basic principles, today we share the MyBatis level two cache and Redis combination, of course MyBatis two cache can also and Ehcache, Memcache, Oscache, Hazelcast used in combination. Second-level cache related source sharing please refer to the "MyBatis source analysis cache level two caching principle." We have two ways to combat, one is to write their own cache, the other is the official given the demo address: http://www.mybatis.org/redis-cache/

One: Custom MyBatis cache


We know that any mybatis level two cache needs to implement an interface that is Org.apache.ibatis.cache.Cacheand 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);p rivate jedis redisclient = createreids () ;p Rivate 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;} @Overridepublic  string getid ()  {return this.id;} @Overridepublic  int getsize ()  {return integer.valueof (Redisclient.dbsize (). toString ());} @Overridepublic  void putobject (Object key, object value)  {logger.debug (">> >>>>>>>>>>>>>>>>>>>>>>putobject: " +  key +  "="  + value); Redisclient.set (Serializeutil.serialize (key.tostring ()),  Serializeutil.serialize (value));} @Overridepublic  object getobject (Object key)  {Object value =  Serializeutil.unserialize (Redisclient.get (Serializeutil.serialize (key.tostring ())); Logger.debug (">>> >>>>>>>>>>>>>>>>>>>>>getobject: " +  key +  "="  + value); return value;} @Overridepublic  object removeobject (Object key)  {return redisclient.expire ( Serializeutil.serialize (Key.tostring ()),  0);} @Overridepublic  void clear ()  {redisclient.flushdb ();} @Overridepublic  readwritelock getreadwritelock ()  {return readwritelock;} Protected static jedis createreids ()  {jedispool pool = new jedispool (" 127.0.0.1 ",  6379); Return pool.getresource ();}}


The above code is very simple is the basic cache implementation, in the definition of a serialized tool 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 the Mapper.xml in the

<cache eviction= "LRU" type= "Com.demo.spring.mybatis.cache.MybatisRedisCache"/>

Of course, in the main configuration file also need to configure the following code, which means to turn on level two cache, default is not open

<setting name= "cacheenabled" value= "true"/>


So the configuration and code are all finished running the result as follows:



Why is the second time going to be a first-level cache?

This is in the two-level cache source of the analysis, only when the execution of a commit before the results of the previous query into the cache.



Do you want to open it? Redis looks like this because the serialized results are stored, but we can still see some information to




II: Official examples


Address: http://www.mybatis.org/redis-cache/


In fact, the same as our custom, but when using the Redis client version to be compatible with its version, or error.

Mybatis-redis 1.0.0-beta2 corresponding negative redis.clients? Jedis 2.8.0 Current maximum version 2.9.0 not supported



Source: http://www.ccblog.cn/91.htm



Null

MyBatis combined with Redis combat level two cache (vi)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.