如何将新值推送到fat-free-framework中的缓存数组?

How to push a new value to a cached array in fat-free-framework?

提问人:Ali Habieb 提问时间:8/25/2022 最后编辑:Ali Habieb 更新时间:8/26/2022 访问量:58

问:

我有一个无脂肪框架应用程序,我需要缓存 API 调用,以便稍后将它们移动到 Cron 作业中的数据库:

<?php

class ApiCallRepository extends BaseRepository
{
    function __construct(ApiCall $model)
    {
        $this->f3 = Base::instance();
        parent::__construct($model);
    }

    public function cache($url, $body, $response)
    {
        if(!$this->f3->exists('API_CALLS')) {
            $this->f3->set('API_CALLS', [], 86400);
        }

        // die(var_export($this->f3->get("API_CALLS"), true));
        /*
            array (
            )
        */
    
        $createdAt = date("Y-m-d H:i:s");
        $api_log = [
            'url' => $url,
            'body' => $body,
            'response' => $response,
            'createdAt' => $createdAt
        ];

        $api_log = json_encode($api_log);
        $this->f3->push("API_CALLS", $api_log);

        die(var_export($this->f3->get("API_CALLS"), true));
        /*
            array (
            0 =>
            '{
                 "url":"https://api-resource.com/users",
                 "body":"",
                 "response":"{"data": {}}"
                 "createdAt":"2022-08-25 14:05:13"
             }'
            )
        */
    }

    public function create($url, $body, $response)
    {
        $this->model->create($url, $body, $response);
    }
}

我使用 Redis 作为我的缓存驱动程序,并且新日志已成功推送到数组,但它似乎没有存储在缓存中,因为当我尝试在另一个请求中var_dump“API_CALLS”的内容时,它显示一个空数组

可能是什么问题?

php 日志记录 redis fat-free-framework

评论

0赞 Pedro Amaral Couto 8/25/2022
为什么需要调用“fe->exists”?
0赞 Ali Habieb 8/25/2022
@PedroAmaralCouto主要目的是确保我在缓存中存储了一个键为“API_CALLS”的数组
0赞 Pedro Amaral Couto 8/26/2022
对不起,我晚上在手机上看到了它,没有注意到和.尽管如此,我注意到现在没有宣布。我不知道是如何实现的,但也许,如果第二个参数为 null,则不会推送任何内容。您应该会找到一条错误消息,例如“警告:未定义的变量 $hlr_item”)。检查是否正在记录警告和日志。!f3->push$hlr_itemBaseRepository::push
0赞 Ali Habieb 8/26/2022
@PedroAmaralCouto没关系,关于,这是由于复制代码时出错,实际上是,并且该项目实际上被推送到数组中。 不是一个函数,而是一个无脂肪框架函数:fatfreeframework.com/3.6/...... 也是一个无脂肪框架函数,它接受第三个参数作为 ,它定义了全局变量应该在缓存中存在多长时间 我需要类似的东西,但对于函数$hlr_item$api_logpushBaseRepositoryset$ttlpush
0赞 xfra35 8/26/2022
您的代码只是将 [] 存储到缓存中,仅此而已。因此,您应该在之后再次调用(使用 ttl)或使用 Cache 类,这可能会使您的代码更清晰。f3->setf3->push

答: 暂无答案