2

I've been working to speed up my site by utilizing transients to store rendered HTML and it works great. I've set up checks that clear specific transients upon post/page/data updates and it all works well. I've got some complex db checks, so it really speeds things up.

But I'm hitting a stumbling block on coming up with an overall management of the transients. For instance, if I change the theme templates I'd like to be able to delete multiple transients so that they will be automagically regenerated. In a few instances, I have only five different transients, so it's easy to loop through them. But in other snippets, I have over 100 variations.

At a base level, transients may be stored in the database and these are easy to search and find, even using wildcard searches. However, they aren't always stored in the database. But where are they stored? I utilize the xcache php opcode cacher, but I don't seem them in the xcache variable listing. I use W3 Total Cache as well, but even with object caching turned off (or the plugin disabled), I can't seem to figure out where they are stored.

My thought is that instead of trying to delete each transient individually (which always works fine), I'd like to be able to do wildcard searches for them such as "myprefix_" and then delete all that match. With the db, I can do that...and there are a lot of examples of that here. But I can't figure out where the transients are...if they aren't in the database..and I don't see them in the xcache admin.

Thanks for any insights..

2
  • Transients are stored in your database in wp_options table.
    – Robert hue
    Jan 9, 2015 at 6:31
  • That is true some of the time, Robert Hue, but not all. They may be stored in the database, but also potentially in various object caches. I'm trying to track down how to find out where...
    – johnmontfx
    Jan 9, 2015 at 18:21

2 Answers 2

1

In general you don't work with storage in the case of transients. You work with Transients API which makes use of either database or Object Cache.

Object Cache implementations are arbitrary, it can make use of any back end out there anyone bothers to make work.

So effectively there is no way to bulk delete transients since there is no bulk delete in underlying Object Cache (some back ends just won't be able to perform such operation).

For a public solution you would have to engineer your caching logic in a way that functions within these constraints.

One of the common techniques is including some sort of identifier in transient keys, so that changing identifier effectively makes "old" set inaccessible. Take note of key length limitations though.

0

WordPress stores transients in either the options table of the database or in the global wp_object_cache object - which requires a key-value backend (thanks Rarst for the clarification). It first detects whether there's a cache object present, and if there is it uses that. If an cache object isn't present, then it falls back to the database.

Without knowing whether is enabled, we don't know where WordPress will store the transients. For a lot of cases, this doesn't matter, but it can be hard if you want to get all transients with a prefix, as you're doing. If you knew the transients were being stored in the database, it would be simple to construct a SQL query to get the transients. The problem is with the cache object.

One solution would be to store all your theme/plugin transients in an array. Then you would use the WordPress transients API to store the values either in the database or in the object cache.

Wrapper Class

/**
 * Class to store all theme/plugin transients as an array in one WordPress transient
 **/
class wpse_174330_transient {
  protected $name;
  protected $transient;

  public function __construct( $name ){
    $this->name;
    $this->transient = ( false !== ( $t = get_transient( $name ) ) ) ? $t : [];
  }
  public function all(){
    return $this->transient;
  }
  public function flush(){
    $this->transient = [];
    delete_transient( $this->name );
  }
  public function set( $transient, $value, $expiration = '' ){
    $this->transient[ $transient ] = $value;
    $this->update();
  }
  public function get( $transient ){
    return isset( $this->transient[ $transient ] ) ?
      $this->transient[ $transient ] : false;
  }
  public function delete( $transient ){
    unset( $this->transient[ $transient ] );
    return $this>update();
  }
  protected function update(){
    return set_transient( $this->name, $this->transient );
  }
}

Usage

$theme_transients = new wpse_174330_transient( 'wpse_174330' );
$theme_transients->set( 'header_color', 'blue' );
$theme_transients->get( 'footer_color' );

//* Delete all the transients
foreach( $theme_transients->all() as $name => $value ){
  $theme_transients->delete( $name );
}

//* Flush 'em
$theme_transients->flush();
1
  • OpCache is tad incorrect term to use, though it's a common mix-up since traditionally APC provided both opcache and key/value store. But these are separate functions and mean different things. WP's Object Cache needs key/value store backend of some kind, not OpCache.
    – Rarst
    Apr 26, 2017 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.