Function to dynamically decrement the cache time

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Function to dynamically decrement the cache time

Post by 3Di » Fri Oct 13, 2017 10:46 pm

For the TPOTM extension I would like to let the SQL of the "top posters ever" which
queries the DB from the board's startdate to the very end of the previous month
to be decrementally cached at runtime, poorly speaking:

The time to cache will exactly expire across the interval of 00:00:00 (midnight) between months, automatically. Absolutely no need to run the query on every when, just when the month expires. ;)

Here's a prototype which sorts the exact contrary of course :geek: , hence this topic :)

Code: Select all

//---------------------------------------------
$now = time();
$date_today = gmdate("Y-m", $now);

/* Top posters_ever (minus the present month) UTC - Thx Steve */
list($year_cur, $month_cur) = explode('-', $date_today);
$month = (int) $month_cur -1;
$year = (int) $year_cur;
$max_days =  date( 't', gmmktime(23, 59, 59, $month, 1, $year) );
$end_last_month = gmmktime(23, 59, 59, $month, $max_days, $year);

/* Top posters_ever dynamically decrements the cache time - UTC */
$cache_since_last_month =  ( (int) ($now - $end_last_month) );

echo gmdate('Y m d H:i', $now) . ' end this month<br>';
echo gmdate('Y m d H:i', $end_last_month) . ' end last month<br>';
echo $cache_since_last_month . ' timegap<br>';
//----------------------------------------------
The $cache_since_last_month var is the one interested here, all of the rest is working pretty well. Atm the gap ot time increases instead of viceversa, seems like easy but..

TIA
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

rxu
Extensions Development Team
Posts: 2769
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Function to dynamically decrement the cache time

Post by rxu » Sat Oct 14, 2017 2:29 pm

Probably I didn't get it right, but for me it looks like calculating the time interval between the SQL query execution time (which is current time) and the end of the month (in seconds), and then setting that interval as sql query result ttl_cache (which is a parameter of the sql_query() function.

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Sat Oct 14, 2017 11:19 pm

Actually calculates two timestamps taking care of Leap years and what not.

1 - for Now (time()) - dynamic

2 - for the very end of the previous month
in this case 30 09 2017 23:59:59 - static (changes every month)

It aims to store the value in the TTL_cache of course, but I am not using this code actually, that's a prototype as I said. I am stuck on how to calculate the dynamicity for the TTL, that should be decremented whilst the time passes, instead with the last line, this:
$cache_since_last_month = ( (int) ($now - $end_last_month) );
I am getting the opposite, the resulting timestamp value to be used for the main scope, increases this way.

The scope, synopsys.
º Give to the TTL a dynamic timestamp at runtime, which is shorter accordingly to time().
º Allow the SQL to run just monthly across the months's change, like Ott/Nov at 00:00:00

Atm that query is cached with a custom general TTL sat in ACP, which rules other 3 queries, not related, as well. I thought I would like to make a distinction here between scopes.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

rxu
Extensions Development Team
Posts: 2769
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Function to dynamically decrement the cache time

Post by rxu » Sun Oct 15, 2017 4:54 am

Still don't get about dynamic cache ttl. When you execute a query with the cache ttl set, it writes the expiry time point down to the cache file (not the time span), which is already time()+ttl. So, if you then do the revesed operation, meaning ttl = end_of_month - time(), the cache expiry date is going to be set to the end of month with the given ttl then. Thus, the query result will be obtained from the cache until the end of month comes.
You simply don't need to recalculate it every time. But again, I'm probably getting your plan wrong, then I'm sorry and please nevermind :)

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Sun Oct 15, 2017 6:08 am

rxu wrote:
Sun Oct 15, 2017 4:54 am
Still don't get about dynamic cache ttl. When you execute a query with the cache ttl set, it writes the expiry time point down to the cache file (not the time span), which is already time()+ttl. So, if you then do the revesed operation, meaning ttl = end_of_month - time(), the cache expiry date is going to be set to the end of month with the given ttl then. Thus, the query result will be obtained from the cache until the end of month comes.
You simply don't need to recalculate it every time. But again, I'm probably getting your plan wrong, then I'm sorry and please nevermind :)
You gave me the solution I was looking for - I think. Thank you. :)

