Jump to content

Autoload custom variable from database


ripper2600
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

@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
    ])
]);

 

  • Like 3
Link to comment
Share on other sites

@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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...