Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghaiji committed May 12, 2020
2 parents dcbe542 + 2662222 commit 080d15a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 9 deletions.
4 changes: 0 additions & 4 deletions doc/file/db1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,3 @@ spring:
reset-enable: false
login-username: admin
login-password: admin123

mybatis:
### xml存放路径
mapper-locations: classpath*:mapper/*/*Mapper.xml
9 changes: 9 additions & 0 deletions doc/file/mybatis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## mybatis 常用配置
mybatis:
### xml存放路径
mapper-locations: classpath*:mapper/*/*Mapper.xml
configuration:
cache-enabled: true
lazy-loading-enabled: false
aggressive-lazy-loading: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.javayh.api.sys.dao.SysLogisticsMapper">

<!-- 数据库中表名为:sys_logistics的列名,as前是数据库的列明,as后是列的别名用于映射成实体类中的属性 -->
<cache type="com.javayh.mybatis.cache.RedisCache"/>

<!-- 数据库中表名为:sys_logistics的列名,as前是数据库的列明,as后是列的别名用于映射成实体类中的属性 -->
<sql id="sysLogisticsColumn">
sys_logistics.id as id,
sys_logistics.addressee_name as addresseeName,
Expand Down
6 changes: 5 additions & 1 deletion javayh-dependencies/javayh-mybatis-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<artifactId>javayh-common-starter</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.javayh</groupId>
<artifactId>javayh-redis-starter</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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 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;

/**
* <p>
* Custom cache
* </p>
*
* @author Dylan-haiji
* @version 1.0.0
* @since 2020-05-11
*/
@Slf4j
@Configuration
public class RedisCache implements Cache {

private String id;

/** 读写锁*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);

public RedisCache() {
}

public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
this.id = id;
}
@Autowired
private RedisUtil redisUtil;
private static RedisCache redisCache ;
//通过@PostConstruct实现初始化bean之前进行的操作
@PostConstruct
public void init() {
redisCache = this;
redisCache.redisUtil = this.redisUtil;
}
@Override
public String getId() {
return this.id;
}

@Override
public void putObject(Object key, Object value) {
if (value != null) {
//向Redis中添加数据,有效时间是12小时
redisCache.redisUtil.setObj(key.toString(),value,43200);
log.debug(value.toString());
}
}

@Override
public Object getObject(Object key) {
try {
if (key != null && readWriteLock.readLock().tryLock()) {
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()) {
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;
}

@Override
public void clear() {
try {
Set<String> keys = redisCache.redisUtil.keys(this.id);
if (!CollectionUtils.isEmpty(keys) && readWriteLock.writeLock().tryLock()) {
redisCache.redisUtil.del(keys);
}
} catch (Exception e) {
Log.error("Mybatis Clear Cache",e.getStackTrace());
}finally {
readWriteLock.writeLock().unlock();
}
}

@Override
public int getSize() {
return redisCache.redisUtil.execute();
}

@Override
public ReadWriteLock getReadWriteLock() {
return this.readWriteLock;
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.javayh.mybatis.cache.RedisCache
#com.javayh.mybatis.filter.MybatisFilterAutoConfiguration
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.javayh.redis.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -61,6 +68,15 @@ public boolean expire(String key, long time) {
}
}


public Integer execute() {
Long size = redisTemplate.execute(RedisServerCommands::dbSize);
return Objects.requireNonNull(size).intValue();
}
public Set<String> keys(String key) {
return redisTemplate.keys("*:" + key + "*");
}

/**
* <p>
* 根据key 获取过期时间
Expand Down Expand Up @@ -111,11 +127,25 @@ public void del(String... key) {
redisTemplate.delete(key[0]);
}
else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
redisTemplate.delete(Arrays.asList(key));
}
}
}

/**
* <p>
* 删除
* </p>
* @version 1.0.0
* @author Dylan-haiji
* @since 2020/5/11
* @param keys
* @return void
*/
public void del(Collection<String> keys) {
redisTemplate.delete(keys);
}

/**
* <p>
* 获取
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private String genContent() {
.append(lineSeparator);
content.append("<mapper namespace=\"").append(mapperFile.getClassQuaName())
.append("\">").append(lineSeparator);

content.append("<cache type=\"com.javayh.mybatis.cache.RedisCache\"/>").append(lineSeparator);
// 写入共有列sql
content.append(lineSeparator);
content.append("\t")
Expand Down

0 comments on commit 080d15a

Please sign in to comment.