Make WordPress Core

Opened 19 months ago

Last modified 15 months ago

#63022 new enhancement

Update cache.php - Improve function wp_cache_decr

Reported by: docjojo Owned by:
Priority: normal Milestone: Awaiting Review
Component: Cache API Version:
Severity: normal Keywords: has-patch close
Cc: Focuses: performance

Description

Make the lines 519-549 in class-wp-object-cache.php obsolete by adjusting the function call to the function wp_cache_decr, line 252 in cache.php.

Change

return $wp_object_cache->decr( $key, $offset, $group );
to
return $wp_object_cache->incr( $key, ((int) $offset) * -1), $group );

because DECR by offset == INCR by (offset * -1)

Change History (5)

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


19 months ago
#1

  • Keywords has-patch added

Make the lines 519-549 in class-wp-object-cache.php obsolete by adjusting the function call to the function wp_cache_decr, line 252 in cache.php.

Change

return $wp_object_cache->decr( $key, $offset, $group );
to
return $wp_object_cache->incr( $key, ((int) $offset) * -1), $group );

because DECR by offset == INCR by (offset * -1)

Trac ticket:

https://core.trac.wordpress.org/ticket/63022#ticket

#2 in reply to: ↑ description @ameliamoon
19 months ago

Replying to docjojo:

Make the lines 519-549 in class-wp-object-cache.php obsolete by adjusting the function call to the function wp_cache_decr, line 252 in cache.php.

Change

return $wp_object_cache->decr( $key, $offset, $group );
to
return $wp_object_cache->incr( $key, ((int) $offset) * -1), $group );

because DECR by offset == INCR by (offset * -1)

i have the same problem

.................

return $wp_object_cache->decr( $key, $offset, $group );
to
return $wp_object_cache->incr( $key, ((int) $offset) * -1), $group );

Last edited 15 months ago by ameliamoon (previous) (diff)

#3 follow-up: @flixos90
17 months ago

  • Keywords close added; wp_cache_decr removed
  • Version 6.7.2

@docjojo Thank you for raising this.

Can you share some context on why this is useful? Given that there is already a decr() method required on the object class implementation, I don't see a benefit of not using it.

Now if we were to rearchitect this from scratch, I could see it being useful to avoid the method. But not using it when we have it does not seem useful to me. It would only make sense to me if we planned to deprecate this method and no longer require it on the object cache implementation. But I'm not sure it's worth the change. Thoughts @tillkruess?

#4 @tillkruess
17 months ago

Seems harder to read/understand without any benefit?

#5 in reply to: ↑ 3 @docjojo
17 months ago

It’s not about removing the functionality — it’s about reducing code duplication.

The idea is to let decr() simply delegate to incr() with a negative offset:

return $wp_object_cache->incr( $key, ((int) $offset) * -1), $group );

This allows decr() to be removed entirely (30 lines gone), with no functional side effects and no additional function calls — just a bit of math.

Fewer lines to maintain, less to compile, and cleaner internal logic.

C.

Replying to flixos90:

@docjojo Thank you for raising this.

Can you share some context on why this is useful? Given that there is already a decr() method required on the object class implementation, I don't see a benefit of not using it.

Now if we were to rearchitect this from scratch, I could see it being useful to avoid the method. But not using it when we have it does not seem useful to me. It would only make sense to me if we planned to deprecate this method and no longer require it on the object cache implementation. But I'm not sure it's worth the change. Thoughts @tillkruess?

Note: See TracTickets for help on using tickets.