优化缓存系统

v1.4.1
Parker 4 years ago
parent 9407145505
commit 92abe7a507

@ -54,24 +54,6 @@
<version>3.3.1</version>
</dependency>
<!--shiro-->
<!-- <dependency>-->
<!-- <groupId>org.apache.shiro</groupId>-->
<!-- <artifactId>shiro-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; shiro-redis &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.crazycake</groupId>-->
<!-- <artifactId>shiro-redis</artifactId>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.shiro</groupId>-->
<!-- <artifactId>shiro-core</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<!-- kaptcha -->
<dependency>
<groupId>com.github.axet</groupId>

@ -1,6 +1,7 @@
package org.opsli.core.cache.local;
import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
@ -39,6 +40,7 @@ import java.util.List;
@Component
public class CacheUtil {
private static final String JSON_KEY = "data";
/** 空状态 key 前缀 */
private static final String NIL_FLAG_PREFIX = "opsli:nil:";
@ -67,6 +69,18 @@ public class CacheUtil {
return CacheUtil.get(CacheConstants.HOT_DATA,key,vClass,true);
}
/**
*
* Key
* jksahdjh1j1hjk1
*
* @param key
* @return V
*/
public static Object get(String key){
return CacheUtil.get(CacheConstants.HOT_DATA,key,true);
}
/**
*
* Key
@ -80,6 +94,18 @@ public class CacheUtil {
return CacheUtil.get(CacheConstants.HOT_DATA,key,vClass,false);
}
/**
*
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @return V
*/
public static Object getByKeyOriginal(String key){
return CacheUtil.get(CacheConstants.HOT_DATA,key,false);
}
/**
* -
@ -94,6 +120,19 @@ public class CacheUtil {
return CacheUtil.get(CacheConstants.EDEN_DATA,key,vClass,true);
}
/**
* -
* Key
* jksahdjh1j1hjk1
*
* @param key
* @return V
*/
public static Object getEden(String key){
return CacheUtil.get(CacheConstants.EDEN_DATA,key,true);
}
/**
* -
* Key
@ -107,6 +146,18 @@ public class CacheUtil {
return CacheUtil.get(CacheConstants.EDEN_DATA,key,vClass,false);
}
/**
* -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @return V
*/
public static Object getEdenByKeyOriginal(String key){
return CacheUtil.get(CacheConstants.EDEN_DATA,key,false);
}
/**
* Hash
* Key
@ -121,6 +172,19 @@ public class CacheUtil {
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,vClass,true);
}
/**
* Hash
* Key
* jksahdjh1j1hjk1
*
* @param key
* @param field
* @return V
*/
public static Object getHash(String key, String field){
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,true);
}
/**
* Hash
* Key
@ -135,6 +199,19 @@ public class CacheUtil {
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,vClass,false);
}
/**
* Hash
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param field
* @return V
*/
public static Object getHashByKeyOriginal(String key, String field){
return CacheUtil.getHash(CacheConstants.EDEN_HASH_DATA,key,field,false);
}
/**
* -
* Key
@ -148,6 +225,18 @@ public class CacheUtil {
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,vClass,true);
}
/**
* -
* Key
* jksahdjh1j1hjk1
*
* @param keys -
* @return V
*/
public static List<Object> getAll(List<String> keys){
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,true);
}
/**
* -
* Key
@ -161,6 +250,18 @@ public class CacheUtil {
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,vClass,false);
}
/**
* -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param keys -
* @return V
*/
public static List<Object> getAllByKeyOriginal(List<String> keys){
return CacheUtil.getAll(CacheConstants.HOT_DATA,keys,false);
}
/**
* - -
* Key
@ -174,6 +275,18 @@ public class CacheUtil {
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,vClass,true);
}
/**
* - -
* Key
* jksahdjh1j1hjk1
*
* @param keys -
* @return V
*/
public static List<Object> getEdenAll(List<String> keys){
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,true);
}
/**
* - -
* Key
@ -187,6 +300,18 @@ public class CacheUtil {
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,vClass,false);
}
/**
* - -
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param keys -
* @return V
*/
public static List<Object> getEdenAllByKeyOriginal(List<String> keys){
return CacheUtil.getAll(CacheConstants.EDEN_DATA,keys,false);
}
/**
* Hash
* Key
@ -201,6 +326,19 @@ public class CacheUtil {
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,vClass,true);
}
/**
* Hash
* Key
* jksahdjh1j1hjk1
*
* @param key
* @param fields
* @return V
*/
public static List<Object> getHashAll(String key, List<String> fields){
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,true);
}
/**
* Hash
* Key
@ -215,6 +353,18 @@ public class CacheUtil {
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,vClass,false);
}
/**
* Hash
* Key
* Key opsli:hotData:ahdjksahjkd1
*
* @param key
* @param fields
* @return V
*/
public static List<Object> getHashAllByKeyOriginal(String key, List<String> fields){
return CacheUtil.getHashAll(CacheConstants.EDEN_HASH_DATA,key,fields,false);
}
/**
* Hash
@ -237,13 +387,55 @@ public class CacheUtil {
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
v = jsonObject.toJavaObject(vClass);
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
v = dataJson.toJavaObject(vClass);
}
}else{
jsonObject = (JSONObject) redisPlugin.hGet(key,field);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, jsonObject);
v = jsonObject.toJavaObject(vClass);
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
v = dataJson.toJavaObject(vClass);
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return v;
}
/**
* Hash
* @param cacheName
* @param key
* @param field
* @param keyFlag key
* @return
*/
private static Object getHash(String cacheName, String key, String field, boolean keyFlag){
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
Object v = null;
try {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
v = jsonObject.get(JSON_KEY);
}else{
jsonObject = (JSONObject) redisPlugin.hGet(key,field);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, jsonObject);
v = jsonObject.get(JSON_KEY);
}
}
}catch (Exception e){
@ -269,17 +461,54 @@ public class CacheUtil {
}
V v = null;
try {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
v = dataJson.toJavaObject(vClass);
}
}else{
jsonObject = (JSONObject) redisPlugin.get(key);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key, jsonObject);
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
v = dataJson.toJavaObject(vClass);
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return v;
}
/**
* Hash
* @param cacheName
* @param key
* @param keyFlag key
* @return
*/
private static Object get(String cacheName, String key, boolean keyFlag){
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
Object v = null;
try {
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
// 如果本地缓存为空 则去Redis中 再去取一次
if(jsonObject != null){
v = jsonObject.toJavaObject(vClass);
v = jsonObject.get(JSON_KEY);
}else{
jsonObject = (JSONObject) redisPlugin.get(key);
if(jsonObject != null){
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key, jsonObject);
v = jsonObject.toJavaObject(vClass);
v = jsonObject.get(JSON_KEY);
}
}
}catch (Exception e){
@ -307,10 +536,55 @@ public class CacheUtil {
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
vs.add(dataJson.toJavaObject(vClass));
}
} else {
nokeys.add(key);
}
}
// 如果本地缓存为空 则去Redis中 再去取一次
if(nokeys.size() > 0){
List<Object> objs = redisPlugin.getAll(nokeys);
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
vs.add(dataJson.toJavaObject(vClass));
}
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return vs;
}
/**
* -
* @param cacheName
* @param keys
* @param keyFlag key
* @return
*/
private static List<Object> getAll(String cacheName, List<String> keys, boolean keyFlag){
List<Object> vs = Lists.newArrayList();
try {
List<String> nokeys = Lists.newArrayList();
for (String key : keys) {
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key, JSONObject.class);
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
vs.add(jsonObject.get(JSON_KEY));
} else {
nokeys.add(key);
}
@ -321,7 +595,7 @@ public class CacheUtil {
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
vs.add(jsonObject.get(JSON_KEY));
}
}
}
@ -350,10 +624,56 @@ public class CacheUtil {
try {
List<Object> nofields = Lists.newArrayList();
for (String field : fields) {
JSONObject jsonObject;
jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
vs.add(dataJson.toJavaObject(vClass));
}
} else {
nofields.add(field);
}
}
// 如果本地缓存为空 则去Redis中 再去取一次
if(nofields.size() > 0){
List<Object> objs = redisPlugin.hMultiGet(key, nofields);
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
JSONObject dataJson = jsonObject.getJSONObject(JSON_KEY);
if(dataJson != null){
vs.add(dataJson.toJavaObject(vClass));
}
}
}
}
}catch (Exception e){
log.error(e.getMessage(),e);
}
return vs;
}
/**
* Hash -
* @param cacheName
* @param key
* @param fields
* @param keyFlag key
* @return
*/
private static List<Object> getHashAll(String cacheName,String key,List<String> fields, boolean keyFlag){
// 自动处理 key
if(keyFlag){
key = CacheUtil.handleKey(cacheName, key);
}
List<Object> vs = Lists.newArrayList();
try {
List<Object> nofields = Lists.newArrayList();
for (String field : fields) {
JSONObject jsonObject = ehCachePlugin.get(CacheConstants.HOT_DATA, key+":"+field, JSONObject.class);
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
vs.add(jsonObject.get(JSON_KEY));
} else {
nofields.add(field);
}
@ -364,7 +684,7 @@ public class CacheUtil {
for (Object obj : objs) {
JSONObject jsonObject = (JSONObject) obj;
if(jsonObject != null){
vs.add(jsonObject.toJavaObject(vClass));
vs.add(jsonObject.get(JSON_KEY));
}
}
}
@ -482,17 +802,15 @@ public class CacheUtil {
key = CacheUtil.handleKey(cacheName, key);
}
// 如果不是 JSONObject 则统一转换为 JSONObject
if(!(value instanceof JSONObject)){
String jsonStr = JSONObject.toJSONString(value);
value = JSONObject.parseObject(jsonStr);
}
// 则统一转换为 JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put(JSON_KEY, value);
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, value);
ehCachePlugin.put(CacheConstants.HOT_DATA, key+":"+field, jsonObject);
// 存入Redis
redisPlugin.hPut(key, field, value);
redisPlugin.hPut(key, field, jsonObject);
ret = true;
}catch (Exception e){
@ -519,14 +837,12 @@ public class CacheUtil {
key = CacheUtil.handleKey(cacheName, key);
}
// 如果不是 JSONObject 则统一转换为 JSONObject
if(!(value instanceof JSONObject)){
String jsonStr = JSONObject.toJSONString(value);
value = JSONObject.parseObject(jsonStr);
}
// 则统一转换为 JSONObject
JSONObject jsonObject = new JSONObject();
jsonObject.put(JSON_KEY, value);
// 存入EhCache
ehCachePlugin.put(CacheConstants.HOT_DATA,key, value);
ehCachePlugin.put(CacheConstants.HOT_DATA,key, jsonObject);
if(timeFlag){
@ -539,10 +855,10 @@ public class CacheUtil {
Double.valueOf(String.valueOf(ttlHotData * 2)).intValue());
}
// 存入Redis
redisPlugin.put(key, value, timeout);
redisPlugin.put(key, jsonObject, timeout);
}else {
// 存入Redis
redisPlugin.put(key, value);
redisPlugin.put(key, jsonObject);
}
ret = true;

