[wp-hackers] Transient key best practice

Otto otto at ottodestruct.com
Mon Sep 9 20:25:40 UTC 2013


It is a very bad idea to use mutating keys for transients. You run the
risk of storing transients in ways such that you never actually try to
get them again, thus creating lots and lots of entries in the database
for those transients, for no reason.

The important part of a caching mechanism is that you read from the
cache. If your key is constantly changing in a way that makes doing
this read less likely, then your cache isn't very useful, is it?

I would absolutely not include cache time or number of items in the
key, because these can change and then you'll never look up that
cached data again, leaving it orphaned.

-Otto


On Mon, Sep 9, 2013 at 2:38 PM, Micky Hulse <mickyhulse.lists at gmail.com> wrote:
> Hello,
>
> I'm writing a class that gets a JSON response from YouTube's API and
> caches the parsed data into a WP transient.
>
> I was tentatively planning on creating transient keys that look like:
>
> namespace_youtube:playlist_or_user|||cache_time|||number_of_items
>
> Where:
>
> 1. "namespace_youtube" = Make sure I don't stomp on other keys and
> easier to find.
>
> 2. "playlist_or_user" = Will be either a playlist ID or a username.
>
> 3. "cache_time" = How long to cache this transient.
>
> 4. "number_of_items" = Number of videos that were cached in this transient.
>
> 5. "|||" = Just an easy delimiter for me to search on if I need to
> get/parse transient keys.
>
> Goal:
>
> I want the ability to cache multiple different types of YouTube API
> calls and have them be cached based on playlist ID or username, how
> long it was it's supposed to be cached for and the number of items
> cached. In other words, there might be times where I need to request
> the same playlist but vary it's output by the cache time and number of
> items requested from the API.
>
> Question(s):
>
> Based upon my goal, does the above transient key naming convention make sense?
>
> What's common transient caching naming practice for situations like I
> describe above?
>
> For when it comes to transients, is there a way to have simpler
> transient key (just simply "namespace_youtube", for example) naming
> convention yet vary, and have the ability to access, the data stored
> in that transient?
>
> Please let me know if I need to clarify my questions.
>
> Thanks!
> M
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list