Skip to content

Commit

Permalink
新增redisson支持
Browse files Browse the repository at this point in the history
  • Loading branch information
iBase4J committed Jan 16, 2017
1 parent 3ab0c73 commit fb0b9be
Show file tree
Hide file tree
Showing 24 changed files with 534 additions and 334 deletions.
4 changes: 3 additions & 1 deletion iBase4J-Biz-Service/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<loggers>
<logger name="org.slf4j" level="INFO" />
<logger name="org.quartz" level="INFO" />
<logger name="org.redisson" level="INFO" />
<logger name="com.atomikos" level="WARN" />
<logger name="com.alibaba.dubbo" level="INFO" />
<logger name="org.apache.activemq" level="INFO" />
<logger name="org.springframework" level="INFO" />
<logger name="org.apache.zookeeper" level="WARN" />
<logger name="org.springframework.web" level="${OUTPUT_LOG_LEVEL}" />
<logger name="org.springframework.web" level="DEBUG" />
<logger name="org.springframework.web.servlet.DispatcherServlet" level="INFO" />
<AsyncLogger name="org.ibase4j" level="${OUTPUT_LOG_LEVEL}" additivity="false" includeLocation="true">
<appender-ref ref="Console" />
<appender-ref ref="ProductLog" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ibase4j.core.Constants;
import org.ibase4j.core.util.JedisUtil;
import org.ibase4j.core.util.RedissonUtil;
import org.ibase4j.service.sys.SysSessionService;
import org.springframework.beans.factory.annotation.Autowired;

Expand Down Expand Up @@ -58,13 +58,13 @@ private void setAllUserNumber(int n) {
Long number = getAllUserNumber() + n;
if (number >= 0) {
logger.info("用户数:" + number);
JedisUtil.set(Constants.ALLUSER_NUMBER, 60 * 60 * 24, number);
RedissonUtil.set(Constants.ALLUSER_NUMBER, number, 60 * 60 * 24);
}
}

