Discussion:
getting the expiration value of a transient when OBJECT_CACHE is involved
Haluk Karamete
2014-06-26 18:21:26 UTC
Permalink
In the code segment that I pasted below ( which is straight from the core -
option.php ), I 'm trying to understand how I can pull the expire_time (
the unix time stamp ) for the transient I'm after.

When there is no object_cache is involved, that's a piece of ccake as
*get_option(
$transient_timeout )* just gets me that.

<begin_snippet>

if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'transient' );
} else {
$transient_option = '_transient_' . $transient;
if ( ! defined( 'WP_INSTALLING' ) ) {
// If option is not in alloptions, it is not autoloaded and
thus has a timeout
$alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' . $transient;
if ( get_option( $transient_timeout ) < time() ) {
delete_option( $transient_option );
delete_option( $transient_timeout );
$value = false;
}
}
}

if ( ! isset( $value ) )
$value = get_option( $transient_option );
}


<end_snippet>

But on an object cache situation, such as total_cache, that's been a uphill
battle for me.

All I got there for inspiration is this;

$value = wp_cache_get( $transient, 'transient' );

When I traced that function ( wp_cache_get() ) down to its roots, I end up
with the following which did not produce any fruits neither.

/**
* Retrieves the cache contents from the cache by key and group.
*
* @since 2.0.0
* @uses $wp_object_cache Object Cache Class
* @see WP_Object_Cache::get()
*
* @param int|string $key What the contents in the cache are called
* @param string $group Where the cache contents are grouped
* @param bool $force Whether to force an update of the local cache
from the persistent cache (default is false)
* @param &bool $found Whether key was found in the cache.
Disambiguates a return of false, a storable value.
* @return bool|mixed False on failure to retrieve contents or the cache
* contents on success
*/
function wp_cache_get( $key, $group = '', $force = false, &$found =
null ) {
global $wp_object_cache;

return $wp_object_cache->get( $key, $group, $force, $found );
}


As you see, there is no talk on $expire there neither.

Then the ball is sent to $wp_object_cache->get court... and I got lost in
there.

Could someone shed some light where to go to get that timestamp?

My goal is to be able to report back to me when the current transient will
expire for the current page.

On my local host, I display this at the bottom of the page without sweat:

This page's transient will expire in 0 day(s) 0 hour(s) 0 min(s) 30
sec(s)


But for the development server, I got nada. :(
Nikola Nikolov
2014-06-26 19:27:02 UTC
Permalink
It's not going to be very easy to do that.

First of all wp_cache_add() can actually be using different storage
mechanisms, or at least different implementations of the clients. For
instance the most common object cache as far as I'm aware is the Memcached
(Memcache Daemon).
In PHP there are two general classes that act as clients for - Memcache and
Memcached. Memcache is pretty plain and doesn't really give you a whole lot
to work with. Memcached I think has more possibilities, but I don't think
getting the expiration date would be one of the built-in ones.

But then there could be a caching plugin that uses a different back-end to
store objects in memory, so unless you cover all of the possibilities, then
it's not really possible to do what you're looking to do.

Is it really necessary to know when a transient will expire? You might have
to look for a way to avoid going that way :)
Post by Haluk Karamete
In the code segment that I pasted below ( which is straight from the core -
option.php ), I 'm trying to understand how I can pull the expire_time (
the unix time stamp ) for the transient I'm after.
When there is no object_cache is involved, that's a piece of ccake as
*get_option(
$transient_timeout )* just gets me that.
<begin_snippet>
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'transient' );
} else {
$transient_option = '_transient_' . $transient;
if ( ! defined( 'WP_INSTALLING' ) ) {
// If option is not in alloptions, it is not autoloaded and
thus has a timeout
$alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' . $transient;
if ( get_option( $transient_timeout ) < time() ) {
delete_option( $transient_option );
delete_option( $transient_timeout );
$value = false;
}
}
}
if ( ! isset( $value ) )
$value = get_option( $transient_option );
}
<end_snippet>
But on an object cache situation, such as total_cache, that's been a uphill
battle for me.
All I got there for inspiration is this;
$value = wp_cache_get( $transient, 'transient' );
When I traced that function ( wp_cache_get() ) down to its roots, I end up
with the following which did not produce any fruits neither.
/**
* Retrieves the cache contents from the cache by key and group.
*
*
from the persistent cache (default is false)
Disambiguates a return of false, a storable value.
* contents on success
*/
function wp_cache_get( $key, $group = '', $force = false, &$found =
null ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force, $found );
}
As you see, there is no talk on $expire there neither.
Then the ball is sent to $wp_object_cache->get court... and I got lost in
there.
Could someone shed some light where to go to get that timestamp?
My goal is to be able to report back to me when the current transient will
expire for the current page.
This page's transient will expire in 0 day(s) 0 hour(s) 0 min(s) 30
sec(s)
But for the development server, I got nada. :(
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Haluk Karamete
2014-06-26 22:10:38 UTC
Permalink
I had kind of sensed that Nikola that it would not be easy to get that
information - for the reasons that you eloborately explained. Thank you for
that.

Since it was way too cool to have that sort of insight displayed at the
bottom of pages, I had to find another way.

When there is a will, there is a way as they say...

So what I ended up doing was this;

Attach a piece of HTML like: <!--timestamp:1455454343--> right within the
HTML I'm saving.
Since I'm the one who sets the transient, and I'm the one who gets it
back, this technique guarantees that the time-stamp wwould be part of the
meal-deal, and when I retrieve the transient, then it would be a string
extract to get what I want. :)

