The SPIP Cache and dynamic PHP functions

Hello,
I’m building a custom dashboard.
I am using modeles.
Inside the modeles, I am calling a PHP function, that fetches some information from the DB.
When I enable SPIP’s cache, the load time is fantastic, but the dynamic PHP function is not working. Stale content is returned.

I would like to either have parts of the code/modele not be cached, or force a refresh on the cache in certain conditions (db modifications).

Is there a way to use dynamic php functions in modeles with the cache enabled?

I would like to understand better how the caching in SPIP is working, and how dynamic content could be handled. I guess one answer would be, to implement everything using BOUCLES. For now I have PHP functions that prepare content using sql_allfetsel.

Otherwise, if there is no way to run these functions with the change, I can also disable the cache:

<?php
// ne jamais utiliser le cache 
// ni meme creer  les fichiers cache
define('_NO_CACHE', -1); 


FYI, using #CACHE{0} on modeles and sub-modeles has not worked.

Best regards

Hello Urs,
Modeles have a very special cache management, and IMO it’s better not to use them outside of an article content, with <modele123|arg> syntax.

For custom dashboards requiring acute cache management, i use memoization plugin with APCU+cachelab plugin so as to optimize it and refresh only specific caches only when specificaly required. It’s quite a whole new way to manage caching with spip. If you go for it, you’ll have to tweak the parts of your PHP where you want a dedicated behaviour : know what triggers uncaching, know what specific caches have to be renewed. There is no english documentation : see plugins.spip.net/cachelab.html

Without these tools, afaik you ought to use spip out of the box cache management :

  • only use dynamic <INCLUDE> so as to keep cache granularity, and never use static #MODELE nor #INCLUDE.
  • use #CACHE{0} when required for some specific include. These includes should be as small as possible. Dashboards are usualy in restricted access areas so it might not be a problem to have no cache at all for the more dynamic includes… but dashboards can also be compute intensive or heavy on external http access… so it depends on your use case.
  • except when timeout, there is no way to trigger some specific cache refresh : it’s « all caches are to be renewed as soon as something changes in the DB » or « no cache is refreshed because nothing changes ».
  • oh yes, there is a way : use var_mode=calcul on links or on a « refresh » button

In any cases, you can also use XRay plugin (requires memoization with APCu) so as to grasp what precisely happens with SPIP caches on your site.

Hello,

The PDF joint is my solution (fr and en google trad), dont know if it’s
work with #MODELE.


Cordialement, Eric Camus.

(La pièce jointe web_acad_article_390_fr.pdf est manquante)

(La pièce jointe web_acad_article_390_en.pdf est manquante)

PDF => images







Code :

function plugin_name_affichage_final($texte) {
        if(preg_match_all(',\<phpcode\>(.+)\</phpcode\>,Us',$texte,$reg,PREG_SET_ORDER)) {
                foreach($reg as $c) {
                        ob_start();
                        eval($c[1]);
                        $x=ob_get_contents();
                        ob_end_clean();
                        if(!$x) $x='';
                        $texte=str_replace($c[0],$x,$texte);
                }
        }
        return $texte;
}

Thank you JLuc,
this is very helpful. I didn’t know that SPIP validates the whole cache.
I found the function for it:

<?php
// On invalide les caches
include_spip('inc/invalideur');
suivre_invalideur("id='$objet/$id_objet'");

I wonder then, why they allow a specific object to be provided when resetting the cache, if the whole cache is reset?

Thank you for the other tips as well regarding granular cache.

Thank you Eric,
Very interesting approach!

For plugins and future uses.
Eg Cachelab enables to block all cache reset and uses this "id='$objet/$id_objet'" argument to call a dedicated reset function depending on the provided object – when this function has been defined. See API CacheLab 2. Actions globales par type d'invalidation - SPIP-Contrib