org.xson.web.cache.vo.CacheBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xco-web Show documentation
Show all versions of xco-web Show documentation
xco-web is an easy to use control layer framework, is part of the SOA system, using xml language to describe the controller.
The newest version!
package org.xson.web.cache.vo;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.xson.common.object.XCO;
import org.xson.web.cache.CacheException;
import org.xson.web.ognl.var.VariableParser;
import org.xson.web.ognl.var.VariableVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class CacheBase {
protected CacheVo cacheVo;
// 服务
protected String service;
protected String simpleKey;
protected List keyList;
/**
* 需要忽略的cache id
*/
protected String[] ignore;
class CacheKey {
// 1: 常量, 2: ${url}, 3:${args}, 4:参数变量
int type;
// String name;
public Object name;
CacheKey(int type, Object name) {
this.type = type;
this.name = name;
}
}
protected CacheBase(CacheVo cacheVo, String[] ignore, String service) {
this.cacheVo = cacheVo;
this.ignore = ignore;
this.service = service;
}
/**
* 分析key: 原始数据
*/
protected void parseKey(String service, String key) {
List keyList = new ArrayList<>();
StringBuilder builder = new StringBuilder();
boolean simple = true;
char[] src = key.toCharArray();
int position = -1;
for (int i = 0; i < src.length; i++) {
char k = src[i];
switch (k) {
case '$':
if ((i + 1) < src.length && '{' == src[i + 1] && (position = findPosition(i + 1, src, '}')) > -1) {
if (builder.length() > 0) {
keyList.add(new CacheKey(1, builder.toString()));
builder = new StringBuilder();
}
// 找到${}
String str = new String(src, i + 2, position - i - 2);
// if ("service".equalsIgnoreCase(str)) {
if ("url".equalsIgnoreCase(str)) {
keyList.add(new CacheKey(2, service));
} else if ("args".equalsIgnoreCase(str)) {
keyList.add(new CacheKey(3, null));
simple = false;
} else {
throw new CacheException("不合法的表达式标签: ${" + str + "}");
}
// i = position + 1;
i = position;// fix bug
} else {
builder.append(k);
}
break;
case '{':
if ((i + 1) < src.length && (position = findPosition(i + 1, src, '}')) > -1) {
if (builder.length() > 0) {
keyList.add(new CacheKey(1, builder.toString()));
builder = new StringBuilder();
}
// 找到{}
String str = new String(src, i + 1, position - i - 1);
keyList.add(new CacheKey(4, VariableParser.parse(str, true)));
simple = false;
// i = position + 1;
i = position;// fix bug
} else {
builder.append(k);
}
break;
default:
builder.append(k);
}
}
if (builder.length() > 0) {
keyList.add(new CacheKey(1, builder.toString()));
}
if (simple) {
builder = new StringBuilder();
for (CacheKey ck : keyList) {
builder.append(ck.name);
}
this.simpleKey = builder.toString();
} else {
this.keyList = keyList;
}
}
private int findPosition(int index, char[] src, char desc) {
for (int i = index; i < src.length; i++) {
if (src[i] == desc) {
return i;
}
}
return -1;
}
@SuppressWarnings("rawtypes")
protected String buildKey(Object obj) {
if (null != simpleKey) {
return simpleKey;
} else {
StringBuilder builder = new StringBuilder();
for (CacheKey cacheKey : keyList) {
if (1 == cacheKey.type || 2 == cacheKey.type) {
builder.append(cacheKey.name);
} else if (3 == cacheKey.type) {
if (null == obj) {
builder.append("null");// MD5
} else if (obj instanceof XCO) {
builder.append(MD5(((XCO) obj).toString()));
} else if (obj instanceof Map) {
builder.append(MD5(JSON.toJSONString((Map) obj)));
} else if (obj instanceof JSONObject) {
builder.append(MD5(((JSONObject) obj).toJSONString()));
} else {
throw new CacheException("不支持的key参数类型:" + obj.getClass());
}
} else if (4 == cacheKey.type) {
if (null == obj) {
throw new CacheException("变量模式下参数不能为空");
} else if (obj instanceof XCO) {
builder.append(((VariableVo) cacheKey.name).getValue(obj));
} else if (obj instanceof Map) {
builder.append(((VariableVo) cacheKey.name).getValue(obj));
} else if (obj instanceof JSONObject) {
builder.append(((JSONObject) obj).get(((VariableVo) cacheKey.name).getOriginal()));
} else {
throw new CacheException("不支持的key参数类型:" + obj.getClass());
}
}
}
return builder.toString();
}
}
private char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private String MD5(String s) {
return MD5(s.getBytes());
}
private String MD5(byte[] btInput) {
try {
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str).substring(8, 24);// 16位
} catch (Exception e) {
throw new CacheException("MD5处理失败", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy