问题来源:从缓存中取出的数据没有空格和换行,很难阅读,但又找不到像JSON一样的在线格式化工具。灵机一动,自己写了一个小程序将其格式化。
一、先看效果
1、原始数据长这样,难以阅读
MemCachedItem{同一申请客户Xh内登录的设备列表=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, list=[caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0]}}}, 同一用户近xd内登录时间段和次数=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={22=CountNumber {count=1, value=1, merged=false}}}}}, reference_time=1474640150272, primary_biz=PAY.BUY, expire_duration=31536000000, primary_tag=机构号加用户号, 同一用户近xM平台内部最大逾期天数=TimedItems {allItems={2016-09-23 22:14:14.423=MaxNumber {count=1, value=0, merged=false}, 2016-09-23 22:15:50.246=MaxNumber {count=1, value=0, merged=false}}}, 同一用户近xd内使用的IP地址和次数=TimedItems {allItems={2016-09-23 22:08:08.258={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.445={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.272={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户最近Xpd内操作APP时间段频率集合=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.440={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.266={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户最近登录时间列表=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, list=[Fri Sep 23 22:08:08 CST 2016]}}}, primary_key=123456-3177000000019572, 同一用户最近Xpd登陆的各设备指纹频率集合=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.440={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.266={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户Xd使用登录设备和次数=TimedItems {allItems={2016-09-24 00:00:00.000={merged=true, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}}}}
2、这样就好多了
MemCachedItem{
同一申请客户Xh内登录的设备列表=TimedItems{
allItems={
2016-09-2322:08:08.253={
merged=false,
limit=0,
list=[
caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0
]
}
}
},
同一用户近xd内登录时间段和次数=TimedItems{
allItems={
2016-09-2322:08:08.253={
merged=false,
limit=0,
map={
22=CountNumber{
count=1,
value=1,
merged=false
}
}
}
}
},
reference_time=1474640150272,
primary_biz=PAY.BUY,
expire_duration=31536000000,
primary_tag=机构号加用户号,
同一用户近xM平台内部最大逾期天数=TimedItems{
allItems={
2016-09-2322:14:14.423=MaxNumber{
count=1,
value=0,
merged=false
},
2016-09-2322:15:50.246=MaxNumber{
count=1,
value=0,
merged=false
}
}
},
同一用户近xd内使用的IP地址和次数=TimedItems{
allItems={
2016-09-2322:08:08.258={
merged=false,
limit=0,
map={
112.17.239.160=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:14:14.445={
merged=false,
limit=0,
map={
112.17.239.160=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:15:50.272={
merged=false,
limit=0,
map={
112.17.239.160=CountNumber{
count=1,
value=1,
merged=false
}
}
}
}
},
同一用户最近Xpd内操作APP时间段频率集合=TimedItems{
allItems={
2016-09-2322:08:08.253={
merged=false,
limit=0,
map={
8=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:14:14.440={
merged=false,
limit=0,
map={
8=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:15:50.266={
merged=false,
limit=0,
map={
8=CountNumber{
count=1,
value=1,
merged=false
}
}
}
}
},
同一用户最近登录时间列表=TimedItems{
allItems={
2016-09-2322:08:08.253={
merged=false,
limit=0,
list=[
FriSep2322:08:08CST2016
]
}
}
},
primary_key=123456-3177000000019572,
同一用户最近Xpd登陆的各设备指纹频率集合=TimedItems{
allItems={
2016-09-2322:08:08.253={
merged=false,
limit=0,
map={
caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:14:14.440={
merged=false,
limit=0,
map={
caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber{
count=1,
value=1,
merged=false
}
}
},
2016-09-2322:15:50.266={
merged=false,
limit=0,
map={
caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber{
count=1,
value=1,
merged=false
}
}
}
}
},
同一用户Xd使用登录设备和次数=TimedItems{
allItems={
2016-09-2400:00:00.000={
merged=true,
limit=0,
map={
caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber{
count=1,
value=1,
merged=false
}
}
}
}
}
}
二、编程实现
仔细观察一下缓存中的数据,发现只有大括号和中括号,并且成对出现。这不就是大学数据结构课程中典型的括号匹配算法的变形吗?既然是括号问题,就可以考虑用栈来实现。代码如下:
package test;
import java.util.Stack;
/**
* 格式化MemCachedItem,便于阅读
* -----------------------------------------
* @author Lynch 2016年9月24日 下午5:32:02
* -----------------------------------------
*/
public class CacheDataFormatUtil {
/**
* 用栈解决括号匹配问题,实现数据格式化
*
* @param str
* @return
*/
public static String getFormatCacheData(String str) {
Stack st = new Stack();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '{' || str.charAt(i) == '[') {
st.push(str.charAt(i));
sb.append(str.charAt(i));
sb.append('\n');
for (int j = 0; j < st.size(); j++) {
sb.append('\t');
}
} else if (str.charAt(i) == '}' || str.charAt(i) == ']') {
st.pop();
sb.append('\n');
for (int j = 0; j < st.size(); j++) {
sb.append('\t');
}
sb.append(str.charAt(i));
} else if (str.charAt(i) == ',') {
sb.append(str.charAt(i)).append("\n");
for (int j = 0; j < st.size(); j++) {
sb.append('\t');
}
} else if (str.charAt(i) != ' ') {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
// public static void main(String[] args) {
// String str = "MemCachedItem{同一申请客户Xh内登录的设备列表=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, list=[caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0]}}}, 同一用户近xd内登录时间段和次数=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={22=CountNumber {count=1, value=1, merged=false}}}}}, reference_time=1474640150272, primary_biz=PAY.BUY, expire_duration=31536000000, primary_tag=机构号加用户号, 同一用户近xM平台内部最大逾期天数=TimedItems {allItems={2016-09-23 22:14:14.423=MaxNumber {count=1, value=0, merged=false}, 2016-09-23 22:15:50.246=MaxNumber {count=1, value=0, merged=false}}}, 同一用户近xd内使用的IP地址和次数=TimedItems {allItems={2016-09-23 22:08:08.258={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.445={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.272={merged=false, limit=0, map={112.17.239.160=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户最近Xpd内操作APP时间段频率集合=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.440={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.266={merged=false, limit=0, map={8=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户最近登录时间列表=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, list=[Fri Sep 23 22:08:08 CST 2016]}}}, primary_key=123456-3177000000019572, 同一用户最近Xpd登陆的各设备指纹频率集合=TimedItems {allItems={2016-09-23 22:08:08.253={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:14:14.440={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}, 2016-09-23 22:15:50.266={merged=false, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}}}, 同一用户Xd使用登录设备和次数=TimedItems {allItems={2016-09-24 00:00:00.000={merged=true, limit=0, map={caZ8d17967b615ceb1V164793Zbb8gei147319824612056b949f73a22797cee0=CountNumber {count=1, value=1, merged=false}}}}}}";
// System.out.println(getFormatCacheData(str));
// }
}