QUIQQER - Cache Update

in #utopian-io6 years ago

QUIQQER - Cache Update

The Cache Module saves your page in an optimal version. Thus your page becomes faster, rises in the search engine rankings and lets also your visitors marvel.

Not only your images will be optimized, but also CSS, HTML and JavaScript. This will not only make your page smaller, but also reduce browser requests.



Repository

Bug Fixes

What was the issue(s)?

Crazy Caching Bug. The whole caching for the user nobody was much too strong and so, wrong user data were cached.

In order to understand the fix, the problem must first be explained.

The Problem

The caching module caches every page for the Nobody. An optimized version of the rendered page is saved. Nobody is an unregistered user in QUIQQER, that meansNobody is also every visitor to your site.

The problem with this is that, if, for example, an American comes to your site, an optimized version will be saved. Basically this is correct, but it is not correct for the country flag. I.e. if a European visits the site the next time, he has US as his country.

What was the solution?

Until the problem has been found it take a few hours. sigh

This fix unfortunately needed an update from the core. Unfortunately there was no good way to find out the area in which the user is defined. Therefore, we first outsourced the part a bit and defined an identifier.

commit

<script id="quiqqer-user-defined">
    {assign var=userCountry value=$User->getCountry()}
    var QUIQQER_USER = {
        id   : {if $User->getId()}{$User->getId()}{else}0{/if},
        name : '{$User->getName()}',
        lang : '{$User->getLang()}',
        country: {if $userCountry}'{$userCountry->getCode()}'{else}false{/if}
    };
</script>

The important part is <script id="quiqqer-user-defined">. The id assignment now allow other modules to access it better.

The fix in the cache module was a bit more complicated.

The fix

// replace user data
$cache = preg_replace_callback(
    '/<script id="quiqqer-user-defined">(.*)<\/script>/Uis',
    function () {
        $Nobody      = QUI::getUsers()->getNobody();
        $Country     = $Nobody->getCountry();
        $countryCode = '';
        
        if ($Country) {
            $countryCode = $Country->getCode();
        }

        $user = [
            'id'      => 0,
            'name'    => $Nobody->getName(),
            'lang'    => $Nobody->getLang(),
            'country' => $countryCode
        ];

        return '<script id="quiqqer-user-defined">var QUIQQER_USER= '.json_encode($user).';</script>';
    },
    $cache
);

return $cache;

What does this do?

For each request the part with quiqqer-user-defined is searched. If quiqqer-user-defined was found, the User Object for JavaScript is then completely replaced and rewritten.

preg_replace_callback is structured as follows, the first argument is the search and if something is found, the callback function is executed (the second argument). The return value then overwrites the part which was found.

Why was preg_replace_callback used?

Since the caching module aims at speed, DOM in PHP is completely unacceptable as a solution. Even with preg_replace_callback we have our concerns. Unfortunately we found no other way to fix this strange bug.

Is it really so bad that the country in the cache isn't right?

Yes, it is. We had the following situation for 3 months: From time to time legal notices for Europeans were displayed and sometimes not. But when you logged in to the system it worked. When you logged out, every second time it worked and sometimes it didn't.

In addition, the error was then two weeks no longer present and then suddenly again.

Even though the fix looks quite simple, it still shows that caching is one of the hardest things in IT :D

GitHub Account

In the next days there will be a new stable version if all was tested

Thanks for reading
Hen, for PCSG Developers

Sort:  

Thank you for your contribution @pcsg-dev and I can see that it takes efforts to understand and fix the problems even the fix may not look complicated.

For the caching, it is expected that the users coming from different regions should be cached into different places. You can take country-code to differentiate the path of the caching.

You might want to use third party caching plugins or services such as Cloudflare.

The pattern (reg) you are looking for may be too strict? Have you considered extra spaces/single quotes in the following?

/<script id="quiqqer-user-defined">(.*)<\/script>/Uis

Similarly, will there be a problem for text/javascript ?

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Hen here from PCSG. Thank you for the hint and your evaluation. 👍

Similarly, will there be a problem for text/javascript ?

no there is no problem. the part comes directly from the core. 99% of the template designers use the quiqqer header. those who don't use it, need to know exactly what they are doing.

the thing is, there are more things in the template header, like meta specifications, title etc. so this is a general template specification for template designers.

Thank you for your review, @justyy! Keep up the good work!

Hi @pcsg-dev!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @pcsg-dev!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @pcsg-dev! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 4000 upvotes. Your next target is to reach 5000 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness and get one more award and increased upvotes!

Coin Marketplace

STEEM 0.23
TRX 0.28
JST 0.042
BTC 104956.85
ETH 3880.98
SBD 3.32