WordPress.org

Ideas

Transient API should allow delete by prefix

  1. webaware
    Member

    12345

    I use the transient API as a simple caching mechanism for heavy queries. Rather than delete each cached item individually when it needs to be invalidated, it would be nice to be able to delete all transients with a specific prefix. e.g.

    delete_transient_prefixed('myprefix');

    What I currently need to do is this, which works (for now) but doesn't take into account additional caching mechanisms like memcached:

    global $wpdb;
    
    $sql = "
        delete from {$wpdb->options}
        where option_name like '_transient_myprefix%' or option_name like '_transient_timeout_myprefix%'
    ";
    return $wpdb->query($sql);
    Posted: 2 years ago #
  2. Maor Chasen
    Member

    +1. Sounds like a good idea!

    Posted: 2 years ago #
  3. I agree. I think it is a good Idea.

    Posted: 2 years ago #
  4. Jonathan Dingman
    Member

    12345

    If you guys like the idea, make you sure vote it with 5 stars.

    Posted: 2 years ago #
  5. Agence Web Maroc
    Member

    12345

    +5 stars. Sounds like a good idea!

    Posted: 2 years ago #
  6. Yakup Hoca
    Member

    12345

    +5 stars. Sounds like a good idea!

    Posted: 2 years ago #
  7. micahjm
    Member

    +5 stars. Would be super helpful for caching pieces of larger landing pages.

    Posted: 6 months ago #
  8. webaware
    Member

    12345

    In retrospect, this isn't all that great an idea. Basically, it would work for installations with no object cache, where it's easy to query the database for transients with a specific prefix, but could not possibly work with some persistent object caches such as memcached. This is because you can't query memcached or some other object caches with anything other than an exact ID match.

    What I do instead is implement a caching layer on top of either transients or the object cache, detecting whether an object cache is present. If there's an object cache, I add a salt to the cache key and change the salt when I want to clear all items -- i.e. the items are "forgotten" because the cache keys all change when the salt changes. If there's no object cache, I use transients with a given prefix so that I can delete then en masse as per the Idea above.

    In short: the transients API can't do this, because some storage mechanisms won't support it. If you need to do this, do it yourself.

    cheers,
    Ross

    Posted: 6 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    11 Votes
  • Status

    This idea is under consideration