Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Carlos Carmona
Carlos Carmona
17,354 Points

How to use Cache within Wordpress plugin?

I'm developing a plugin that uses a lot of Querys and this makes the site go slow.

One of the solutions I found is to implement Cache to my Querys, but I see that there are 2 kinds:

Non-Persistent Cache and Transient type. So now I'm confuse on witch one to use. Is there any course here that could guide me with this?! Or anyone can guide me on this one?!

Thanks

1 Answer

Sue Dough
Sue Dough
35,800 Points

Hi Carlos,

First try to optimize your queries. Maybe share some code here.

This really depends. If its just normal post queries I would just use the caching functions. Where I may use transisents would be this example:

I have 200,000 posts. I want to find out 4 times a day which 10 fit in the algorithim I wrote. I need to run an expensive query to grab 10 post ids that will take 5-15 seconds and just returns 10 post ids. These 10 posts ids are just 65 characters in length when put all together. This may make sense to put in a transient for X amount of time. I would then set these in a variable and use that with the post__in paramater.

What you SHOULD NOT DO is store a bunch of HTML static content for the query in a transient. Simply store the post ID's to then use in a query if its very expensive.

Try them both out and test the performance.