/** 获取在线用户数量 */
public static Long getAllUserNumber() {
String v = JedisUtil.get(Constants.ALLUSER_NUMBER);
String v = (String) RedissonUtil.get(Constants.ALLUSER_NUMBER);
if (v != null) {
return Long.valueOf(v);
}
Expand Down
4 changes: 3 additions & 1 deletion iBase4J-Biz-Web/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<loggers>
<logger name="org.slf4j" level="INFO" />
<logger name="org.quartz" level="INFO" />
<logger name="org.redisson" level="INFO" />
<logger name="com.atomikos" level="WARN" />
<logger name="com.alibaba.dubbo" level="INFO" />
<logger name="org.apache.activemq" level="INFO" />
<logger name="org.springframework" level="INFO" />
<logger name="org.apache.zookeeper" level="WARN" />
<logger name="org.springframework.web" level="${OUTPUT_LOG_LEVEL}" />
<logger name="org.springframework.web" level="DEBUG" />
<logger name="org.springframework.web.servlet.DispatcherServlet" level="INFO" />
<AsyncLogger name="org.ibase4j" level="${OUTPUT_LOG_LEVEL}" additivity="false" includeLocation="true">
<appender-ref ref="Console" />
<appender-ref ref="ProductLog" />
Expand Down
266 changes: 133 additions & 133 deletions iBase4J-Common/src/main/java/org/ibase4j/core/base/BaseProviderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.ibase4j.core.Constants;
import org.ibase4j.core.util.DataUtil;
import org.ibase4j.core.util.InstanceUtil;
import org.ibase4j.core.util.RedisUtil;
import org.ibase4j.core.util.RedissonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -24,146 +24,146 @@
* @version 2016年5月20日 下午3:19:19
*/
public abstract class BaseProviderImpl<T extends BaseModel> implements BaseProvider<T> {
protected Logger logger = LogManager.getLogger(getClass());
@Autowired
protected BaseMapper<T> mapper;
protected Logger logger = LogManager.getLogger(getClass());
@Autowired
protected BaseMapper<T> mapper;

/** 分页查询 */
public static Page<Long> getPage(Map<String, Object> params) {
Integer current = 1;
Integer size = 10;
String orderBy = "";
if (DataUtil.isNotEmpty(params.get("pageNum"))) {
current = Integer.valueOf((String)params.get("pageNum"));
}
if (DataUtil.isNotEmpty(params.get("pageSize"))) {
size = Integer.valueOf((String)params.get("pageSize"));
}
if (DataUtil.isNotEmpty(params.get("orderBy"))) {
orderBy = (String)params.get("orderBy");
}
Page<Long> page = new Page<Long>(current, size, orderBy);
page.setAsc(false);
return page;
}
/** 分页查询 */
public static Page<Long> getPage(Map<String, Object> params) {
Integer current = 1;
Integer size = 10;
String orderBy = "";
if (DataUtil.isNotEmpty(params.get("pageNum"))) {
current = Integer.valueOf((String) params.get("pageNum"));
}
if (DataUtil.isNotEmpty(params.get("pageSize"))) {
size = Integer.valueOf((String) params.get("pageSize"));
}
if (DataUtil.isNotEmpty(params.get("orderBy"))) {
orderBy = (String) params.get("orderBy");
}
Page<Long> page = new Page<Long>(current, size, orderBy);
page.setAsc(false);
return page;
}

/** 根据Id查询(默认类型T) */
public Page<T> getPage(Page<Long> ids) {
if (ids != null) {
Page<T> page = new Page<T>(ids.getCurrent(), ids.getSize());
page.setTotal(ids.getTotal());
List<T> records = InstanceUtil.newArrayList();
for (Long id : ids.getRecords()) {
records.add(this.queryById(id));
}
page.setRecords(records);
return page;
}
return new Page<T>();
}
/** 根据Id查询(默认类型T) */
public Page<T> getPage(Page<Long> ids) {
if (ids != null) {
Page<T> page = new Page<T>(ids.getCurrent(), ids.getSize());
page.setTotal(ids.getTotal());
List<T> records = InstanceUtil.newArrayList();
for (Long id : ids.getRecords()) {
records.add(this.queryById(id));
}
page.setRecords(records);
return page;
}
return new Page<T>();
}

/** 根据Id查询(cls返回类型Class) */
public <K> Page<K> getPage(Page<Long> ids, Class<K> cls) {
if (ids != null) {
Page<K> page = new Page<K>(ids.getCurrent(), ids.getSize());
page.setTotal(ids.getTotal());
List<K> records = InstanceUtil.newArrayList();
for (Long id : ids.getRecords()) {
T t = this.queryById(id);
K k = InstanceUtil.to(t, cls);
records.add(k);
}
page.setRecords(records);
return page;
}
return new Page<K>();
}
/** 根据Id查询(cls返回类型Class) */
public <K> Page<K> getPage(Page<Long> ids, Class<K> cls) {
if (ids != null) {
Page<K> page = new Page<K>(ids.getCurrent(), ids.getSize());
page.setTotal(ids.getTotal());
List<K> records = InstanceUtil.newArrayList();
for (Long id : ids.getRecords()) {
T t = this.queryById(id);
K k = InstanceUtil.to(t, cls);
records.add(k);
}
page.setRecords(records);
return page;
}
return new Page<K>();
}

/** 根据Id查询(默认类型T) */
public List<T> getList(List<Long> ids) {
List<T> list = InstanceUtil.newArrayList();
if (ids != null) {
for (Long id : ids) {
list.add(this.queryById(id));
}
}
return list;
}
/** 根据Id查询(默认类型T) */
public List<T> getList(List<Long> ids) {
List<T> list = InstanceUtil.newArrayList();
if (ids != null) {
for (Long id : ids) {
list.add(this.queryById(id));
}
}
return list;
}

/** 根据Id查询(cls返回类型Class) */
public <K> List<K> getList(List<Long> ids, Class<K> cls) {
List<K> list = InstanceUtil.newArrayList();
if (ids != null) {
for (Long id : ids) {
T t = this.queryById(id);
K k = InstanceUtil.to(t, cls);
list.add(k);
}
}
return list;
}
/** 根据Id查询(cls返回类型Class) */
public <K> List<K> getList(List<Long> ids, Class<K> cls) {
List<K> list = InstanceUtil.newArrayList();
if (ids != null) {
for (Long id : ids) {
T t = this.queryById(id);
K k = InstanceUtil.to(t, cls);
list.add(k);
}
}
return list;
}

@Transactional
public void delete(Long id, Long userId) {
try {
T record = this.queryById(id);
record.setEnable(false);
record.setUpdateTime(new Date());
record.setUpdateBy(userId);
mapper.updateById(record);
RedisUtil.set(getCacheKey(id), record);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Transactional
public void delete(Long id, Long userId) {
try {
T record = this.queryById(id);
record.setEnable(false);
record.setUpdateTime(new Date());
record.setUpdateBy(userId);
mapper.updateById(record);
RedissonUtil.set(getCacheKey(id), record);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

@Transactional
public T update(T record) {
try {
record.setEnable(true);
record.setUpdateTime(new Date());
if (record.getId() == null) {
record.setCreateTime(new Date());
mapper.insert(record);
} else {
mapper.updateById(record);
}
RedisUtil.set(getCacheKey(record.getId()), record);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
return record;
}
@Transactional
public T update(T record) {
try {
record.setEnable(true);
record.setUpdateTime(new Date());
if (record.getId() == null) {
record.setCreateTime(new Date());
mapper.insert(record);
} else {
mapper.updateById(record);
}
RedissonUtil.set(getCacheKey(record.getId()), record);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
return record;
}

@Transactional
@SuppressWarnings("unchecked")
public T queryById(Long id) {
try {
String key = getCacheKey(id);
T record = (T)RedisUtil.get(key);
if (record == null) {
record = mapper.selectById(id);
RedisUtil.set(key, record);
}
return record;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Transactional
@SuppressWarnings("unchecked")
public T queryById(Long id) {
try {
String key = getCacheKey(id);
T record = (T) RedissonUtil.get(key);
if (record == null) {
record = mapper.selectById(id);
RedissonUtil.set(key, record);
}
return record;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/** 获取缓存键值 */
protected String getCacheKey(Object id) {
String cacheName = null;
CacheConfig cacheConfig = getClass().getAnnotation(CacheConfig.class);
if (cacheConfig == null || cacheConfig.cacheNames() == null || cacheConfig.cacheNames().length < 1) {
cacheName = getClass().getName();
} else {
cacheName = cacheConfig.cacheNames()[0];
}
return new StringBuilder(Constants.CACHE_NAMESPACE).append(cacheName).append(":").append(id).toString();
}
/** 获取缓存键值 */
protected String getCacheKey(Object id) {
String cacheName = null;
CacheConfig cacheConfig = getClass().getAnnotation(CacheConfig.class);
if (cacheConfig == null || cacheConfig.cacheNames() == null || cacheConfig.cacheNames().length < 1) {
cacheName = getClass().getName();
} else {
cacheName = cacheConfig.cacheNames()[0];
}
return new StringBuilder(Constants.CACHE_NAMESPACE).append(cacheName).append(":").append(id).toString();
}

public Page<T> query(Map<String, Object> params) {
return null;
}
public Page<T> query(Map<String, Object> params) {
return null;
}
}
Loading

0 comments on commit fb0b9be

Please sign in to comment.