All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.jiangzeyin.system.cache.PageTemplateCache Maven / Gradle / Ivy

package cn.jiangzeyin.system.cache;

import cn.jiangzeyin.controller.api.SystemApiControl;
import cn.jiangzeyin.database.base.ReadBase;
import cn.jiangzeyin.database.run.read.Select;
import cn.jiangzeyin.entity.defaults.PageTemplate;
import cn.jiangzeyin.entity.defaults.SiteInfo;
import cn.jiangzeyin.system.SystemBean;
import cn.jiangzeyin.system.log.LogType;
import cn.jiangzeyin.system.log.SystemLog;
import cn.jiangzeyin.system.pool.DefaultExecutor;
import cn.jiangzeyin.util.net.http.HttpUtil;
import cn.jiangzeyin.util.util.StringUtil;
import cn.jiangzeyin.util.util.file.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jiangzeyin
 * Created by jiangzeyin on 2017/2/3.
 */
public class PageTemplateCache {
    /**
     * 清空旧数据
     */
    private static void deleteCache() {
        SystemLog.LOG().info("删除:" + SystemBean.getInstance().VelocityPath);
        FileUtil.deleteDir(SystemBean.getInstance().VelocityPath);
    }

    /**
     * 加载模板内容
     */
    public static void init() {
        if (SiteCache.currentSite == null)
            return;
        if (StringUtil.isEmpty(SystemBean.getInstance().VelocityPath))
            return;
        long s = System.currentTimeMillis();
        SystemLog.LOG(LogType.DEFAULT).info("加载页面模板");
        if (SiteCache.currentSite == null) {
            SystemLog.LOG(LogType.DEFAULT).info("没有对应站点信息无法加载站点页面模板");
            return;
        }
        Select util = new Select() {
        };
        util.setWhere("isDelete=0 and (siteId=0 or FIND_IN_SET(" + SiteCache.currentSite.getId() + ",siteId))");
        util.setRemove("siteId");
        List list = util.run();
        if (list != null) {
            PageTemplateCache.deleteCache();
            for (PageTemplate template : list) {
                try {
                    FileUtil.writeFile(SystemBean.getInstance().VelocityPath + template.getName() + SystemBean.getInstance().velocitySuffix, template.getContent());
                } catch (IOException e) {
                    SystemLog.LOG().error("写入文件错误", e);
                }
            }
        }
        SystemLog.LOG(LogType.DEFAULT).info("加载页面模板结束:" + (System.currentTimeMillis() - s));
    }

    /**
     * 根据模板刷新对呀站点缓存
     *
     * @param templateId id
     */
    public static void refreshCaChe(int templateId) {
        Select select = new Select() {
        };
        select.setKeyColumn("id");
        select.setKeyValue(templateId);
        select.setColumns("siteId");
        select.setResultType(ReadBase.Result.String);
        String siteId = select.run();
        if (StringUtil.isEmpty(siteId))
            return;
        Select siteInfoSelect = new Select() {
        };
        if ("0".equals(siteId)) {
            siteInfoSelect.setWhere("IsDelete=0");
        } else {
            siteInfoSelect.setWhere("isDelete=0 and id in(" + siteId + ")");
        }
        siteInfoSelect.setColumns("run_ing,tag,api_token");
        siteInfoSelect.setResultType(ReadBase.Result.ListMap);
        List> siteInfos = siteInfoSelect.run();
        if (siteInfos == null)
            return;
        for (Map map : siteInfos) {
            Object value = map.get("run_ing");
            if (value == null)
                continue;
            String json = value.toString();
            JSONArray jsonArray = JSONArray.parseArray(json);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject == null)
                    continue;
                boolean status = jsonObject.getBooleanValue("status");
                if (!status)
                    continue;
                String active_ = jsonObject.getString("active");
                SystemBean.Active active = SystemBean.Active.valueOf(active_);
                if (active == SystemBean.Active.prod)
                    continue;
                DefaultExecutor.DEFAULT_EXECUTOR.execute(() -> {

                    String url = jsonObject.getString("url");
                    List nameValuePairList = new ArrayList<>();
                    nameValuePairList.add(new BasicNameValuePair(SystemApiControl.TOKEN_PARAMETER_NAME, (String) map.get("api_token")));
                    String msg;
                    try {
                        String root = url.startsWith("http") ? "" : "http://";
                        String reqUrl = String.format("%s%s/%s/%s", root, url, SystemApiControl.SYSTEM_API_START_URL, SystemApiControl.REFRESH_PAGE_CACHE);
                        //System.out.println(reqUrl);
                        SystemLog.LOG().info("刷新页面缓存:" + reqUrl);
                        msg = HttpUtil.doPost(reqUrl, nameValuePairList, "UTF-8");
                    } catch (Exception e) {
                        SystemLog.ERROR().error("刷新站点页面缓存失败", e);
                        msg = e.getMessage();
                        jsonObject.put("status", false);
                    }
                    jsonObject.put("msg", msg);
                    jsonObject.put("refreshCaChe", System.currentTimeMillis());
                    SiteCache.saveSiteRunIng(jsonObject, (String) map.get("tag"));
                });
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy