Discussion:
transients and caching on your own
Haluk Karamete
2014-01-25 18:23:34 UTC
Permalink
Hey Hackers,

Say there is a URL such as http://www.domain.com/pageX.
Say that URL happens to be a magazine/news like URL and has a ton of cross
taxonomy queries and runs over 100 queries blah blah. The point is that the
page is very loaded.

And this page happens to attract a ton of traffic.
To top it off, there are a few more URLs like that throughout the site.

Total cache, super cache is not an option for me. Due to their complex
configs and weirdo behaviors, I confidently say, that they will never be
part of my wordpress work. Their too many features introduce more problems
than they solve. I'm done with them.

So, I am looking for a 80/20 solution that addresses my main need - by
building my own. With the right guidance, this should take less than a day.

I'm not looking for an elaborate solution, like those that have 100+
features, settings and more...

All I want is this:

have a piece of code like this ( the question I will be asking you later is
WHERE THIS CODE SHOULD GO ) and run it in the right hooks.

$cached_urls= array(
'url_1' => 'num_of_secs_to_cache|my_filter_function_name',
'http://www.domain.com/pageX' => '60*60|xyx_my_filter',
);
handle_my_cache($cached_urls);

in this array, left side needs no explanation.
right side though has a separator (|) and I use that to visually split the
data (expire time and filter ).

The array is pretty visual and tells what's happening at a glance.

The plan is to store the URLs' corresponding HTMLs (all the way from
<!doctype html> to </html>) in a transient - where the transient ID is made
up on the fly from the URL associated with it. That I can do without a
sweat.

What I'd like to get an insight from a master like you is that where this
piece of code should go?

I need an action hook as early as possible but after wordpress loaded the
transient API, & it knows about the current user login status.

I sense that location would be template_redirect. Am I right?
But on the other hand, at that time, wordpress would have already run the
main query which is no use to me, because I already got the HTML in a
transient (which includes the results of that main query too) So, there is
a waste in there...
So, Is it the "pre_get_posts" hook that I should go after... See my issue?

Once we plug the code in the right place, the "handle_my_cache();" function
to take over and do its thing.

which is mainly,
* check if the current user is logged in, and if so,
exit out and let WordPress run its course.

* check if the transient is alive and if so,
load it and prevent from WordPress taking its further course.

* and if the transient is not alive, let WordPress run its course,
but at the end, let the transient be created for the next time around.

Could you guide me which hook(s) do I need here?Thank you for your time
Nikola Nikolov
2014-01-25 18:39:49 UTC
Permalink
Hi Haluk,

I believe that the action hook you're looking into would be
wp_loaded<http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded>
-
the glance at the Action
Reference<http://codex.wordpress.org/Plugin_API/Action_Reference>
shows
that this is the last hook before the
parse_request<http://codex.wordpress.org/Plugin_API/Action_Reference/parse_request>action
- which happens after WordPress completes it's request
parsing(basically when it goes through the permalinks[if they are enabled]
and finds a matching pattern and fills the query parameters).

That also seems like the place to start your output buffer in order to save
the outputted HTML and there you can add your action that would complete
the output buffering and save it in the transient. I *think* the hook you'd
be looking forward for that would be
shutdown<http://codex.wordpress.org/Plugin_API/Action_Reference/shutdown>
.

Other than that I don't think there's anything else you need(hook-wise).
You might want to do some checks on the output that you're storing just to
make sure it's not incorrect(maybe see if it's empty or not), but I don't
know if it's worth to make more complex checks - like to see if the HTML is
properly formed(so that you're not missing the <html></html> tags for
instance), but if something goes wrong it might be a good idea to have some
kind of a fail-safe.
Post by Haluk Karamete
Hey Hackers,
Say there is a URL such as http://www.domain.com/pageX.
Say that URL happens to be a magazine/news like URL and has a ton of cross
taxonomy queries and runs over 100 queries blah blah. The point is that the
page is very loaded.
And this page happens to attract a ton of traffic.
To top it off, there are a few more URLs like that throughout the site.
Total cache, super cache is not an option for me. Due to their complex
configs and weirdo behaviors, I confidently say, that they will never be
part of my wordpress work. Their too many features introduce more problems
than they solve. I'm done with them.
So, I am looking for a 80/20 solution that addresses my main need - by
building my own. With the right guidance, this should take less than a day.
I'm not looking for an elaborate solution, like those that have 100+
features, settings and more...
have a piece of code like this ( the question I will be asking you later is
WHERE THIS CODE SHOULD GO ) and run it in the right hooks.
$cached_urls= array(
'url_1' => 'num_of_secs_to_cache|my_filter_function_name',
'http://www.domain.com/pageX' => '60*60|xyx_my_filter',
);
handle_my_cache($cached_urls);
in this array, left side needs no explanation.
right side though has a separator (|) and I use that to visually split the
data (expire time and filter ).
The array is pretty visual and tells what's happening at a glance.
The plan is to store the URLs' corresponding HTMLs (all the way from
<!doctype html> to </html>) in a transient - where the transient ID is made
up on the fly from the URL associated with it. That I can do without a
sweat.
What I'd like to get an insight from a master like you is that where this
piece of code should go?
I need an action hook as early as possible but after wordpress loaded the
transient API, & it knows about the current user login status.
I sense that location would be template_redirect. Am I right?
But on the other hand, at that time, wordpress would have already run the
main query which is no use to me, because I already got the HTML in a
transient (which includes the results of that main query too) So, there is
a waste in there...
So, Is it the "pre_get_posts" hook that I should go after... See my issue?
Once we plug the code in the right place, the "handle_my_cache();" function
to take over and do its thing.
which is mainly,
* check if the current user is logged in, and if so,
exit out and let WordPress run its course.
* check if the transient is alive and if so,
load it and prevent from WordPress taking its further course.
* and if the transient is not alive, let WordPress run its course,
but at the end, let the transient be created for the next time around.
Could you guide me which hook(s) do I need here?Thank you for your time
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Haluk Karamete
2014-01-25 19:22:54 UTC
Permalink
Thank you Nikola. What you wrote makes total sense.
Post by Nikola Nikolov
Hi Haluk,
I believe that the action hook you're looking into would be
wp_loaded<http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded
-
the glance at the Action
Reference<http://codex.wordpress.org/Plugin_API/Action_Reference>
shows
that this is the last hook before the
parse_request<
http://codex.wordpress.org/Plugin_API/Action_Reference/parse_request
Post by Haluk Karamete
action
- which happens after WordPress completes it's request
parsing(basically when it goes through the permalinks[if they are enabled]
and finds a matching pattern and fills the query parameters).
That also seems like the place to start your output buffer in order to save
the outputted HTML and there you can add your action that would complete
the output buffering and save it in the transient. I *think* the hook you'd
be looking forward for that would be
shutdown<http://codex.wordpress.org/Plugin_API/Action_Reference/shutdown>
.
Other than that I don't think there's anything else you need(hook-wise).
You might want to do some checks on the output that you're storing just to
make sure it's not incorrect(maybe see if it's empty or not), but I don't
know if it's worth to make more complex checks - like to see if the HTML is
properly formed(so that you're not missing the <html></html> tags for
instance), but if something goes wrong it might be a good idea to have some
kind of a fail-safe.
Post by Haluk Karamete
Hey Hackers,
Say there is a URL such as http://www.domain.com/pageX.
Say that URL happens to be a magazine/news like URL and has a ton of
cross
Post by Haluk Karamete
taxonomy queries and runs over 100 queries blah blah. The point is that
the
Post by Haluk Karamete
page is very loaded.
And this page happens to attract a ton of traffic.
To top it off, there are a few more URLs like that throughout the site.
Total cache, super cache is not an option for me. Due to their complex
configs and weirdo behaviors, I confidently say, that they will never be
part of my wordpress work. Their too many features introduce more
problems
Post by Haluk Karamete
than they solve. I'm done with them.
So, I am looking for a 80/20 solution that addresses my main need - by
building my own. With the right guidance, this should take less than a
day.
Post by Haluk Karamete
I'm not looking for an elaborate solution, like those that have 100+
features, settings and more...
have a piece of code like this ( the question I will be asking you later
is
Post by Haluk Karamete
WHERE THIS CODE SHOULD GO ) and run it in the right hooks.
$cached_urls= array(
'url_1' => 'num_of_secs_to_cache|my_filter_function_name',
'http://www.domain.com/pageX' => '60*60|xyx_my_filter',
);
handle_my_cache($cached_urls);
in this array, left side needs no explanation.
right side though has a separator (|) and I use that to visually split
the
Post by Haluk Karamete
data (expire time and filter ).
The array is pretty visual and tells what's happening at a glance.
The plan is to store the URLs' corresponding HTMLs (all the way from
<!doctype html> to </html>) in a transient - where the transient ID is
made
Post by Haluk Karamete
up on the fly from the URL associated with it. That I can do without a
sweat.
What I'd like to get an insight from a master like you is that where this
piece of code should go?
I need an action hook as early as possible but after wordpress loaded the
transient API, & it knows about the current user login status.
I sense that location would be template_redirect. Am I right?
But on the other hand, at that time, wordpress would have already run the
main query which is no use to me, because I already got the HTML in a
transient (which includes the results of that main query too) So, there
is
Post by Haluk Karamete
a waste in there...
So, Is it the "pre_get_posts" hook that I should go after... See my
issue?
Post by Haluk Karamete
Once we plug the code in the right place, the "handle_my_cache();"
function
Post by Haluk Karamete
to take over and do its thing.
which is mainly,
* check if the current user is logged in, and if so,
exit out and let WordPress run its course.
* check if the transient is alive and if so,
load it and prevent from WordPress taking its further course.
* and if the transient is not alive, let WordPress run its course,
but at the end, let the transient be created for the next time around.
Could you guide me which hook(s) do I need here?Thank you for your time
_______________________________________________
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...