[wp-trac] [WordPress Trac] #55969: The function set_transient should have the autoload argument

WordPress Trac noreply at wordpress.org
Mon Jun 13 10:57:48 UTC 2022


#55969: The function set_transient should have the autoload argument
-------------------------+------------------------------
 Reporter:  giuse        |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Database     |     Version:  6.0
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  performance
-------------------------+------------------------------

Comment (by giuse):

 Do you see any downsides in modifying the function with something like
 this?



 {{{#!php
 <?php
 function set_transient( $transient, $value, $expiration =
 0,$custom_autoload = null ) {

     $expiration = (int) $expiration;

     /**
      * Filters a specific transient before its value is set.
      *
      * The dynamic portion of the hook name, `$transient`, refers to the
 transient name.
      *
      * @since 3.0.0
      * @since 4.2.0 The `$expiration` parameter was added.
      * @since 4.4.0 The `$transient` parameter was added.
      *
      * @param mixed  $value      New value of transient.
      * @param int    $expiration Time until expiration in seconds.
      * @param string $transient  Transient name.
      */
     $value = apply_filters( "pre_set_transient_{$transient}", $value,
 $expiration, $transient );

     /**
      * Filters the expiration for a transient before its value is set.
      *
      * The dynamic portion of the hook name, `$transient`, refers to the
 transient name.
      *
      * @since 4.4.0
      *
      * @param int    $expiration Time until expiration in seconds. Use 0
 for no expiration.
      * @param mixed  $value      New value of transient.
      * @param string $transient  Transient name.
      */
     $expiration = apply_filters( "expiration_of_transient_{$transient}",
 $expiration, $value, $transient );

     if ( wp_using_ext_object_cache() || wp_installing() ) {
         $result = wp_cache_set( $transient, $value, 'transient',
 $expiration );
     } else {
         $transient_timeout = '_transient_timeout_' . $transient;
         $transient_option  = '_transient_' . $transient;

         if ( false === get_option( $transient_option ) ) {
             $ccustom_autoload_is_valid = null !== $custom_autoload &&
 in_array( $custom_autoload,array( 'no','yes',true,false ) );
             $autoload = $custom_autoload_is_valid ? $custom_autoload :
 'yes';
             if ( $expiration ) {
                 $autoload = $ccustom_autoload_is_valid ? $custom_autoload
 : 'no';
                 add_option( $transient_timeout, time() + $expiration, '',
 $autoload );
             }
             $result = add_option( $transient_option, $value, '', $autoload
 );
         } else {
             // If expiration is requested, but the transient has no
 timeout option,
             // delete, then re-create transient rather than update.
             $update = true;

             if ( $expiration ) {
                 $autoload = $ccustom_autoload_is_valid ? $custom_autoload
 : 'no';
                 if ( false === get_option( $transient_timeout ) ) {
                     delete_option( $transient_option );
                     add_option( $transient_timeout, time() + $expiration,
 '', $autoload );
                     $result = add_option( $transient_option, $value, '',
 $autoload );
                     $update = false;
                 } else {
                     update_option( $transient_timeout, time() +
 $expiration,$autoload );
                 }
             }

             if ( $update ) {
                 $result = update_option( $transient_option, $value );
             }
         }
     }

     if ( $result ) {

         /**
          * Fires after the value for a specific transient has been set.
          *
          * The dynamic portion of the hook name, `$transient`, refers to
 the transient name.
          *
          * @since 3.0.0
          * @since 3.6.0 The `$value` and `$expiration` parameters were
 added.
          * @since 4.4.0 The `$transient` parameter was added.
          *
          * @param mixed  $value      Transient value.
          * @param int    $expiration Time until expiration in seconds.
          * @param string $transient  The name of the transient.
          */
         do_action( "set_transient_{$transient}", $value, $expiration,
 $transient );

         /**
          * Fires after the value for a transient has been set.
          *
          * @since 3.0.0
          * @since 3.6.0 The `$value` and `$expiration` parameters were
 added.
          *
          * @param string $transient  The name of the transient.
          * @param mixed  $value      Transient value.
          * @param int    $expiration Time until expiration in seconds.
          */
         do_action( 'setted_transient', $transient, $value, $expiration );
     }

     return $result;
 }
 }}}



 Then, if I want to set a transient that expires for instance in 90 days,
 and I want the autoload (e.g. for a transient that is needed for 90 days
 and has to be autoloaded:

 {{{#!php
 <?php
 set_transient( 'my_transient',$my_value, 60*60*24*90,'yes' );
 }}}


 Without any workaround and any additional code.


 IF I want a transient without expiration but also without autoload (e.g.
 for a transient that is needed only in the backend:

 {{{#!php
 <?php
 set_transient( 'my_transient',$my_value,false,'yes' );
 }}}


 Without workarounds and without additional code.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55969#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list