Make WordPress Core

Opened 2 years ago

Last modified 2 weeks ago

#56060 new enhancement

Implement wp_cache_replace_multiple() – and related WP_Object_Cache method

Reported by: johnjamesjacoby's profile johnjamesjacoby Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Cache API Keywords: 2nd-opinion has-patch
Focuses: performance Cc:

Description

See: #20875, #54574.

See also: https://github.com/WordPress/wordpress-develop/pull/2018#discussion_r799643985

🫠

I would like to replace multiple values in a cache, but not add or set them if they do not exist.

In my experience and understanding, replace() doesn't get much love because _exists() may be impossible to implement in certain backends – specifically (or especially) when $group is empty.

My current situation is essentially juggling multiple backends, and only wanting to replace multiple values if they exist on one server and not add or set them until some other unrelated things happen.

I went to call wp_cache_replace_multiple() and thought my IDE had deceived me when it wasn't autocompleting. Alas! Alas, I say!

I can handle this In my own drop-in, and I can account for it in my own codes, but I think perhaps it's worth reconsidering this in core for the sake of parity with everything else.

Thoughts?

Change History (4)

#1 @spacedmonkey
18 months ago

@tillkruess You interested in this one?

#2 @spacedmonkey
18 months ago

  • Milestone changed from Awaiting Review to Future Release

#3 @tillkruess
18 months ago

Would this function set keys that don't exist, or none if even one of the keys exists?

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


2 weeks ago
#4

  • Keywords has-patch added; needs-patch removed

Trac Ticket: Core-56060

## Overview

  • This Pull Request introduces the wp_cache_replace_multiple function, addressing the need to replace multiple cache values only if they already exist. This functionality is essential for scenarios involving multiple caching backends, allowing developers to manage cache data without unintentionally adding new entries.

## Problem Statement

  • The existing replace() method does not effectively check for the existence of multiple keys, particularly when the $group parameter is empty. This limitation can cause issues in managing caches across different backends, especially when updates are needed without creating new entries.

## Key Features

  • Conditional Replacement: The function checks if each specified key exists in the cache before attempting to replace its value, ensuring that only existing keys are updated.
  • Batch Processing: It utilizes set_multiple() for efficient bulk updates, minimizing backend calls and improving overall performance.
  • Return Values: The function returns an array of keys that were successfully replaced, providing clear feedback on the updates made.

## Benefits

  • Improved Cache Integrity: Prevents the addition of new entries, maintaining the integrity of existing cache data.
  • Performance Efficiency: Reduces unnecessary operations by only updating existing entries.
  • Usability: Enhances developer experience with a straightforward approach to manage cache values.
Note: See TracTickets for help on using tickets.