PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

Phpfastache not get the data on first visit #867

Closed Pok4 closed 2 years ago

Pok4 commented 2 years ago

Hi guys.. Im using php fastcache (last version) and PHP 8.1. I get this trouble on older versions too and i decide to start a question here.. Maybe i go wrong on something with the configurations..

I have this code:

function last_topics() {
    global $dbh, $bb_db, $bb_prefix, $user, $cacher;

    if (!$cacher->has("last_topics_from_forum")) {
        $mysql = $dbh->query("SELECT topic_first_poster_name,topic_title,topic_id,a.forum_id,topic_views,topic_time,topic_first_poster_colour FROM `$bb_db`.".$bb_prefix."_topics a INNER JOIN `$bb_db`.".$bb_prefix."_forums g ON g.forum_id=a.forum_id WHERE topic_posts_approved >= '1' AND enable_indexing=1 ORDER BY topic_time DESC LIMIT 0,5");
        if ($mysql->rowCount() > 0) {
            while ($row = $mysql->fetch(PDO::FETCH_ASSOC)) {
                $usernames = $row['topic_first_poster_name'];
                $topic_titles = htmlspecialchars_decode(truncate_chars($row['topic_title'], 35, '...'));
                $topic_id = $row['topic_id'];
                $forum_id = $row['forum_id'];
                $topic_views = $row['topic_views'];
                $topic_times =  $user->format_date($row['topic_time']);
                $user_post_color = $row['topic_first_poster_colour'];
                if (empty($user_post_color)) { $user_post_color = get_from_db_config('default_user_color'); }
                $last_topics[]  = ['usernames'=>$usernames, 'topic_time'=>$topic_times, 'topic_titles'=>$topic_titles, 'topic_link'=>''.base_forum_url().'viewtopic.php?f='.$forum_id.'&t='.$topic_id.'', 'topic_views'=>$topic_views, 'topic_user_color'=>$user_post_color];
                new ArrayIterator($last_topics);
                $cacher->set("last_topics_from_forum", $last_topics, 60);
            }
        }
    } else {
        return new ArrayIterator($cacher->get('last_topics_from_forum'));
    }
}

Here is some pics:

On first visit: https://i.ibb.co/QJtKzb8/1.png

On second refresh: https://i.ibb.co/kHCQYyF/2.png

This issue is happen on 2-3 more scripts which is works with phpfastcache.. The code is similar to code above..

Please, tell me a variant to works on first visit too... Sometimes it doesn't even work after the 2nd refresh, and after the third ..

Sorry for my english. :)

Geolim4 commented 2 years ago

Hello,

Where your variable $cacher come from and what class is it ?

Also please reformat your issue according default template issue.

Thanks

On Tue, 24 May 2022, 7:15 pm Pok4, @.***> wrote:

Hi guys.. Im using php fastcache (last version) and PHP 8.1. I get this trouble on older versions too and i decide to start a question here.. Maybe i go wrong on something with the configurations..

I have this code:

function last_topics() { global $dbh, $bb_db, $bb_prefix, $user, $cacher;

if (!$cacher->has("last_topics_from_forum")) { $mysql = $dbh->query("SELECT topic_first_poster_name,topic_title,topic_id,a.forum_id,topic_views,topic_time,topic_first_poster_colour FROM $bb_db.".$bb_prefix."_topics a INNER JOIN $bb_db.".$bb_prefix."_forums g ON g.forum_id=a.forum_id WHERE topic_posts_approved >= '1' AND enable_indexing=1 ORDER BY topic_time DESC LIMIT 0,5"); if ($mysql->rowCount() > 0) { while ($row = $mysql->fetch(PDO::FETCH_ASSOC)) { $usernames = $row['topic_first_poster_name']; $topic_titles = htmlspecialchars_decode(truncate_chars($row['topic_title'], 35, '...')); $topic_id = $row['topic_id']; $forum_id = $row['forum_id']; $topic_views = $row['topic_views']; $topic_times = $user->format_date($row['topic_time']); $user_post_color = $row['topic_first_poster_colour']; if (empty($user_post_color)) { $user_post_color = get_from_db_config('default_user_color'); } $last_topics[] = ['usernames'=>$usernames, 'topic_time'=>$topic_times, 'topic_titles'=>$topic_titles, 'topic_link'=>''.base_forum_url().'viewtopic.php?f='.$forum_id.'&t='.$topic_id.'', 'topic_views'=>$topic_views, 'topic_user_color'=>$user_post_color]; new ArrayIterator($last_topics); $cacher->set("last_topics_from_forum", $last_topics, 60); } } } else { return new ArrayIterator($cacher->get('last_topics_from_forum')); } }