And no, I am not using the filesystem for those 2 queries, the DB TTL instead.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Sun Oct 15, 2017 7:30 am

I mean, I haven't try yet but it seems so logical I have to use the end of THIS month instead

$end_this_month = 'timestamp';

$ttl_diff = ( (int) ($end_this_month - $now) );

Which ends up for the system in (NOW + $ttl_diff).. (future date) :? :geek:

Some reference.: time-remaining-until-specific-time-from-current-time-of-page-load

Too late I already coded a drop down for that with 5 options, but hey. next release if any.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Sun Oct 15, 2017 9:01 am

Here's the snippet tested for the posterity (UTC+00 & Leap years)

Code: Select all

//---------------------------------------------
$now = time();
$date_today = gmdate("Y-m", $now);
list($year_cur, $month_cur) = explode('-', $date_today);
$this_max_days =  date( 't', gmmktime(23, 59, 59, $month_cur, 1, $year_cur) );
$end_this_month = gmmktime(23, 59, 59, $month_cur, $this_max_days, $year_cur);
$ttl_diff = ( (int) ($end_this_month - $now) );

echo gmdate('Y m d H:i:s', $now) . ' now<br>';
echo gmdate('Y m d H:i:s', $end_this_month) . ' end this month<br>';
echo $ttl_diff . ' timegap<br>';
//----------------------------------------------
Which solves in
2017 10 15 08:59:50 now
2017 10 31 23:59:59 end this month
1436409 timegap
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 2144
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Function to dynamically decrement the cache time

Post by kasimi » Sun Oct 15, 2017 6:43 pm

A bit shorter and more expressive:

Code: Select all

$ttl_diff = strtotime('last day of this month midnight') - time();

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Mon Oct 16, 2017 1:25 am

kasimi wrote:
Sun Oct 15, 2017 6:43 pm
A bit shorter and more expressive:

Code: Select all

$ttl_diff = strtotime('last day of this month midnight') - time();
Hi kasimi, I would not trust the above code but mine. ;)

Example:

Code: Select all

$end_last_month = strtotime('last day of last month midnight');
$end_this_month = strtotime('last day of this month midnight');

echo gmdate('Y m d H:i:s', $end_last_month) . ' end_last_month<br>';
echo gmdate('Y m d H:i:s', $end_this_month) . ' end this month<br>';
solves in
2017 09 29 22:00:00 end_last_month
2017 10 30 23:00:00 end this month
On the same server, as you can see looks like relative, September has 30 and October has 31 days.. BTW.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 2144
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Function to dynamically decrement the cache time

Post by kasimi » Mon Oct 16, 2017 9:15 am

It depends on the timezone you've set or the server defaults to, and it takes DST into account. Use the first day of the next month and add UTC to get what you want:

Code: Select all

$end_last_month = strtotime('first day of this month midnight UTC') - 1;
$end_this_month = strtotime('first day of next month midnight UTC') - 1;

echo gmdate('Y m d H:i:s', $end_last_month) . ' end_last_month<br>';
echo gmdate('Y m d H:i:s', $end_this_month) . ' end this month<br>';
2017 09 30 23:59:59 end_last_month
2017 10 31 23:59:59 end this month

User avatar
3Di
Registered User
Posts: 11821
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano - Frankfurt
Name: Marco
Contact:

Re: Function to dynamically decrement the cache time

Post by 3Di » Mon Oct 16, 2017 2:59 pm

I have to admit is way more elegant than mine, indeed.

Still I have some doubt about the precision on Leap Years, the more I read the docs and search the more I am convinced to do not dismiss what I know it perfectly works for a new implementation.
So to speak, if ain't broken don't repair it. :)

Thanks kasimi. ;)

Open to discuss this more though, I am just saying I will keep my code for TPOTM as it is now and finally submit it. I lost 1 day implementing two mode of caching the viewport and relative ACP side, lol.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 8 guests

cron