@ -18,6 +18,16 @@ public enum MsgArgsType {
/** 字典操作类型 */
DICT_TYPE,
/** 用户ID */
USER_ID,
/** 用户名 */
USER_USERNAME,
/** 用户数据类型 */
USER_MODEL_TYPE,
/** 用户数据*/
USER_MODEL_DATA,
/** 缓存数据Key */
CACHE_DATA_KEY,
/** 缓存数据Value */

@ -12,11 +12,14 @@ public enum PushSubType {
/** 字典类型 */
DICT,
/** 用户数据 */
USER,
/** 热点数据 */
HOT_DATA,
/** 永久数据 */
EDEN_DATA
EDEN_DATA,
;

@ -0,0 +1,23 @@
package org.opsli.core.cache.pushsub.enums;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.cache.pushsub.enums
* @Author: Parker
* @CreateTime: 2020-09-16 22:28
* @Description:
*/
public enum UserModelType {
/** 用户模型 */
USER_MODEL,
/** 用户角色集合模型 */
USER_ROLES_MODEL,
/** 用户权限集合模型 */
USER_PERMS_MODEL,
/** 用户菜单集合模型 */
USER_MENU_MODEL,
;
}

@ -41,14 +41,17 @@ public class DictHandler implements RedisPushSubHandler{
if(DictModelType.COLLECTION == dictModelType){
Collection<Object> dicts = (Collection<Object>) msgJson.get(MsgArgsType.DICT_MODELS.toString());
for (Object dictObj : dicts) {
JSONObject jsonObject = (JSONObject) dictObj;
DictWrapper dictWrapperModel = JSONObject.toJavaObject(jsonObject, DictWrapper.class);
JSONObject jsonObject = msgJson.getJSONObject(MsgArgsType.DICT_MODEL.toString());
if(jsonObject == null) continue;
DictWrapper dictWrapperModel = jsonObject.toJavaObject(DictWrapper.class);
this.handler(dictWrapperModel, type);
}
} else if(DictModelType.OBJECT == dictModelType){
Object dictObj = msgJson.get(MsgArgsType.DICT_MODEL.toString());
JSONObject jsonObject = (JSONObject) dictObj;
DictWrapper dictWrapperModel = JSONObject.toJavaObject(jsonObject, DictWrapper.class);
JSONObject jsonObject = msgJson.getJSONObject(MsgArgsType.DICT_MODEL.toString());
if(jsonObject == null) return;
DictWrapper dictWrapperModel = jsonObject.toJavaObject(DictWrapper.class);
this.handler(dictWrapperModel, type);
}
}

@ -0,0 +1,179 @@
package org.opsli.core.cache.pushsub.handler;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.constants.CacheConstants;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.enums.MsgArgsType;
import org.opsli.core.cache.pushsub.enums.PushSubType;
import org.opsli.core.cache.pushsub.enums.UserModelType;
import org.opsli.core.utils.UserUtil;
import org.opsli.plugins.cache.EhCachePlugin;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.cache.pushsub.handler
* @Author: Parker
* @CreateTime: 2020-09-15 16:24
* @Description:
*/
@Slf4j
public class UserHandler implements RedisPushSubHandler{
@Autowired
EhCachePlugin ehCachePlugin;
@Override
public PushSubType getType() {
return PushSubType.USER;
}
@Override
public void handler(JSONObject msgJson) {
UserModelType userModelType = UserModelType.valueOf((String)
msgJson.get(MsgArgsType.USER_MODEL_TYPE.toString()));
// 用户刷新
if(UserModelType.USER_MODEL == userModelType){
this.userHandler(msgJson);
}
// 用户角色刷新
else if(UserModelType.USER_ROLES_MODEL == userModelType){
this.userRolesHandler(msgJson);
}
// 用户权限刷新
else if(UserModelType.USER_PERMS_MODEL == userModelType){
this.userPermsHandler(msgJson);
}
// 用户菜单刷新
else if(UserModelType.USER_MENU_MODEL == userModelType){
this.userMenusHandler(msgJson);
}
}
/**
*
* @param msgJson
*/
private void userHandler(JSONObject msgJson){
JSONObject data = msgJson.getJSONObject(MsgArgsType.USER_MODEL_DATA.toString());
// 数据为空则不执行
if(data == null) return;
// 获得数据
UserModel userModel = data.toJavaObject(UserModel.class);
// 获得用户ID 和 用户名
String userId = (String) msgJson.get(MsgArgsType.USER_ID.toString());
String username = (String) msgJson.get(MsgArgsType.USER_USERNAME.toString());
if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(username) ){
return;
}
// 先删除
ehCachePlugin.delete(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID + userId);
ehCachePlugin.delete(CacheConstants.HOT_DATA, UserUtil.PREFIX_USERNAME + username);
// 再赋值
ehCachePlugin.put(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID + userId, userModel);
ehCachePlugin.put(CacheConstants.HOT_DATA,UserUtil.PREFIX_USERNAME + username, userModel);
// 清除空拦截
CacheUtil.putNilFlag(UserUtil.PREFIX_ID + userId);
CacheUtil.putNilFlag(UserUtil.PREFIX_USERNAME + username);
}
/**
*
* @param msgJson
*/
private void userRolesHandler(JSONObject msgJson){
JSONArray dataArray = msgJson.getJSONArray(MsgArgsType.USER_MODEL_DATA.toString());
// 数据为空则不执行
if(dataArray == null || dataArray.isEmpty()) return;
// 获得用户ID
String userId = (String) msgJson.get(MsgArgsType.USER_ID.toString());
if(StringUtils.isEmpty(userId)){
return;
}
// 用户角色列表
List<String> roleCodes = dataArray.toJavaList(String.class);
if(roleCodes == null || roleCodes.isEmpty()){
return;
}
// 先删除
ehCachePlugin.delete(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_ROLES + userId);
// 存入缓存
ehCachePlugin.put(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_ROLES + userId, roleCodes);
// 清除空拦截
CacheUtil.putNilFlag(UserUtil.PREFIX_ID_ROLES + userId);
}
/**
*
* @param msgJson
*/
private void userPermsHandler(JSONObject msgJson){
JSONArray dataArray = msgJson.getJSONArray(MsgArgsType.USER_MODEL_DATA.toString());
// 数据为空则不执行
if(dataArray == null || dataArray.isEmpty()) return;
// 获得用户ID
String userId = (String) msgJson.get(MsgArgsType.USER_ID.toString());
if(StringUtils.isEmpty(userId)){
return;
}
// 用户权限列表
List<String> perms = dataArray.toJavaList(String.class);
if(perms == null || perms.isEmpty()){
return;
}
// 先删除
ehCachePlugin.delete(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_PERMISSIONS + userId);
// 存入缓存
ehCachePlugin.put(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_PERMISSIONS + userId, perms);
// 清除空拦截
CacheUtil.putNilFlag(UserUtil.PREFIX_ID_PERMISSIONS + userId);
}
/**
*
* @param msgJson
*/
private void userMenusHandler(JSONObject msgJson){
JSONArray dataArray = msgJson.getJSONArray(MsgArgsType.USER_MODEL_DATA.toString());
// 数据为空则不执行
if(dataArray == null || dataArray.isEmpty()) return;
// 获得用户ID
String userId = (String) msgJson.get(MsgArgsType.USER_ID.toString());
if(StringUtils.isEmpty(userId)){
return;
}
// 用户菜单列表
List<MenuModel> menus = dataArray.toJavaList(MenuModel.class);
if(menus == null || menus.isEmpty()){
return;
}
// 先删除
ehCachePlugin.delete(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_MENUS + userId);
// 存入缓存
ehCachePlugin.put(CacheConstants.HOT_DATA, UserUtil.PREFIX_ID_MENUS + userId, menus);
// 清除空拦截
CacheUtil.putNilFlag(UserUtil.PREFIX_ID_MENUS + userId);
}
}

@ -0,0 +1,98 @@
package org.opsli.core.cache.pushsub.msgs;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.core.cache.pushsub.enums.MsgArgsType;
import org.opsli.core.cache.pushsub.enums.PushSubType;
import org.opsli.core.cache.pushsub.enums.UserModelType;
import org.opsli.core.cache.pushsub.receiver.RedisPushSubReceiver;
import org.opsli.plugins.redis.pushsub.entity.BaseSubMessage;
import java.util.List;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.cache.pushsub.msgs
* @Author: Parker
* @CreateTime: 2020-09-15 16:50
* @Description:
*/
@Data
@Accessors(chain = true)
public final class UserMsgFactory extends BaseSubMessage{
/** 通道 */
private static final String CHANNEL = RedisPushSubReceiver.BASE_CHANNEL+RedisPushSubReceiver.CHANNEL;
private UserMsgFactory(){}
/**
* -
*/
public static BaseSubMessage createUserMsg(UserModel userModel){
BaseSubMessage baseSubMessage = new BaseSubMessage();
// 数据
JSONObject jsonObj = new JSONObject();
jsonObj.put(MsgArgsType.USER_ID.toString(), userModel.getId());
jsonObj.put(MsgArgsType.USER_USERNAME.toString(), userModel.getUsername());
jsonObj.put(MsgArgsType.USER_MODEL_TYPE.toString(), UserModelType.USER_MODEL.toString());
jsonObj.put(MsgArgsType.USER_MODEL_DATA.toString(), userModel);
// 用户
baseSubMessage.build(CHANNEL,PushSubType.USER.toString(),jsonObj);
return baseSubMessage;
}
/**
* -
*/
public static BaseSubMessage createUserRolesMsg(String userId, List<String> roles){
BaseSubMessage baseSubMessage = new BaseSubMessage();
// 数据
JSONObject jsonObj = new JSONObject();
jsonObj.put(MsgArgsType.USER_ID.toString(), userId);
jsonObj.put(MsgArgsType.USER_MODEL_TYPE.toString(), UserModelType.USER_ROLES_MODEL.toString());
jsonObj.put(MsgArgsType.USER_MODEL_DATA.toString(), roles);
// 用户
baseSubMessage.build(CHANNEL,PushSubType.USER.toString(),jsonObj);
return baseSubMessage;
}
/**
* -
*/
public static BaseSubMessage createUserPermsMsg(String userId, List<String> perms){
BaseSubMessage baseSubMessage = new BaseSubMessage();
// 数据
JSONObject jsonObj = new JSONObject();
jsonObj.put(MsgArgsType.USER_ID.toString(), userId);
jsonObj.put(MsgArgsType.USER_MODEL_TYPE.toString(), UserModelType.USER_PERMS_MODEL.toString());
jsonObj.put(MsgArgsType.USER_MODEL_DATA.toString(), perms);
// 用户
baseSubMessage.build(CHANNEL,PushSubType.USER.toString(),jsonObj);
return baseSubMessage;
}
/**
* -
*/
public static BaseSubMessage createUserMenusMsg(String userId, List<MenuModel> menus){
BaseSubMessage baseSubMessage = new BaseSubMessage();
// 数据
JSONObject jsonObj = new JSONObject();
jsonObj.put(MsgArgsType.USER_ID.toString(), userId);
jsonObj.put(MsgArgsType.USER_MODEL_TYPE.toString(), UserModelType.USER_MENU_MODEL.toString());
jsonObj.put(MsgArgsType.USER_MODEL_DATA.toString(), menus);
// 用户
baseSubMessage.build(CHANNEL,PushSubType.USER.toString(),jsonObj);
return baseSubMessage;
}
}

@ -100,7 +100,7 @@ public class RedisPushSubReceiver extends BaseReceiver {
JSONObject msgJson = JSONObject.parseObject(substring);
String type = (String) msgJson.get(BaseSubMessage.BASE_TYPE);
String identifier = (String) msgJson.get(BaseSubMessage.BASE_ID);
// 本机不广播
// TODO 本机不广播
// if(SystemInfo.INSTANCE.getSystemID().equals(identifier)){
// return;
// }

@ -120,7 +120,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(TokenException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public ResultVo<?> tokenException(LockedAccountException e) {
public ResultVo<?> tokenException(TokenException e) {
// Token 异常
return ResultVo.error(e.getMessage());
}

@ -31,6 +31,7 @@ public enum TokenMsg implements BaseMsg {
*
*/
EXCEPTION_USER_NULL(12200, "用户为空"),
EXCEPTION_CAPTCHA_NULL(12201, "验证码已失效"),
;

@ -3,6 +3,8 @@ package org.opsli.core.utils;
import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Producer;
import org.apache.commons.lang3.StringUtils;
import org.opsli.common.exception.TokenException;
import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -61,7 +63,7 @@ public class CaptchaUtil{
String codeTemp = (String) redisPlugin.get(PREFIX + uuid);
if(StringUtils.isEmpty(codeTemp)){
return false;
throw new TokenException(TokenMsg.EXCEPTION_CAPTCHA_NULL);
}
//删除验证码

@ -1,7 +1,5 @@
package org.opsli.core.utils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.base.result.ResultVo;
@ -11,8 +9,10 @@ import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.cache.pushsub.msgs.UserMsgFactory;
import org.opsli.core.msg.TokenMsg;
import org.opsli.plugins.redis.RedisLockPlugins;
import org.opsli.plugins.redis.RedisPlugin;
import org.opsli.plugins.redis.lock.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -31,15 +31,16 @@ import java.util.List;
public class UserUtil {
/** 超级管理员名称 */
private static final String SUPER_ADMIN = "system";
private static final String PREFIX_ID = "userId:";
private static final String PREFIX_ID_ROLES = "userId:roles:";
private static final String PREFIX_ID_PERMISSIONS = "userId:permissions:";
private static final String PREFIX_ID_MENUS = "userId:menus:";
private static final String PREFIX_USERNAME = "username:";
public static final String SUPER_ADMIN = "system";
public static final String PREFIX_ID = "userId:";
public static final String PREFIX_ID_ROLES = "userId:roles:";
public static final String PREFIX_ID_PERMISSIONS = "userId:permissions:";
public static final String PREFIX_ID_MENUS = "userId:menus:";
public static final String PREFIX_USERNAME = "username:";
/** Redis插件 */
private static RedisPlugin redisPlugin;
/** Redis分布式锁 */
private static RedisLockPlugins redisLockPlugins;
@ -80,6 +81,7 @@ public class UserUtil {
return userModel;
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID + userId);
@ -194,14 +196,15 @@ public class UserUtil {
*/
public static List<String> getUserRolesByUserId(String userId){
List<String> roles = null;
// 先从缓存里拿
List rolesObj = CacheUtil.get(PREFIX_ID_ROLES + userId, List.class);
if (rolesObj != null && !rolesObj.isEmpty()){
roles = Lists.newArrayListWithCapacity(rolesObj.size());
for (Object role : rolesObj) {
roles.add((String) role);
try {
roles = (List<String>) CacheUtil.get(PREFIX_ID_ROLES + userId);
if (roles != null && !roles.isEmpty()){
return roles;
}
return roles;
}catch (Exception e){
log.error(e.getMessage(), e);
}
// 拿不到 --------
@ -257,16 +260,18 @@ public class UserUtil {
*/
public static List<String> getUserAllPermsByUserId(String userId){
List<String> permissions = null;
// 先从缓存里拿
List permissionsObj = CacheUtil.get(PREFIX_ID_PERMISSIONS + userId, List.class);
if (permissionsObj != null && !permissionsObj.isEmpty()){
permissions = Lists.newArrayListWithCapacity(permissionsObj.size());
for (Object permission : permissionsObj) {
permissions.add((String) permission);
try {
permissions = (List<String>) CacheUtil.get(PREFIX_ID_PERMISSIONS + userId);
if (permissions != null && !permissions.isEmpty()){
return permissions;
}
return permissions;
}catch (Exception e){
log.error(e.getMessage(), e);
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_PERMISSIONS + userId);
@ -319,19 +324,18 @@ public class UserUtil {
*/
public static List<MenuModel> getMenuListByUserId(String userId){
List<MenuModel> menus = null;
// 先从缓存里拿
List menusObj = CacheUtil.get(PREFIX_ID_MENUS + userId, List.class);
if (menusObj != null && !menusObj.isEmpty()){
menus = Lists.newArrayListWithCapacity(menusObj.size());
for (Object menu : menusObj) {
JSONObject jsonObject = (JSONObject) menu;
if(jsonObject != null){
menus.add(jsonObject.toJavaObject(MenuModel.class));
}
try {
menus = (List<MenuModel>) CacheUtil.get(PREFIX_ID_MENUS + userId);
if (menus != null && !menus.isEmpty()){
return menus;
}
return menus;
}catch (Exception e){
log.error(e.getMessage(), e);
}
// 拿不到 --------
// 防止缓存穿透判断
boolean hasNilFlag = CacheUtil.hasNilFlag(PREFIX_ID_MENUS + userId);
@ -404,6 +408,11 @@ public class UserUtil {
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID + user.getId());
CacheUtil.putNilFlag(PREFIX_USERNAME + user.getUsername());
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMsg(user)
);
}
}
@ -419,14 +428,23 @@ public class UserUtil {
return;
}
List list = CacheUtil.get(PREFIX_ID_ROLES + userId, List.class);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_ROLES + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_ROLES + userId, list);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_ROLES + userId);
try {
List<String> list = (List<String>) CacheUtil.get(PREFIX_ID_ROLES + userId);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_ROLES + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_ROLES + userId, list);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_ROLES + userId);
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserRolesMsg(userId, roleCodes)
);
}
}catch (Exception e){
log.error(e.getMessage(), e);
}
}
@ -441,14 +459,23 @@ public class UserUtil {
return;
}
List list = CacheUtil.get(PREFIX_ID_PERMISSIONS + userId, List.class);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_PERMISSIONS + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_PERMISSIONS + userId, list);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_PERMISSIONS + userId);
try {
List<String> list = (List<String>) CacheUtil.get(PREFIX_ID_PERMISSIONS + userId);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_PERMISSIONS + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_PERMISSIONS + userId, permissions);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_PERMISSIONS + userId);
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserPermsMsg(userId, permissions)
);
}
}catch (Exception e){
log.error(e.getMessage(), e);
}
}
@ -463,14 +490,23 @@ public class UserUtil {
return;
}
List list = CacheUtil.get(PREFIX_ID_MENUS + userId, List.class);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_MENUS + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_MENUS + userId, list);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_MENUS + userId);
try {
List<MenuModel> list = (List<MenuModel>) CacheUtil.get(PREFIX_ID_MENUS + userId);
if(list != null && !list.isEmpty()){
// 先删除
CacheUtil.del(PREFIX_ID_MENUS + userId);
// 存入缓存
CacheUtil.put(PREFIX_ID_MENUS + userId, menus);
// 清除空拦截
CacheUtil.putNilFlag(PREFIX_ID_MENUS + userId);
// 发送通知消息
redisPlugin.sendMessage(
UserMsgFactory.createUserMenusMsg(userId, menus)
);
}
}catch (Exception e){
log.error(e.getMessage(), e);
}
}
@ -498,6 +534,11 @@ public class UserUtil {
// =====================================
@Autowired
public void setRedisPlugin(RedisPlugin redisPlugin) {
UserUtil.redisPlugin = redisPlugin;
}
@Autowired
public void setRedisLockPlugins(RedisLockPlugins redisLockPlugins) {
UserUtil.redisLockPlugins = redisLockPlugins;

@ -45,8 +45,19 @@ spring:
#driver-class-name: com.mysql.jdbc.Driver
# swagger 配置
swagger:
# knife4j 文档 配置
knife4j:
basic:
## 开启Swagger的Basic认证功能,默认是false
enable: true
## Basic认证用户名
username: doc
## Basic认证密码
password: 123456
# 是否生产环境 (生产环境默认屏蔽文档)
production: false

Loading…
Cancel
Save