I need to install Redis on my computer before using Redis;
The essence of using Spring+mybaties+redis is the extended class Org.apache.ibatis.cache.Cache, which uses the Redis API in our own extended cache;
One: The dependencies that need to be introduced:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
Two: Open the cache in the Mybaties-config.xml configuration file:
<settings>
<!--This configuration enables the global mapper to enable or disable the cache--
<setting name= "cacheenabled" value= "true"/>
<!--enable or disable lazy loading globally. When disabled, all associated objects are loaded immediately. -
<setting name= "lazyloadingenabled" value= "false"/>
<setting name= "aggressivelazyloading" value= "true"/>
</settings>
Here's what to note:
Property deferred loading and associated object loading (lazyloadingenabled) is turned off, or the Cglib proxy object is put into Redis, and errors occur when the data changes.
Three: Write your own extension class (extension class for the Org.apache.ibatis.cache.Cache Class):
Class one is as follows:
Package Com.rayeye.law.app.utils.redis;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Org.apache.ibatis.cache.Cache;
Import Redis.clients.jedis.Jedis;
Import Redis.clients.jedis.JedisPool;
Import Redis.clients.jedis.JedisPoolConfig;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Java.util.concurrent.locks.ReadWriteLock;
Import Java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* Created by Sean on 2016/9/13.
*/
public class Rediscache implements Cache {
private static Log logger = Logfactory.getlog (Rediscache.class);
Private Jedis redisclient = CreateClient ();
/** 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");
}
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 createclient () {
try {
Jedispool pool = new Jedispool (new Jedispoolconfig (), "localhost");
return Pool.getresource ();
} catch (Exception e) {
E.printstacktrace ();
}
throw new RuntimeException ("Initialize connection pool error");
}
}
Class Serializeutil {
public static byte[] Serialize (Object object) {
ObjectOutputStream oos = null;
Bytearrayoutputstream BAOs = null;
try {
Serialization of
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 {
Deserialization
Bais = new Bytearrayinputstream (bytes);
ObjectInputStream ois = new ObjectInputStream (Bais);
return Ois.readobject ();
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}
}
Class two is as follows:
Package Com.rayeye.law.app.utils.redis;
Import Org.apache.ibatis.cache.decorators.LoggingCache;
/**
* Created by Sean on 2016/9/13.
*/
public class Loggingrediscache extends Loggingcache {
Public Loggingrediscache (String ID) {
Super (new Rediscache (ID));
}
}
Four: Directly referencing the cache in the Mapper.xml file
- <!--Enable Cache--
- <cache type="Cn.seafood.cache.LoggingRedisCache" />
As follows:
<?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.rayeye.law.app.dao.LawAssistantMapper" >
<cache type= "Com.rayeye.law.app.utils.redis.LoggingRedisCache"/>
<select id= "GETWFK" parametertype= "Java.util.Map" resulttype= "Java.util.Map" >
SELECT * FROM Weifaku where entitystatus=0
</select>
</mapper>
The above is the Spring+mybaties+redis simple integration process,
Benefits of Redis:
1. Very high performance (on-line),
2. Rich data types,
3. The operation is atomic (the combined operation is also atomic),
Disadvantages:
1. Receive physical memory limitations (Redis is a standalone module with exclusive memory) and cannot be used to read and write Yu Hai volume data (high-performance operations for smaller data volumes).
Summary: Redis is limited to specific scenarios, focused on specific areas, and is very fast, and has not yet been found to replace the use of the product. (said online).
SPRINGMVC Cache (Getting Started Spring+mybaties+redis one)