/**
 *
 */
package com.cnd3b.common.data.db;

import com.cnd3b.common.BaseClass;

import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;

/**
 * @author Administrator
 *
 */
public class DataPool extends BaseClass {
    // 数据缓存池
    public static Hashtable<String, Object> datapool = new Hashtable<String, Object>();
    // 数据缓存池有效期
    public static Hashtable<String, Date> datapoolCtrl = new Hashtable<String, Date>();
    /**
     * 默认缓存有效时间，分钟
     */
    public static int defaultdatalife = 5;
    /**
     * 缓存统一清理时间，分钟
     */
    public static int cleandatatime = 30;

    public static Date nextAutoRemoveTime = Calendar.getInstance().getTime();

    /**
     * 从缓存中获取数据
     * @param datakey
     * @return
     */
    public static Object get(String datakey) {
        Object data = null;
        if (DataPool.datapoolCtrl.containsKey(datakey)) {
            Date enddate = DataPool.datapoolCtrl.get(datakey);
            if (enddate.after(Calendar.getInstance().getTime()) && DataPool.datapool.containsKey(datakey)) {
                data = DataPool.datapool.get(datakey);
            } else {
                autoRemove();
            }
        }
        return data;
    }

    /**
     * 将数据添加到缓存中
     * @param datakey
     * @param data
     */
    public static void put(String datakey, Object data) {
        DataPool.datapool.put(datakey, data);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + defaultdatalife);
        DataPool.datapoolCtrl.put(datakey, calendar.getTime());
    }

    /**
     * 将数据添加到缓存中
     * @param datakey
     * @param data
     */
    public static void put(String datakey, Object data, int datalife) {
        if (datalife <= 0) {
            datalife = defaultdatalife;
        }
        DataPool.datapool.put(datakey, data);
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + datalife);
        DataPool.datapoolCtrl.put(datakey, calendar.getTime());
    }

    public static void remove(String datakey) {
        synchronized (DataPool.class) {
            DataPool.datapoolCtrl.remove(datakey);
            DataPool.datapool.remove(datakey);
        }
    }

    public static void autoRemove() {
        synchronized (DataPool.class) {
            try {
                if (nextAutoRemoveTime.before(Calendar.getInstance().getTime())) {
                    Date nowdate = Calendar.getInstance().getTime();
                    if (!DataPool.datapoolCtrl.isEmpty()) {
                        Object[] keyArray = DataPool.datapoolCtrl.keySet().toArray();
                        for (Object datakey : keyArray) {
                            String key = (String) datakey;
                            if (DataPool.datapoolCtrl.containsKey(key) && DataPool.datapoolCtrl.get(key).before(nowdate)) {
                                remove(key);
                            }
                        }
                    }
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.MINUTE, cleandatatime);
                    nextAutoRemoveTime = calendar.getTime();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void autoRemove() {
//        synchronized (DataPool.class) {
//            try {
//                if (nextAutoRemoveTime.before(Calendar.getInstance().getTime())) {
//                    Date nowdate = Calendar.getInstance().getTime();
//                    Set<String> set = DataPool.datapoolCtrl.keySet();
//                    Iterator<String> it = set.iterator();
//                    ArrayList<String> list = new ArrayList<>();
//                    while (it.hasNext()) {
//                        list.add(it.next());
//                    }
//                    for (String datakey : list) {
//                        if (DataPool.datapoolCtrl.containsKey(datakey) && DataPool.datapoolCtrl.get(datakey).after(nowdate)) {
//                            remove(datakey);
//                        }
//                    }
//                    Calendar calendar = Calendar.getInstance();
//                    calendar.add(Calendar.MINUTE, cleandatatime);
//                    nextAutoRemoveTime = calendar.getTime();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