Tested and works like a charm - no matter what the internal mechanism is...

before setting:
$expire_time = time()+$expire;
$transient_data = $transient_data . '<!--blp:expires{' . $expire_time .
'}-->';
$ret_val = set_transient($id,$transient_data,$expire);

after getting:
$when_to_expire = blp_getinbetween
($transient_data,'<!--blp:expires{','}-->');
$transient_data =
blp_replace($transient_data,'<!--blp:expires{' . $when_to_expire .
'}-->','');
Post by Nikola Nikolov
It's not going to be very easy to do that.
First of all wp_cache_add() can actually be using different storage
mechanisms, or at least different implementations of the clients. For
instance the most common object cache as far as I'm aware is the Memcached
(Memcache Daemon).
In PHP there are two general classes that act as clients for - Memcache and
Memcached. Memcache is pretty plain and doesn't really give you a whole lot
to work with. Memcached I think has more possibilities, but I don't think
getting the expiration date would be one of the built-in ones.
But then there could be a caching plugin that uses a different back-end to
store objects in memory, so unless you cover all of the possibilities, then
it's not really possible to do what you're looking to do.
Is it really necessary to know when a transient will expire? You might have
to look for a way to avoid going that way :)
Post by Haluk Karamete
In the code segment that I pasted below ( which is straight from the
core -
Post by Haluk Karamete
option.php ), I 'm trying to understand how I can pull the
expire_time (
Post by Haluk Karamete
the unix time stamp ) for the transient I'm after.
When there is no object_cache is involved, that's a piece of ccake as
*get_option(
$transient_timeout )* just gets me that.
<begin_snippet>
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'transient' );
} else {
$transient_option = '_transient_' . $transient;
if ( ! defined( 'WP_INSTALLING' ) ) {
// If option is not in alloptions, it is not autoloaded and
thus has a timeout
$alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' . $transient;
if ( get_option( $transient_timeout ) < time() ) {
delete_option( $transient_option );
delete_option( $transient_timeout );
$value = false;
}
}
}
if ( ! isset( $value ) )
$value = get_option( $transient_option );
}
<end_snippet>
But on an object cache situation, such as total_cache, that's been a
uphill
Post by Haluk Karamete
battle for me.
All I got there for inspiration is this;
$value = wp_cache_get( $transient, 'transient' );
When I traced that function ( wp_cache_get() ) down to its roots, I end
up
Post by Haluk Karamete
with the following which did not produce any fruits neither.
/**
* Retrieves the cache contents from the cache by key and group.
*
*
from the persistent cache (default is false)
Disambiguates a return of false, a storable value.
* contents on success
*/
function wp_cache_get( $key, $group = '', $force = false, &$found =
null ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force, $found );
}
As you see, there is no talk on $expire there neither.
Then the ball is sent to $wp_object_cache->get court... and I got lost
in
Post by Haluk Karamete
there.
Could someone shed some light where to go to get that timestamp?
My goal is to be able to report back to me when the current transient
will
Post by Haluk Karamete
expire for the current page.
This page's transient will expire in 0 day(s) 0 hour(s) 0 min(s) 30
sec(s)
But for the development server, I got nada. :(
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nikola Nikolov
2014-06-26 23:22:49 UTC
Permalink
Nicely done! That's a very elegant solution - I like it :)
Post by Haluk Karamete
I had kind of sensed that Nikola that it would not be easy to get that
information - for the reasons that you eloborately explained. Thank you for
that.
Since it was way too cool to have that sort of insight displayed at the
bottom of pages, I had to find another way.
When there is a will, there is a way as they say...
So what I ended up doing was this;
Attach a piece of HTML like: <!--timestamp:1455454343--> right within the
HTML I'm saving.
Since I'm the one who sets the transient, and I'm the one who gets it
back, this technique guarantees that the time-stamp wwould be part of the
meal-deal, and when I retrieve the transient, then it would be a string
extract to get what I want. :)
Tested and works like a charm - no matter what the internal mechanism is...
$expire_time = time()+$expire;
$transient_data = $transient_data . '<!--blp:expires{' . $expire_time .
'}-->';
$ret_val = set_transient($id,$transient_data,$expire);
$when_to_expire = blp_getinbetween
($transient_data,'<!--blp:expires{','}-->');
$transient_data =
blp_replace($transient_data,'<!--blp:expires{' . $when_to_expire .
'}-->','');
Post by Nikola Nikolov
It's not going to be very easy to do that.
First of all wp_cache_add() can actually be using different storage
mechanisms, or at least different implementations of the clients. For
instance the most common object cache as far as I'm aware is the
Memcached
Post by Nikola Nikolov
(Memcache Daemon).
In PHP there are two general classes that act as clients for - Memcache
and
Post by Nikola Nikolov
Memcached. Memcache is pretty plain and doesn't really give you a whole
lot
Post by Nikola Nikolov
to work with. Memcached I think has more possibilities, but I don't think
getting the expiration date would be one of the built-in ones.
But then there could be a caching plugin that uses a different back-end
to
Post by Nikola Nikolov
store objects in memory, so unless you cover all of the possibilities,
then
Post by Nikola Nikolov
it's not really possible to do what you're looking to do.
Is it really necessary to know when a transient will expire? You might
have
Post by Nikola Nikolov
to look for a way to avoid going that way :)
Post by Haluk Karamete
In the code segment that I pasted below ( which is straight from the
core -
Post by Haluk Karamete
option.php ), I 'm trying to understand how I can pull the
expire_time (
Post by Haluk Karamete
the unix time stamp ) for the transient I'm after.
When there is no object_cache is involved, that's a piece of ccake as
*get_option(
$transient_timeout )* just gets me that.
<begin_snippet>
if ( wp_using_ext_object_cache() ) {
$value = wp_cache_get( $transient, 'transient' );
} else {
$transient_option = '_transient_' . $transient;
if ( ! defined( 'WP_INSTALLING' ) ) {
// If option is not in alloptions, it is not autoloaded and
thus has a timeout
$alloptions = wp_load_alloptions();
if ( !isset( $alloptions[$transient_option] ) ) {
$transient_timeout = '_transient_timeout_' .
$transient;
Post by Nikola Nikolov
Post by Haluk Karamete
if ( get_option( $transient_timeout ) < time() ) {
delete_option( $transient_option );
delete_option( $transient_timeout );
$value = false;
}
}
}
if ( ! isset( $value ) )
$value = get_option( $transient_option );
}
<end_snippet>
But on an object cache situation, such as total_cache, that's been a
uphill
Post by Haluk Karamete
battle for me.
All I got there for inspiration is this;
$value = wp_cache_get( $transient, 'transient' );
When I traced that function ( wp_cache_get() ) down to its roots, I
end
Post by Nikola Nikolov
up
Post by Haluk Karamete
with the following which did not produce any fruits neither.
/**
* Retrieves the cache contents from the cache by key and group.
*
*
from the persistent cache (default is false)
Disambiguates a return of false, a storable value.
* contents on success
*/
function wp_cache_get( $key, $group = '', $force = false, &$found =
null ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force, $found );
}
As you see, there is no talk on $expire there neither.
Then the ball is sent to $wp_object_cache->get court... and I got lost
in
Post by Haluk Karamete
there.
Could someone shed some light where to go to get that timestamp?
My goal is to be able to report back to me when the current transient
will
Post by Haluk Karamete
expire for the current page.
On my local host, I display this at the bottom of the page without
This page's transient will expire in 0 day(s) 0 hour(s) 0 min(s) 30
sec(s)
But for the development server, I got nada. :(
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...