Make WordPress Core

Opened 13 years ago

Closed 2 weeks ago

Last modified 2 weeks ago

#22369 closed defect (bug) (invalid)

get_transient() do not call delete_transient() when deleting one

Reported by: juliobox's profile juliobox Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.8
Component: General Keywords: has-patch
Focuses: Cc:

Description

Hello

i was wondering why get_transient() is not calling delete_transient when deleting a timeouted transient.

Piece of code from get_transient()

				$transient_option = '_transient_' . $transient;
				$transient_timeout = '_transient_timeout_' . $transient;
				if ( get_option( $transient_timeout ) < time() ) {
					delete_option( $transient_option  );
					delete_option( $transient_timeout );
					return false;
				}

but i need to trigger the "deleted_transient" hook.
Can we do that:

				$transient_option = '_transient_' . $transient;
				$transient_timeout = '_transient_timeout_' . $transient;
				if ( get_option( $transient_timeout ) < time() ) {
					delete_transient( $transient_option  );
					return false;
				}

Change History (7)

#1 @nacin
13 years ago

  • Keywords close added
  • Version changed from trunk to 2.8

The transient isn't being "deleted" — it has simply expired and we've used the opportunity here to clean it up. (We could just as easily do it on cron or on upgrade — things we've considered.) A separate "expired transient" hook could be beneficial, but it wouldn't always work. That's because in environments with an object caching backend such as APC or Memcache, transient caching is completely offloaded there. Data in an external backend could get pushed out of cache at any time.

I do think the return false should go through the existing transient_$transient filter, though. When using an external backend, the false from wp_cache_get() does go through the filter, so this should as well.

#2 @juliobox
13 years ago

ok i understand, thank you !

#3 @johnbillion
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

I've opened #24685 for the transient_$transient filter issue. Going to close this one as invalid because, as nacin stated, the transient isn't being deleted at this point, it has expired.

Last edited 12 years ago by johnbillion (previous) (diff)

#4 @juliobox
16 months ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Hey there, i'm back.
So if it has expired, why not trigger an action hook?

				$transient_option = '_transient_' . $transient;
				$transient_timeout = '_transient_timeout_' . $transient;
				if ( get_option( $transient_timeout ) < time() ) {
					delete_option( $transient_option  );
					delete_option( $transient_timeout );
					do_action( 'transient_expired', $transient, $timeout );
					return false;
				}

Thanks

This ticket was mentioned in PR #7653 on WordPress/wordpress-develop by @nirajgirixd.


9 months ago
#5

  • Keywords has-patch added

## Description

This PR introduces a feature enhancement to get_transient() by adding a new action hook, transient_expired, which triggers when a transient has expired.

## Changes Made

  • Introduced a new action hook, transient_expired, which is triggered when an expired transient is detected.

## Trac ticket: https://core.trac.wordpress.org/ticket/22369

#6 @juliobox
4 weeks ago

hey, wanna merge the PR now? :)

#7 @desrosj
2 weeks ago

  • Keywords close removed
  • Resolution set to invalid
  • Status changed from reopened to closed

Hi @juliobox,

Is there any reason why the delete_option, delete_option_${option}, or deleted_option hooks cant be used to accomplish this?

The first one, delete_option, is called before any deletion occurs. So something like this would accomplish the same:

add_action( 'delete_option', function( $key ) {
	if ( ! str_contains( $key, '_transient_timeout' ) ) {
		return;
	}

	$timeout = get_option( $key );
	
	if ( ! empty( $timeout ) && $timeout < time() ) {
		// The transient has expired. Do whatever you need.
	}
});

If you need to know what the value is, you can just target the actual transient will be deleted first (( str_contains( $key, '_transient_timeout' ) || ~ str_contains( $key, '_transient_' ))

That said, adding a new filter is a distinctly different request (enhancement vs. a bug) than what this ticket originally proposed (calling delete_transient() within get_transient() when an expired transient is deleted. If you're interested in pursuing this further, could you please create a new ticket for that feature request?

Last edited 2 weeks ago by desrosj (previous) (diff)
Note: See TracTickets for help on using tickets.