ripper2600 Posted November 21, 2018 Posted November 21, 2018 Hi, Is there easy way to auto load custom data from database on every page. For example Wordpress store this in wp_options and loads fields that are marked for autoload. I have football related site and want to show how many matches are played (today,yesterday and tomorrow) but don't want to query database each time for things that will not change at least 24H.
szabesz Posted November 21, 2018 Posted November 21, 2018 Hi, yes, there is: 16 minutes ago, ripper2600 said: but don't want to query database each time for things that will not change at least 24H. For this you might want to use cached data, or am I missing something?
ripper2600 Posted November 21, 2018 Author Posted November 21, 2018 Thanks for answer! 'autojoin' will not help in my case. I should read more about cache API. I already look in `caches` table early but do not find any variables except FileCompiler__xxxx. By the way all expires rows in `caches` table have same datetime (in my case 2010-04-08 03:10:10).
Zeka Posted November 21, 2018 Posted November 21, 2018 @ripper2600 For sure you can use WireCache to cache number of matches. $cache->preloadFor('matches'); $today_matches = $cache->getFor('matches', 'today'); if(!$today_matches) { $latest_matches = $api->getLatestMatches('today'); $cache->saveFor('matches', 'today', $today_matches, 3600) } $yesterday_matches = $cache->getFor('matches', 'yesterday'); if(!$yesterday_matches) { $latest_matches = $api->getLatestMatches('yesterday'); $cache->saveFor('matches', 'yesterday', $yesterday_matches, 3600) } // setting setting([ 'matches' => WireArray([ 'today' => $today_matches, 'yesterday' => $yesterday_matches ]) ]); 3
adrian Posted November 21, 2018 Posted November 21, 2018 You can read more about wireCache here: https://processwire.com/blog/posts/processwire-core-updates-2.5.28/#wirecache-upgrades 1
ripper2600 Posted November 21, 2018 Author Posted November 21, 2018 @Zeka Thanks! That's exactly what i'm looking for. I'm updating a lots of stats via php and python. Now i can put some data into `caches` table through external scripts and fetch it from PW. The magic date 2010-04-08 03:10:10 clearly shows 'expireNever' on https://processwire.com/api/ref/cache/ ? I'm not sure it is ok to use if(!$yesterday_matches) { if return value is null. @ZekaThanks for prompt and detailed answer!
Zeka Posted November 21, 2018 Posted November 21, 2018 7 minutes ago, ripper2600 said: if(!$yesterday_matches) { if return value is null. Yeap http://php.net/manual/en/types.comparisons.php Second line in 'Comparisons of $x with PHP functions' table. Also, I have found the usage of namespaces very сonveniently, as you can delete and preload several caches at once by $cache->deleteFor("my-namespace"); and $cache->preloadFor("my-namespace"); On one project I have used addition config variable 'disableWireCache' defined in config.php, so my statement looks like if (!$filters || config('disableWireCache')). It allows to 'disable' WireCache globaly for testing etc.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now