package com.publiccms.views.directive.api;

import java.io.IOException;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;

import com.publiccms.common.base.AbstractAppDirective;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.entities.sys.SysApp;
import com.publiccms.entities.sys.SysUser;
import com.publiccms.logic.component.cache.CacheComponent;

import freemarker.template.TemplateException;

/**
 *
 * clearCache 缓存清理接口
 * <p>使用示例
 * <pre>
  &lt;script&gt;
   fetch('${site.dynamicPath}api/clearCache',{"headers":{"appToken":"接口访问授权Token"}}).then(res => res.json()).then(data=>{
     console.log("ok");
   });
   &lt;/script&gt;
 * </pre>
 */
@Component
public class ClearCacheDirective extends AbstractAppDirective {

    @Override
    public void execute(RenderHandler handler, SysApp app, SysUser user) throws IOException, TemplateException {
        cacheComponent.clear();
        handler.render();
    }

    @Resource
    private CacheComponent cacheComponent;

    @Override
    public boolean needUserToken() {
        return false;
    }

    @Override
    public boolean needAppToken() {
        return true;
    }
}