如何在 Fat-free Framework 中为动态 url 强制清除缓存?

How to force clear cache for dynamic url in Fat-free Framework?

如何清除我的模式 url(动态 url)的缓存?
比如,/assets/js/@jsname.js...

我不想等到缓存过期,我想要它,在我更新数据库上的东西后立即。

They said 清除数据库需要指定key。而且我不知道我应该为密钥输入什么:(

这是我的代码:

\F3::route("GET @virtualasset: /assets/img/@link/@size.@id.@type", "Control\Imager->akses", 3600 * 24 * 7); // cache seminggu :3

使用以下 key 缓存 HTTP 响应:hash(VERB URI).url

因此,如果您想刷新 assets/js/foo.js,您必须执行以下操作:

$hash=$f3->hash('GET /assets/js/foo.js');
$cache->clear($hash.'.url');

但这有点像 hack,因为此密钥未记录在案并且将来可能会更改。

另外你要记住路由$ttl参数不仅会触发服务器缓存,还会触发客户端缓存。这意味着您需要实施缓存破坏器来刷新客户端缓存。类似于 assets/js/foo.js?v=3.

您可以考虑直接在控制器中实现缓存 class。这会给你更多的灵活性。

我使用这条路线:

GET|HEAD /assets/@folder/@type/v-@version/* = controllers\Assets->send, 3600

我的控制器:

namespace controllers;
class Assets {

    private $paths;
    private $types = ['css', 'js', 'i', 'fonts', 'files', 'icons'];
    private $dontminify = ['i', 'fonts', 'files', 'icons'];


    public function __construct() {
        $this->fw = \Base::instance();
        $this->paths['app'] =  '../app/views/site_tpl/assets/';
        $this->paths['core'] = '../core/views/default/assets/';
    }

    public function send() {

        if (in_array($this->fw->get('PARAMS.folder'), array_keys($this->paths))
            && in_array($this->fw->get('PARAMS.type'), $this->types))
        {
            $folder = $this->paths[$this->fw->get('PARAMS.folder')];
            $type = $this->fw->get('PARAMS.type');
            if (in_array($type, $this->dontminify)) {
                $file = $folder . $type . '/' . $this->fw->pop('PARAMS');
                if (\Web::instance()->send($file)) {
                    return true;
                }
            } else {
                $this->fw->set('UI', $folder . $type . '/');
                $file = $this->fw->pop('PARAMS');
                if (file_exists($this->fw->get('UI') . $file)) {
                    if (!$this->fw->exists('GET.dontminify')) {
                        echo \Web::instance()->minify($file);
                        return true;
                    } elseif (\Web::instance()->send($this->fw->get('UI') . $file)) {
                        return true;
                    }
                }
            }
        }
        $this->fw->error(404);
    }

}

所以,当我想更新文件版本时,我会这样写:

<script src="/assets/app/js/v-20160205/main.js"></script>