添加缓存代码

dev
lixiangrong 2016-05-23 09:39:48 +08:00
parent 5615898dd0
commit 9078eb70f9
9 changed files with 717 additions and 225 deletions

View File

@ -0,0 +1,71 @@
package osc.git.eh3.cache;
/**
* <p>
* Title:
* </p>
*
* <p>
* Description: DTO
* </p>
*
* <p>
* Copyright: Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* @author Deepblue 2008-11-11
* @version 1.0
*/
public class Cache {
private String key;// 缓存ID
private Object value;// 缓存数据
private long timeOut;// 更新时间
private boolean expired; // 是否终止
public Cache() {
super();
}
public Cache(String key, Object value, long timeOut, boolean expired) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
this.expired = expired;
}
public String getKey() {
return key;
}
public long getTimeOut() {
return timeOut;
}
public Object getValue() {
return value;
}
public void setKey(String string) {
key = string;
}
public void setTimeOut(long l) {
timeOut = l;
}
public void setValue(Object object) {
value = object;
}
public boolean isExpired() {
return expired;
}
public void setExpired(boolean b) {
expired = b;
}
}

View File

@ -0,0 +1,220 @@
package osc.git.eh3.cache;
import java.util.*;
import java.util.Map.Entry;
/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
* Deep blue 2008-11-28 think
* chche
* <p>
* Copyright: Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* @author Deepblue 2008-11-11
* @version 1.0
*/
public class CacheManager {
private static HashMap<String, Object> cacheMap = new HashMap<String, Object>();
// 单实例构造方法
private CacheManager() {
super();
}
// 获取布尔值的缓存
public static boolean getSimpleFlag(String key) {
try {
return (Boolean) cacheMap.get(key);
} catch (NullPointerException e) {
return false;
}
}
public static long getServerStartdt(String key) {
try {
return (Long) cacheMap.get(key);
} catch (Exception ex) {
return 0;
}
}
// 设置布尔值的缓存
public synchronized static boolean setSimpleFlag(String key, boolean flag) {
if (flag && getSimpleFlag(key)) {// 假如为真不允许被覆盖
return false;
} else {
cacheMap.put(key, flag);
return true;
}
}
public synchronized static boolean setSimpleFlag(String key, long serverbegrundt) {
if (cacheMap.get(key) == null) {
cacheMap.put(key, serverbegrundt);
return true;
} else {
return false;
}
}
// 得到缓存。同步静态方法
private synchronized static Cache getCache(String key) {
return (Cache) cacheMap.get(key);
}
// 判断是否存在一个缓存
private synchronized static boolean hasCache(String key) {
return cacheMap.containsKey(key);
}
// 清除所有缓存
public synchronized static void clearAll() {
cacheMap.clear();
}
// 清除某一类特定缓存,通过遍历HASHMAP下的所有对象来判断它的KEY与传入的TYPE是否匹配
public synchronized static void clearAll(String type) {
Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
String key;
ArrayList<String> arr = new ArrayList<String>();
try {
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
key = (String) entry.getKey();
if (key.startsWith(type)) { // 如果匹配则删除掉
arr.add(key);
}
}
for (int k = 0; k < arr.size(); k++) {
clearOnly(arr.get(k));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 清除指定的缓存
public synchronized static void clearOnly(String key) {
cacheMap.remove(key);
}
// 载入缓存
public synchronized static void putCache(String key, Cache obj) {
cacheMap.put(key, obj);
}
// 获取缓存信息
public static Cache getCacheInfo(String key) {
if (hasCache(key)) {
Cache cache = getCache(key);
if (cacheExpired(cache)) { // 调用判断是否终止方法
cache.setExpired(true);
}
return cache;
} else {
return null;
}
}
// 载入缓存信息
public static void putCacheInfo(String key, Cache obj, long dt, boolean expired) {
Cache cache = new Cache();
cache.setKey(key);
cache.setTimeOut(dt + System.currentTimeMillis()); // 设置多久后更新缓存
cache.setValue(obj);
cache.setExpired(expired); // 缓存默认载入时终止状态为FALSE
cacheMap.put(key, cache);
}
// 重写载入缓存信息方法
public static void putCacheInfo(String key, Cache obj, long dt) {
Cache cache = new Cache();
cache.setKey(key);
cache.setTimeOut(dt + System.currentTimeMillis());
cache.setValue(obj);
cache.setExpired(false);
cacheMap.put(key, cache);
}
// 判断缓存是否终止
public static boolean cacheExpired(Cache cache) {
if (null == cache) { // 传入的缓存不存在
return false;
}
long nowDt = System.currentTimeMillis(); // 系统当前的毫秒数
long cacheDt = cache.getTimeOut(); // 缓存内的过期毫秒数
if (cacheDt <= 0 || cacheDt > nowDt) { // 过期时间小于等于零时,或者过期时间大于当前时间时则为FALSE
return false;
} else { // 大于过期时间 即过期
return true;
}
}
// 获取缓存中的大小
public static int getCacheSize() {
return cacheMap.size();
}
// 获取指定的类型的大小
public static int getCacheSize(String type) {
int k = 0;
Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
String key;
try {
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
key = (String) entry.getKey();
if (key.indexOf(type) != -1) { // 如果匹配则删除掉
k++;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return k;
}
// 获取缓存对象中的所有键值名称
public static ArrayList<String> getCacheAllkey() {
ArrayList<String> a = new ArrayList<String>();
try {
Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
a.add((String) entry.getKey());
}
} catch (Exception ex) {
}
return a;
}
// 获取缓存对象中指定类型 的键值名称
public static ArrayList<String> getCacheListkey(String type) {
ArrayList<String> a = new ArrayList<String>();
String key;
try {
Iterator<Entry<String, Object>> i = cacheMap.entrySet().iterator();
while (i.hasNext()) {
Entry<String, Object> entry = i.next();
key = (String) entry.getKey();
if (key.indexOf(type) != -1) {
a.add(key);
}
}
} catch (Exception ex) {
}
return a;
}
}

View File

@ -0,0 +1,50 @@
package osc.git.eh3.cache;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public final class SimpleCache<K, V> {
private final Lock lock = new ReentrantLock();
private final int maxCapacity;
private final Map<K, V> eden;
private final Map<K, V> perm;
public SimpleCache(int maxCapacity) {
this.maxCapacity = maxCapacity;
this.eden = new ConcurrentHashMap<K, V>(maxCapacity);
this.perm = new WeakHashMap<K, V>(maxCapacity);
}
public V get(K k) {
V v = this.eden.get(k);
if (v == null) {
lock.lock();
try {
v = this.perm.get(k);
} finally {
lock.unlock();
}
if (v != null) {
this.eden.put(k, v);
}
}
return v;
}
public void put(K k, V v) {
if (this.eden.size() >= maxCapacity) {
lock.lock();
try {
this.perm.putAll(this.eden);
} finally {
lock.unlock();
}
this.eden.clear();
}
this.eden.put(k, v);
}
}

View File

@ -0,0 +1,17 @@
package osc.git.eh3.cache;
public class Test {
public static void main(String[] args) {
SimpleCache<String, Object> cache = new SimpleCache<>(5);
for (int i = 0; i < 10; i++) {
cache.put(""+i, i);
}
for (int i = 0; i < 10; i++) {
System.out.println(cache.get(i+""));
}
}
}

View File

@ -0,0 +1,32 @@
package osc.git.eh3.cache;
import java.util.ArrayList;
public class TestCache {
public static void main(String[] args) {
System.out.println(CacheManager.getSimpleFlag("alksd"));
CacheManager.putCache("abc", new Cache("key", "value", 10, false));
CacheManager.putCache("def", new Cache());
CacheManager.putCache("ccc", new Cache());
CacheManager.clearOnly("");
Cache c = new Cache();
for (int i = 0; i < 10; i++) {
CacheManager.putCache("" + i, c);
}
CacheManager.putCache("aaaaaaaa", c);
CacheManager.putCache("abchcy;alskd", c);
CacheManager.putCache("cccccccc", c);
CacheManager.putCache("abcoqiwhcy", c);
System.out.println("删除前的大小:" + CacheManager.getCacheSize());
ArrayList<String> cacheAllkey = CacheManager.getCacheAllkey();
for (String key : cacheAllkey) {
System.out.println(key + ":" + CacheManager.getCacheInfo(key).getValue());
}
CacheManager.clearAll("aaaa");
System.out.println("删除后的大小:" + CacheManager.getCacheSize());
cacheAllkey = CacheManager.getCacheAllkey();
for (String key : cacheAllkey) {
System.out.println(key);
}
}
}

View File

@ -0,0 +1,11 @@
package osc.git.eh3.test;
import osc.git.eh3.utils.HttpClientUtil;
public class TestAudiQ2 {
public static String URL = "http://127.0.0.1:8080/Audi2016Q2Wap/getUserInfo";
public static void main(String[] args) throws Exception {
System.out.println(HttpClientUtil.sendPostJSONData(URL, ""));
}
}

View File

@ -1,24 +1,17 @@
package osc.git.eh3.test;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import osc.git.eh3.utils.AESTool;
import osc.git.eh3.utils.Base64;
public class TestCode {
public static void main(String[] args) throws Exception {
@ -39,14 +32,14 @@ public class TestCode {
// Map<String, Object> resultMap = new HashMap<String, Object>();
// System.out.println((String)resultMap.get("dd"));
// try {
// String str = null;
// str.equals("");
// } catch (Exception e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
// System.out.println("fffff");
// try {
// String str = null;
// str.equals("");
// } catch (Exception e) {
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
// System.out.println("fffff");
// String[] s = {"111","eee"};
// System.out.println(Arrays.toString(s));
@ -123,209 +116,272 @@ public class TestCode {
// JSONObject jj = new JSONObject();
// System.out.println(jj.optString("pring"));
// // 根据网卡取本机配置的IP
// InetAddress inet = null;
// try {
// inet = InetAddress.getLocalHost();
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// String ipAddress = inet.getHostAddress();
//
// System.out.println(ipAddress);
// TestCode test = new TestCode();
// System.out.println(test.dd("ddd"));
// // 根据网卡取本机配置的IP
// InetAddress inet = null;
// try {
// inet = InetAddress.getLocalHost();
// } catch (UnknownHostException e) {
// e.printStackTrace();
// }
// String ipAddress = inet.getHostAddress();
//
// System.out.println(ipAddress);
// Package pkg = Package.getPackage("osc.git.eh3.test");
// Annotation[] annotations = pkg.getAnnotations();
// for (Annotation annotation : annotations) {
// System.out.println(annotation);
// TestCode test = new TestCode();
// System.out.println(test.dd("ddd"));
// Package pkg = Package.getPackage("osc.git.eh3.test");
// Annotation[] annotations = pkg.getAnnotations();
// for (Annotation annotation : annotations) {
// System.out.println(annotation);
// }
// String[] arrs = new String[]{"111","111","2222"};
// for (String string : Array2Set(arrs)) {
//
// System.out.println(string);
// }
// Class<?> clazz = StatisByHourModel.class;
// Method[] methods = clazz.getMethods();
// for (Method method : methods) {
// System.out.println(method.getName());
// }
// Object dd = new Date();
//
// System.out.println(dd instanceof Date);
//
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd
// HH:mm:ss.SSS");
// System.out.println(sdf.format(dd));
// JSONObject groupAdxs =
// JSONObject.fromObject("{\"4ebdb328-5d4b-42e6-80c3-a6aaaecdcea1\":[\"1e03319c-425d-4a17-a6bf-eeec2f48db29\",\"1fed4171-9925-4834-aa7b-9b4d3a58841b\",\"ce579246-e707-4cb9-b982-88cad7944b92\"],\"9262cbe8-a9dc-4f4e-888b-cf3ffe65defd\":\"ce579246-e707-4cb9-b982-88cad7944b92\"}");
// Set<String> keySet = groupAdxs.keySet();
// for (Object object : keySet) {
// System.out.println(groupAdxs.get(object).getClass().isArray());
// }
// System.out.println(UUID.randomUUID().toString());
// System.out.println(new Integer(0x11));
// System.out.println(Integer.toBinaryString(30000));
// System.out.println(Integer.valueOf("11", 16));
// System.out.println(Integer.valueOf("11", 2));
// System.out.println(AESTool.encrypt("lixiangrong"));
// System.out.println(AESTool.decrypt(AESEncrypter.encrypt("lixiangrong")));
// System.out.println(AESTool.encrypt("liixangrong","adjdjfjfjfjdkdkd"));
// System.out.println(AESTool.decrypt("bfb0c038342ffead45511879853279bf","adjdjfjfjfjdkdkd"));
// System.out.println(Base64.encodeToString(AESTool.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202").getBytes(),
// false));
// byte[] bytes = "lixiangrong".getBytes();
// for (int i = 0; i < bytes.length; i++) {
// System.out.println(bytes[i]);
// }
// System.out.println(Base64.encodeToString("lixiangrong".getBytes(),
// false));
// double lon1 = 109.0145193759;
// double lat1 = 34.236080797698;
// System.out.println(GeoHash.encode(lat1, lon1));
// System.out.println(GeoHash.decode("wmtdgn5esrb1")[0]+"
// "+GeoHash.decode("wmtdgn5esrb1")[1]);
// String url =
// "http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=json&ak=LCG4dyrXyadeD8hFhi8SGCv6";
// System.out.println(HttpClientUtil.sendGet(url));
// JSONArray array = new JSONArray();
// array.add("1");
// array.add("2");
// array.add("3");
// array.add("4");
// array.add("5");
// List<String> list = JSONArray.toList(array, new String(), new
// JsonConfig());
// System.out.println(list);
// System.out.println(System.nanoTime());
// System.out.println(System.nanoTime());
// Map<String, String> postParam = new HashMap<String, String>();
// postParam.put("groupid", "100003");
// postParam.put("count", "1");
// postParam.put("type", "m");
// for(int i=0;i<5;i++){
// try {
// HttpClientUtil.sendPostParam("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult",
// postParam);
//// HttpClientUtil.sendPost("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult",
// "groupid=100003&count=1&type=m");
// break;
// } catch (Exception e) {
// System.out.println(e.getMessage());
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
// }
// String str = "0,";
// System.out.println(str.split(",").length);
// System.out.println(JedisUtil.getStr("0000"));
// Map<String,Integer> result = new HashMap<String, Integer>();
// System.out.println(result.get("jj"));
// double budgets = 10000;
// System.out.println((budgets/100));
// String str = null;
// BigDecimal budget = new BigDecimal(str);
// budget = budget.subtract(new BigDecimal(10));
// if (budget.compareTo(new BigDecimal(0)) <= 0) {
// System.out.println("1");
// } else {
// System.out.println("2");
// }
// System.out.println(budget.doubleValue());
// String REG_FLOAT = "^[1-9]\\d*.?\\d+$"; // 浮点正数
// System.out.println(Pattern.compile(REG_FLOAT).matcher("1.21").matches());
// String str ="浮点数sss";
// String s1 = new String(str.getBytes("utf-8"),"gbk");
// System.out.println(s1);
// System.out.println(new String(s1.getBytes("gbk")));
// System.out.println();
//
// String s2 = URLEncoder.encode(str, "utf-8");
// System.out.println(s2);
// System.out.println(URLDecoder.decode(s2,"utf-8"));
// Object object = null;
// JSONObject creativeGroupObj = JSONObject.fromObject(object);
// System.out.println(creativeGroupObj.isEmpty());
//
// System.out.println(UUID.randomUUID().toString());
// JSONArray putTime =
// JSONArray.fromObject("[{\"monday\":[\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"tuesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"wednesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"thursday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"friday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"saturday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"sunday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]}]");
// JSONArray periods = new JSONArray();
// for (Object object : putTime) {
// JSONObject putTimeObj = JSONObject.fromObject(object);
// if (!putTimeObj.isEmpty()) {
// Set<String> keySet = putTimeObj.keySet();
// JSONObject period = new JSONObject();
// for (String key : keySet) {
// JSONArray value = putTimeObj.optJSONArray(key);
// int start = -1,end = -1;
// StringBuffer sb = new StringBuffer();
// for (int i = 0; i < value.size(); i++) {
// Object object2 = value.get(i);
// // 第一次出现 1
// if (object2.equals("1") && start==-1) {
// start=i;
// end = 0;
// }
// // 出现1后的第一次出现0结束
// if (object2.equals("0") && start>-1) {
// end=i-1;
// sb.append(start+"-"+end+",");
// start = -1;end = -1;
// }
// }
// period.put("week", key);
// period.put("ranges",sb.toString().substring(0, (sb.length()-1)));
// }
// periods.add(period);
// }
// }
// System.out.println(periods.toString());
// JSONObject period = new JSONObject();
// period.put("test", 100.32);
// System.out.println(period.optString("test"));
// BigDecimal clicks = new BigDecimal(100.23);
// System.out.println(clicks.intValue());
// System.out.println(Long.parseLong("8000.01"));
// JSONObject jsonParam = new JSONObject();
// JSONArray jsonArray = new JSONArray();
// jsonArray.add("000000");
// jsonParam.put("app", jsonArray);
// System.out.println(jsonParam);
// String head = "00,";
// head = head.substring(0, head.lastIndexOf(","));
// System.out.println(head);
// BigDecimal bid = new BigDecimal("").divide(BigDecimal.valueOf(100),0,
// BigDecimal.ROUND_DOWN);
// System.out.println(bid);
// System.out.println(UUID.randomUUID().toString());
// final Map<String, String> srt = new HashMap<String,String>();
// srt.put("1", "1111");
// System.out.println(srt);
// String ff = null;
// System.out.println(String.valueOf(ff));
// Map<String, String[]> map = new HashMap<>();
// String[] sarr = { "0397e36c-e5c3-4d96-825e-acf668b92e9b",
// "0397e36c-e5c3-4d96-825e-acf668b92e9b" };
// String[] sarr1 = { "0397e36c-e5c3-4d96-825e-acf668b92e9b"};
// map.put("test[]", sarr);
// map.put("test1", sarr1);
// Map<String, Object> formatMap = formatMap(map);
// System.out.println(formatMap.get("fffffffff"));
// List<String> ssStrings = new ArrayList<>();
// ssStrings.add("1");
// ssStrings.add("2");
// ssStrings.add("3");
// ssStrings.add("4");
// System.out.println(ssStrings.toString());
// String ip = "127,0,0,1";
// // String [] s = ip.split(".");
// String[] s = ip.split(",");
// for (String string : s) {
// System.out.println(string + "-");
// }
// String[] arrs = new String[]{"111","111","2222"};
// for (String string : Array2Set(arrs)) {
//
// System.out.println(string);
// }
// Class<?> clazz = StatisByHourModel.class;
// Method[] methods = clazz.getMethods();
// for (Method method : methods) {
// System.out.println(method.getName());
// }
// Object dd = new Date();
//
// System.out.println(dd instanceof Date);
//
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
// System.out.println(sdf.format(dd));
// JSONObject groupAdxs = JSONObject.fromObject("{\"4ebdb328-5d4b-42e6-80c3-a6aaaecdcea1\":[\"1e03319c-425d-4a17-a6bf-eeec2f48db29\",\"1fed4171-9925-4834-aa7b-9b4d3a58841b\",\"ce579246-e707-4cb9-b982-88cad7944b92\"],\"9262cbe8-a9dc-4f4e-888b-cf3ffe65defd\":\"ce579246-e707-4cb9-b982-88cad7944b92\"}");
// Set<String> keySet = groupAdxs.keySet();
// for (Object object : keySet) {
// System.out.println(groupAdxs.get(object).getClass().isArray());
// }
// System.out.println(UUID.randomUUID().toString());
// System.out.println(new Integer(0x11));
// System.out.println(Integer.toBinaryString(30000));
// System.out.println(Integer.valueOf("11", 16));
// System.out.println(Integer.valueOf("11", 2));
// System.out.println(AESTool.encrypt("lixiangrong"));
// System.out.println(AESTool.decrypt(AESEncrypter.encrypt("lixiangrong")));
// System.out.println(AESTool.encrypt("liixangrong","adjdjfjfjfjdkdkd"));
// System.out.println(AESTool.decrypt("bfb0c038342ffead45511879853279bf","adjdjfjfjfjdkdkd"));
// System.out.println(Base64.encodeToString(AESTool.encrypt("fa4d7d90618dcba5fa1d969cffc04def","002020202").getBytes(), false));
// byte[] bytes = "lixiangrong".getBytes();
// for (int i = 0; i < bytes.length; i++) {
// System.out.println(bytes[i]);
// }
// System.out.println(Base64.encodeToString("lixiangrong".getBytes(), false));
// double lon1 = 109.0145193759;
// double lat1 = 34.236080797698;
// System.out.println(GeoHash.encode(lat1, lon1));
// System.out.println(GeoHash.decode("wmtdgn5esrb1")[0]+" "+GeoHash.decode("wmtdgn5esrb1")[1]);
// String url = "http://api.map.baidu.com/place/v2/search?query=银行&location=39.915,116.404&radius=2000&output=json&ak=LCG4dyrXyadeD8hFhi8SGCv6";
// System.out.println(HttpClientUtil.sendGet(url));
// JSONArray array = new JSONArray();
// array.add("1");
// array.add("2");
// array.add("3");
// array.add("4");
// array.add("5");
// List<String> list = JSONArray.toList(array, new String(), new JsonConfig());
// System.out.println(list);
// System.out.println(System.nanoTime());
// System.out.println(System.nanoTime());
// Map<String, String> postParam = new HashMap<String, String>();
// postParam.put("groupid", "100003");
// postParam.put("count", "1");
// postParam.put("type", "m");
// for(int i=0;i<5;i++){
// try {
// HttpClientUtil.sendPostParam("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult", postParam);
//// HttpClientUtil.sendPost("http://192.168.1.135:8080/dsp-counter/remote/chargeCounter/counterControlForThisSumResult", "groupid=100003&count=1&type=m");
// break;
// } catch (Exception e) {
// System.out.println(e.getMessage());
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
// }
// }
// String str = "0,";
// System.out.println(str.split(",").length);
// System.out.println(JedisUtil.getStr("0000"));
// Map<String,Integer> result = new HashMap<String, Integer>();
// System.out.println(result.get("jj"));
// double budgets = 10000;
// System.out.println((budgets/100));
// String str = null;
// BigDecimal budget = new BigDecimal(str);
// budget = budget.subtract(new BigDecimal(10));
// if (budget.compareTo(new BigDecimal(0)) <= 0) {
// System.out.println("1");
// } else {
// System.out.println("2");
// }
// System.out.println(budget.doubleValue());
// String REG_FLOAT = "^[1-9]\\d*.?\\d+$"; // 浮点正数
// System.out.println(Pattern.compile(REG_FLOAT).matcher("1.21").matches());
// String str ="浮点数sss";
// String s1 = new String(str.getBytes("utf-8"),"gbk");
// System.out.println(s1);
// System.out.println(new String(s1.getBytes("gbk")));
// System.out.println();
//
// String s2 = URLEncoder.encode(str, "utf-8");
// System.out.println(s2);
// System.out.println(URLDecoder.decode(s2,"utf-8"));
// Object object = null;
// JSONObject creativeGroupObj = JSONObject.fromObject(object);
// System.out.println(creativeGroupObj.isEmpty());
//
// System.out.println(UUID.randomUUID().toString());
// JSONArray putTime = JSONArray.fromObject("[{\"monday\":[\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"tuesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"wednesday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"thursday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"friday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"saturday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]},{\"sunday\":[\"0\",\"0\",\"0\",\"0\",\"1\",\"1\",\"1\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]}]");
// JSONArray periods = new JSONArray();
// for (Object object : putTime) {
// JSONObject putTimeObj = JSONObject.fromObject(object);
// if (!putTimeObj.isEmpty()) {
// Set<String> keySet = putTimeObj.keySet();
// JSONObject period = new JSONObject();
// for (String key : keySet) {
// JSONArray value = putTimeObj.optJSONArray(key);
// int start = -1,end = -1;
// StringBuffer sb = new StringBuffer();
// for (int i = 0; i < value.size(); i++) {
// Object object2 = value.get(i);
// // 第一次出现 1
// if (object2.equals("1") && start==-1) {
// start=i;
// end = 0;
// }
// // 出现1后的第一次出现0结束
// if (object2.equals("0") && start>-1) {
// end=i-1;
// sb.append(start+"-"+end+",");
// start = -1;end = -1;
// }
// }
// period.put("week", key);
// period.put("ranges",sb.toString().substring(0, (sb.length()-1)));
// }
// periods.add(period);
// }
// }
// System.out.println(periods.toString());
// JSONObject period = new JSONObject();
// period.put("test", 100.32);
// System.out.println(period.optString("test"));
// BigDecimal clicks = new BigDecimal(100.23);
// System.out.println(clicks.intValue());
// System.out.println(Long.parseLong("8000.01"));
// JSONObject jsonParam = new JSONObject();
// JSONArray jsonArray = new JSONArray();
// jsonArray.add("000000");
// jsonParam.put("app", jsonArray);
// System.out.println(jsonParam);
String head = "00,";
head = head.substring(0, head.lastIndexOf(","));
System.out.println(head);
String string = "666";
System.out.println(isNumeric(string));
}
public static boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("^(\\d+\\.?\\d+)|\\d+$");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
public static Map<String, Object> formatMap(Map<String, String[]> map) {
Map<String, Object> result = new HashMap<String, Object>();
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String[] values = map.get(key);
if (key.contains("[") || key.contains("]")) {
result.put(key.replace("[", "").replace("]", ""), Arrays.toString(values).replace("[", "").replace("]", ""));
} else {
result.put(key, Arrays.toString(values).replace("[", "").replace("]", ""));
}
}
return result;
}
public static Long parseDate(String s) {
@ -343,17 +399,17 @@ public class TestCode {
}
return time;
}
public <B> B dd(B t){
public <B> B dd(B t) {
return t;
}
public static <T extends Object> Set<T> Array2Set(T[] tArray) {
Set<T> tSet = new HashSet<T>(Arrays.asList(tArray));
return tSet;
}
public static <T extends Object> void printArrs(T[] arrs){
public static <T extends Object> void printArrs(T[] arrs) {
for (T t : arrs) {
System.out.println(t);
}

View File

@ -0,0 +1,34 @@
package osc.git.eh3.testminisite;
import java.util.HashMap;
import java.util.Map;
import osc.git.eh3.utils.HttpClientUtil;
public class TestMinisite {
public static String URL = "http://127.0.0.1:8080/dsp-minisite/Audi2016Q2/getProvinces";
public static void main(String[] args) throws Exception {
Map<String, String> params = new HashMap<>();
params.put("project","1");
params.put("bid","1");
params.put("mapid","1");
params.put("name","1");
params.put("gender","1");
params.put("tel","1");
params.put("favoriteModel","1");
params.put("province","1");
params.put("city","1");
params.put("dealerName","1");
params.put("dealerCode","1");
params.put("scheduledTime","1");
params.put("budget","1");
params.put("provinceName","北京");
params.put("cityName","北京");
// System.out.println(HttpClientUtil.sendPostParam(URL, params));
System.out.println(HttpClientUtil.sendGet(URL));
}
}

View File

@ -10,6 +10,7 @@ import osc.git.eh3.utils.Base64;
import osc.git.eh3.utils.HttpClientUtil;
public class TestOpenPuton {
// public static String URL = "http://111.235.158.25:8080/dsp-open/pxene/dsp.do";
public static String URL = "http://127.0.0.1:8080/dsp-open/pxene/dsp.do";
private static String key = "adjdjfjfjfjdkdkd";//
private static String appid = "t123456";// 用户
@ -22,9 +23,9 @@ public class TestOpenPuton {
// System.out.println(JSONObject.fromObject(data));
JSONObject json = new JSONObject();
json.put("param", "中午");
String str = "{\"order_info\":{\"put_time\":[],\"cost_type\":1,\"order_budget\":{\"budget_all\":100.0},\"cost_single\":100.0,\"type\":1,\"order_id\":14575,\"landing_page\":\"www.baidu.com\",\"order_name\":\"tony-test-31\",\"brand_en\":null,\"plat\":1,\"end_time\":\"2016-05-03\",\"creative_group\":[{\"group_name\":\"test_五一\",\"plat\":1,\"ratio\":\"-\",\"group_id\":405,\"click_code\":null,\"impression_code\":null,\"landing_page\":\"www.baidu.com\"}],\"buy_type\":1,\"order_freq\":{\"imp_day_req\":5,\"click_total_freq\":5,\"imp_total_req\":10,\"click_day_freq\":1},\"ad_owner\":{\"owner_qualification\":[],\"owner_name\":\"ABI\",\"organization_code\":null,\"owner_id\":107,\"owner_category\":\"食品饮料>健康饮料,运动饮料,功能性饮料\",\"website_url\":\"http://sedrin.reloadbuzz.com/food2/xueJin/index.php/home/user/index\"},\"start_time\":\"2016-05-03\",\"order_goal\":{\"order_total_show\":1},\"brand_zh\":\"Sedrin\",\"industry\":\"食品饮料>健康饮料,运动饮料,功能性饮料\",\"note\":\"媒体类型定向:\\nnull\\n\\n关键词定向\\n123\\n\\n广告素材轮播\\n平均\\n\\n备注\\n备注1(资源设置)\\n321\\n\\n\\n\"}}";
String str = "{\"order_info\":{\"order_id\":300001,\"order_name\":\"20160506Amnet\",\"brand_zh\":\"复歌Amnet\",\"brand_en\":\"fugetech\",\"note\":\"note\",\"industry\":\"个人护理/药品>护发品\",\"ad_owner\":{\"owner_id\":201,\"owner_name\":\"复歌\",\"owner_category\":\"个人护理/药品>护发品\",\"organization_code\":\"123456789\",\"website_url\":\"www.fugetech.com\",\"owner_qualification\":[{\"url\":\"http://img.pxene.com/basic/221dab14-e401-446d-bd59-5e0a798a3eb5/366f3ad4156c4625aae8202e03f1f01d.jpg\",\"qualification_name\":\"企业营业执照\"},{\"url\":\"http://img.pxene.com/basic/221dab14-e401-446d-bd59-5e0a798a3eb5/366f3ad4156c4625aae8202e03f1f01d.jpg\",\"qualification_name\":\"ICP证书\"}]},\"start_time\":\"2015-12-01\",\"end_time\":\"2016-12-30\",\"buy_type\":1,\"plat\":1,\"type\":1,\"landing_page\":\"www.fugetech.com\",\"order_area\":[{\"first_level\":\"山东\",\"second_level\":[\"烟台\",\"青岛\"]},{\"first_level\":\"河北\"}],\"order_budget\":{\"budget_all\":20000000,\"budget_day\":200000},\"order_goal\":{\"order_day_show\":200,\"order_total_show\":20000,\"order_day_click\":100,\"order_total_click\":10000,\"order_day_conversion\":50,\"order_total_conversion\":5000,\"order_day_ctr\":0.5,\"order_total_ctr\":0.5},\"order_freq\":{\"imp_day_req\":2000,\"imp_total_req\":10000,\"click_day_freq\":100,\"click_total_freq\":500},\"order_pmp\":[{\"deal_id\":123456,\"ratio\":0.12,\"purcharse\":12345,\"media_domain\":\"www.fugetech.com\"}],\"media_white\":[\"111\"],\"media_black\":[\"222\"],\"creative_group\":[{\"group_id\":1,\"group_name\":\"创意分组A\",\"landing_page\":\"www.fugetech.com\",\"impression_code\":\"www.123.com\",\"click_code\":\"www.456.com\",\"ratio\":\"40%\",\"creative\":[{\"id\":301,\"name\":\"test\",\"plat\":1,\"width\":580,\"height\":90,\"type\":\"jpg\",\"download\":true,\"url\":\"http://img.pxene.com/basic/221dab14-e401-446d-bd59-5e0a798a3eb5/366f3ad4156c4625aae8202e03f1f01d.jpg\"}]},{\"group_id\":2,\"group_name\":\"创意分组B\",\"landing_page\":\"www.fugetech.com\",\"impression_code\":\"www.789.com\",\"click_code\":\"www.963.com\",\"ratio\":\"60%\",\"creative\":[{\"id\":302,\"name\":\"test\",\"plat\":1,\"width\":580,\"height\":90,\"type\":\"jpg\",\"download\":true,\"url\":\"http://img.pxene.com/basic/221dab14-e401-446d-bd59-5e0a798a3eb5/366f3ad4156c4625aae8202e03f1f01d.jpg\"}]}]}}";
// String str = "{\"order_info\":{\"note\":\"媒体类型定向:\\nnull\\n\\n广告素材轮播\\n平均\\n\\n\",\"put_time\":[],\"buy_type\":1,\"cost_type\":2,\"ad_owner\":{\"owner_name\":\"Fuge-test\",\"owner_category\":\"技术,媒体,通讯类>计算机服务和软件业\",\"website_url\":\"http://www.fugetech.com/\",\"owner_id\":180,\"organization_code\":null,\"owner_qualification\":[]},\"end_time\":\"2016-05-31\",\"order_budget\":{\"budget_all\":11100.0},\"landing_page\":\"www.fugetech.com\",\"industry\":\"技术,媒体,通讯类>计算机服务和软件业\",\"type\":1,\"cost_single\":100.0,\"order_name\":\"Pxene Test\",\"start_time\":\"2016-05-08\",\"order_goal\":{\"order_total_show\":111000},\"brand_zh\":\"Fuge\",\"plat\":1,\"order_id\":14578,\"brand_en\":null,\"creative_group\":[{\"group_id\":380,\"group_name\":\"test1_ttt\",\"landing_page\":\"www.123.com\",\"plat\":1,\"impression_code\":\"www.abc.com\",\"click_code\":\"www.456.com\",\"creative\":[{\"download\":true,\"name\":\"3218191555260729151.png\",\"width\":960,\"id\":653,\"type\":\"png\",\"url\":\"http://rimix.fugetech.com/sending/14578/20160509171537/creative/3218191555260729151.png\",\"height\":60}],\"ratio\":\"-\"}]}}";
System.out.println(HttpClientUtil.sendPostJSONData(URL, str));
}