Skip to content

Commit

Permalink
add mybatis-redis-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
haiji.yang committed May 19, 2020
1 parent 2662222 commit 576ea81
Showing 1 changed file with 5 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
package com.javayh.mybatis.cache;

import com.javayh.common.selector.SpringSelector;
import com.javayh.common.util.log.Log;
import com.javayh.common.util.spring.SpringUtils;
import com.javayh.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cache.Cache;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -53,7 +44,7 @@ public RedisCache(final String id) {
@Autowired
private RedisUtil redisUtil;
private static RedisCache redisCache ;
//通过@PostConstruct实现初始化bean之前进行的操作

@PostConstruct
public void init() {
redisCache = this;
Expand All @@ -76,29 +67,23 @@ public void putObject(Object key, Object value) {
@Override
public Object getObject(Object key) {
try {
if (key != null && readWriteLock.readLock().tryLock()) {
if (key != null) {
return redisCache.redisUtil.get(key.toString());
}
} catch (Exception e) {
Log.error("Mybatis Get Cache",e.getStackTrace());
}finally {
log.debug(key.toString());
readWriteLock.readLock().unlock();
}
return null;
}

@Override
public Object removeObject(Object key) {
try {
if (key != null && readWriteLock.writeLock().tryLock()) {
if (!ObjectUtils.isEmpty(key)) {
redisCache.redisUtil.del(key.toString());
}
} catch (Exception e) {
Log.error("Mybatis Del Cache",e.getStackTrace());
}finally {
log.debug(key.toString());
readWriteLock.writeLock().unlock();
}
return null;
}
Expand All @@ -107,13 +92,11 @@ public Object removeObject(Object key) {
public void clear() {
try {
Set<String> keys = redisCache.redisUtil.keys(this.id);
if (!CollectionUtils.isEmpty(keys) && readWriteLock.writeLock().tryLock()) {
if (!CollectionUtils.isEmpty(keys)) {
redisCache.redisUtil.del(keys);
}
} catch (Exception e) {
Log.error("Mybatis Clear Cache",e.getStackTrace());
}finally {
readWriteLock.writeLock().unlock();
}
}

Expand Down

0 comments on commit 576ea81

Please sign in to comment.