Merge branch 'master' of git.oschina.net:eh3/useful-code

This commit is contained in:
lixiangrong
2016-07-04 10:04:22 +08:00
5 changed files with 142 additions and 46 deletions

View File

@@ -32,42 +32,6 @@ public class 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);

View File

@@ -0,0 +1,78 @@
package osc.git.eh3.cache;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class SimpleCacheUtil<T> {
private final Map<String, T> m_objects;
private final Map<String, Long> m_expiredObjects;
private final long m_lExpireTime;
private final ExecutorService m_executor;
public SimpleCacheUtil() {
this(500);
}
public SimpleCacheUtil(final int nExpireTime) {
m_objects = Collections.synchronizedMap(new HashMap<String, T>());
m_expiredObjects = Collections.synchronizedMap(new HashMap<String, Long>());
m_lExpireTime = nExpireTime;
m_executor = Executors.newFixedThreadPool(256);
Executors.newScheduledThreadPool(5).scheduleWithFixedDelay(RemoveExpiredObjects(), m_lExpireTime / 2, m_lExpireTime,
TimeUnit.SECONDS);
}
private final Runnable RemoveExpiredObjects() {
return new Runnable() {
public void run() {
for (final String name : m_expiredObjects.keySet()) {
if (System.currentTimeMillis() > m_expiredObjects.get(name)) {
m_executor.execute(CreateRemoveRunnable(name));
}
}
}
};
}
private final Runnable CreateRemoveRunnable(final String name) {
return new Runnable() {
public void run() {
m_objects.remove(name);
m_expiredObjects.remove(name);
}
};
}
public long getExpireTime() {
return m_lExpireTime;
}
public void put(final String name, final T obj) {
put(name, obj, m_lExpireTime);
}
public void put(final String name, final T obj, final long expireTime) {
m_objects.put(name, obj);
m_expiredObjects.put(name, System.currentTimeMillis() + expireTime * 1000);
}
public T get(final String name) {
final Long expireTime = m_expiredObjects.get(name);
if (expireTime == null)
return null;
if (System.currentTimeMillis() > expireTime) {
m_executor.execute(CreateRemoveRunnable(name));
return null;
}
return m_objects.get(name);
}
@SuppressWarnings("unchecked")
public <R extends T> R get(final String name, final Class<R> type) {
return (R) get(name);
}
}

View File

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

View File

@@ -4,7 +4,6 @@ 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());

View File

@@ -0,0 +1,61 @@
package osc.git.eh3.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class ExecBaics50Log {
public static void main(String[] args) throws Exception {
StringBuffer sb = new StringBuffer("");
FileReader reader = new FileReader("C:\\Users\\lixiangrong\\Desktop\\minisite\\20160606\\3\\2016-06-06(3、4、5).txt");
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
String[] split = str.split(",");
String ssString = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (String s : split) {
String substring = s.substring(s.indexOf("=") + 1);
if (substring.contains("CST")) {
substring = formatDate(substring);
ssString += "'" + substring + "'";
}else{
ssString += "'" + substring + "',";
}
}
System.out.println(
"insert into user_info (`u_name`, `u_gender`, `u_tel`, `u_province`, `u_city`, `u_dealername`,`u_dealercode`, `u_scheduledtime`, `addtime`) VALUES("
+ ssString + ");");
}
br.close();
reader.close();
}
private static String formatDate(String date) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", java.util.Locale.ENGLISH);
TimeZone tz = TimeZone.getTimeZone("CST");
sdf1.setTimeZone(tz);
sdf.setTimeZone(tz);
Calendar c = Calendar.getInstance();
c.setTime(sdf1.parse(date));
c.set(Calendar.HOUR, c.get(Calendar.HOUR) - 1);
return sdf.format(c.getTime());
}
}