DataCache 批量的好方法

实例

public class HbaseDataCache2Db {
    private static Logger LOG = LoggerFactory.getLogger(HbaseDataCache2Db.class);
    private static ThreadLocal<Map<String, List<Put>>> THREAD_DATA = new ThreadLocal<>();

    public static void cacheData(String key, Put put) {
        Map<String, List<Put>> cacheMap = THREAD_DATA.get();
        if (cacheMap == null) {
            cacheMap = new HashMap<>();
        }
        List<Put> tmp = cacheMap.get(key);
        if (tmp == null) {
            tmp = new ArrayList<>();
        }
        tmp.add(put);
        cacheMap.put(key, tmp);
        THREAD_DATA.set(cacheMap);
    }

    public static void flushData() {
        try {
            Map<String, List<Put>> cacheMap = THREAD_DATA.get();
            if (cacheMap == null) {
                return;
            }
            for (String tableName : cacheMap.keySet()) {
                HbaseUtil.putData(tableName, cacheMap.get(tableName));
            }
            cacheMap.clear();
            THREAD_DATA.set(cacheMap);
        } catch (Exception e) {
            LOG.error("Hbase 数据写入异常:", e);
        }
    }


    public static void cacheData(Map<String, List<Put>> cacheMap, String key, Put put) {
        List<Put> tmp = cacheMap.get(key);
        if (tmp == null) {
            tmp = new ArrayList<>();
        }
        tmp.add(put);
        cacheMap.put(key, tmp);
    }

    public static void flushData(Map<String, List<Put>> cacheMap) {
        try {
            for (String tableName : cacheMap.keySet()) {
                HbaseUtil.putData(tableName, cacheMap.get(tableName));
            }
        } catch (Exception e) {
            LOG.error("Hbase 数据写入异常:", e);
        }
    }
}

采用了map k,v 的方法

public class MysqlDataCache2Db {
    private static Logger LOG = LoggerFactory.getLogger(MysqlDataCache2Db.class);
    private static ThreadLocal<Map<String, List<Object>>> THREAD_DATA = new ThreadLocal<>();
    public static String KEY_WX = "wx_base_info";
    public static String KEY_WX_ARTICLE = "wx_article_info";

    public static void cacheData(String key, Object obj) {
        Map<String, List<Object>> cacheMap = THREAD_DATA.get();
        if (cacheMap == null) {
            cacheMap = new HashMap<>();
        }
        List<Object> tmp = cacheMap.get(key);
        if (tmp == null) {
            tmp = new ArrayList<>();
        }
        tmp.add(obj);
        cacheMap.put(key, tmp);
        THREAD_DATA.set(cacheMap);
    }


    public static void flushData() {
        try {
            Map<String, List<Object>> cacheMap = THREAD_DATA.get();
            if (cacheMap == null) {
                return;
            }
            for (String tableName : cacheMap.keySet()) {
                List<Object> tmpList = cacheMap.get(tableName);
                SqlUtilBusiness.data2DB(tableName, tmpList);
                String sql = "update " + tableName + " set deptCode=cityCode where deptCode=‘‘ OR deptCode is null";
                SqlUtilBusiness.exceteSql(sql);
            }
            cacheMap.clear();
            THREAD_DATA.set(cacheMap);
        } catch (Exception e) {
            LOG.error("MYSQL 数据写入异常:", e);
        }
    }

   public static void cacheData(Map<String, List<Object>> cacheMap, String key, Object obj) {
        List<Object> tmp = cacheMap.get(key);
        if (tmp == null) {
            tmp = new ArrayList<>();
        }
        tmp.add(obj);
        cacheMap.put(key, tmp);
    }

    public static void flushData(Map<String, List<Object>> cacheMap) {
        try {
            for (String tableName : cacheMap.keySet()) {
                List<Object> tmpList = cacheMap.get(tableName);
                SqlUtilBusiness.data2DB(tableName, tmpList);
                String sql = "update " + tableName + " set deptCode=cityCode where deptCode=‘‘ OR deptCode is null";
                SqlUtilBusiness.exceteSql(sql);
                cacheMap.clear();
            }
        } catch (Exception e) {
            LOG.error("MYSQL 数据写入异常:", e);
        }
    }
}

 

DataCache 批量的好方法

上一篇:【接上一篇】winform中dataGridView高度和宽度自适应填充完数据的高度和宽度,即dataGridView根据数据自适应大小


下一篇:转!!常用的4种动态网页技术—CGI、ASP、JSP、PHP