import lombok. extern. slf4j. Slf4j;
import org. apache. ibatis. cache. Cache;
import org. springframework. context. ApplicationContext;
import org. springframework. data. redis. core. RedisCallback;
import org. springframework. data. redis. core. RedisTemplate;
import org. springframework. data. redis. core. ValueOperations;
import java. util. concurrent. TimeUnit;
import java. util. concurrent. locks. ReadWriteLock;
import java. util. concurrent. locks. ReentrantReadWriteLock;
@Slf4j
public class RedisCache implements Cache {
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock ( ) ;
private final String id;
private RedisTemplate redisTemplate;
private static final long EXPIRE_TIME_IN_MINUTES = 30 ;
public RedisCache ( String id) {
if ( id == null) {
throw new IllegalArgumentException ( "Cache instances require an ID" ) ;
}
this . id = id;
}
@Override
public String getId ( ) {
return id;
}
@Override
@SuppressWarnings ( "unchecked" )
public void putObject ( Object key, Object value) {
RedisTemplate redisTemplate = getRedisTemplate ( ) ;
ValueOperations opsForValue = redisTemplate. opsForValue ( ) ;
opsForValue. set ( key, value, EXPIRE_TIME_IN_MINUTES, TimeUnit. MINUTES) ;
log. debug ( "Put query result to redis" ) ;
}
@Override
public Object getObject ( Object key) {
RedisTemplate redisTemplate = getRedisTemplate ( ) ;
ValueOperations opsForValue = redisTemplate. opsForValue ( ) ;
log. debug ( "Get cached query result from redis" ) ;
return opsForValue. get ( key) ;
}
@Override
@SuppressWarnings ( "unchecked" )
public Object removeObject ( Object key) {
RedisTemplate redisTemplate = getRedisTemplate ( ) ;
redisTemplate. delete ( key) ;
log. debug ( "Remove cached query result from redis" ) ;
return null;
}
@Override
public void clear ( ) {
RedisTemplate redisTemplate = getRedisTemplate ( ) ;
redisTemplate. execute ( ( RedisCallback) connection - > {
connection. flushDb ( ) ;
return null;
} ) ;
log. debug ( "Clear all the cached query result from redis" ) ;
}
@Override
public int getSize ( ) {
return 0 ;
}
@Override
public ReadWriteLock getReadWriteLock ( ) {
return readWriteLock;
}
private RedisTemplate getRedisTemplate ( ) {
if ( redisTemplate == null) {
redisTemplate = ( RedisTemplate) ApplicationContextHolder. getBean ( "redisTemplate" ) ;
}
return redisTemplate;
}
}
import org. springframework. beans. BeansException;
import org. springframework. context. ApplicationContext;
import org. springframework. context. ApplicationContextAware;
import org. springframework. stereotype. Component;
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext ( ApplicationContext applicationContext) throws BeansException {
this . applicationContext = applicationContext;
}
public static Object getBean ( String beanName) {
return applicationContext. getBean ( beanName) ;
}
}
< cache type= "org.mybatis.caches.redis.RedisCache" blocking= "false"
flushInterval= "0" readOnly= "true" size= "1024" eviction= "FIFO" / >
< select id= "querybyid" resultMap= "BaseResultMap" useCache= "true" >
SELECT * FROM user WHERE userID= #{ userID}
< / select>
< update id= "updateone" parameterType= "com.example.formybatis.entity.User" flushCache= "true" >
UPDATE user SET userName= #{ username} , userPasswd= #{ userpasswd} , userArea= #{ userarea} where userID= #{ userid}
< / update>