Here is some pics:

On first visit: https://i.ibb.co/QJtKzb8/1.png

On second refresh: https://i.ibb.co/kHCQYyF/2.png

This issue is happen on 2-3 more scripts which is works with phpfastcache.. The code is similar to code above..

Please, tell me a variant to works on first visit too... Sometimes it doesn't even work after the 2nd refresh, and after the third ..

Sorry for my english. :)

— Reply to this email directly, view it on GitHub https://github.com/PHPSocialNetwork/phpfastcache/issues/867, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKFGZ6UQINX5N64DQKLUK3VLUFEXANCNFSM5W2JOZWQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Pok4 commented 2 years ago

from core.php (there is a main things from the system... routers,autoloader,event dispatcher and etc..) here is:

//PHP Fast Cache
use Phpfastcache\Helper\Psr16Adapter;
use Phpfastcache\Drivers\Files\Config as FilesConfig;
$cacher = new Psr16Adapter(empty($_SERVER['CACHE_TYPE']) ? 'files' : $_SERVER['CACHE_TYPE'],

i will edit the first post :)

Geolim4 commented 2 years ago

Look like an issue on your code to me.

The tests pass on the Psr16-adapter with complex CRUD operations: https://app.travis-ci.com/github/PHPSocialNetwork/phpfastcache/jobs/570996973#L3633

        new ArrayIterator($last_topics);

This statement look unsigned is that normal ?

Pok4 commented 2 years ago

Oh yes, i must remove this from there... But anyway, without this one - it works on same way.. On first visit - not showing anything... When you go thru pages sometimes is hiding again (maybe this is ok, because cache is expired) I dont know what is wrong...

Pok4 commented 2 years ago

oh.. i edited the code to looks like this:

function last_topics() {
    global $dbh, $bb_db, $bb_prefix, $user, $cacher;

    if (!$cacher->has("last_topics_from_forum")) {
        $mysql = $dbh->query("SELECT topic_first_poster_name,topic_title,topic_id,a.forum_id,topic_views,topic_time,topic_first_poster_colour FROM `$bb_db`.".$bb_prefix."_topics a INNER JOIN `$bb_db`.".$bb_prefix."_forums g ON g.forum_id=a.forum_id WHERE topic_posts_approved >= '1' AND enable_indexing=1 ORDER BY topic_time DESC LIMIT 0,5");
        if ($mysql->rowCount() > 0) {
            while ($row = $mysql->fetch(PDO::FETCH_ASSOC)) {
                $usernames = $row['topic_first_poster_name'];
                $topic_titles = htmlspecialchars_decode(truncate_chars($row['topic_title'], 35, '...'));
                $topic_id = $row['topic_id'];
                $forum_id = $row['forum_id'];
                $topic_views = $row['topic_views'];
                $topic_times =  $user->format_date($row['topic_time']);
                $user_post_color = $row['topic_first_poster_colour'];
                if (empty($user_post_color)) { $user_post_color = get_from_db_config('default_user_color'); }
                $last_topics[]  = ['usernames'=>$usernames, 'topic_time'=>$topic_times, 'topic_titles'=>$topic_titles, 'topic_link'=>''.base_forum_url().'viewtopic.php?f='.$forum_id.'&t='.$topic_id.'', 'topic_views'=>$topic_views, 'topic_user_color'=>$user_post_color];

                $cacher->set("last_topics_from_forum", $last_topics, 60);
            }
        }
    } 
    return new ArrayIterator($cacher->get('last_topics_from_forum'));
}

i removed the else and it works for now...

Does this else is necessary or not ?

Geolim4 commented 2 years ago

It is your own implementation of this, but basically it works on the following scenario:

// IF cache get data is null // do database fetch stuff // setCache with fetched data // ELSE // return cached result

Geolim4 commented 2 years ago

Aside of that it's most likely an implementation issue, not a Phpfastcache bug.

Pok4 commented 2 years ago

Ok for now everything is working fine.