Skip to content

fix(memcache): preserve float values in Redis-based cache backends#62104

Open
joshtrichards wants to merge 3 commits into
masterfrom
jtr/fix-memcache-encoding
Open

fix(memcache): preserve float values in Redis-based cache backends#62104
joshtrichards wants to merge 3 commits into
masterfrom
jtr/fix-memcache-encoding

Conversation

@joshtrichards

@joshtrichards joshtrichards commented Jul 14, 2026

Copy link
Copy Markdown
Member
  • Resolves: # (hard to say)

Summary

Preserve float values in Redis/Valkey-backed memcache backends.

This updates Redis and KeyValueCache to:

  • decode values via json_decode(...) instead of coercing all numeric payloads to int
  • encode floats with JSON_PRESERVE_ZERO_FRACTION so 1.0 stays distinct from 1

It also adds shared round-trip test coverage in tests/lib/Memcache/Cache.php, including floats and numeric edge cases.

Before / after

Before

  • 3.143
  • 1.01

After

  • 3.143.14
  • 1.01.0

Notes

This slightly changes the cache format for values like 1.0, so older entries may not compare equal in Redis-side CAS/Lua operations until they expire and are rewritten.

Down the road we may want to consider using _pack / _unpack (at least for phpredis; not sure about predis).

TODO

  • Drop redundant round-trip tests from KeyValueCacheTest once deciding what to do about the embedded comments there

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Signed-off-by: Josh <josh.t.richards@gmail.com>
Signed-off-by: Josh <josh.t.richards@gmail.com>
@joshtrichards joshtrichards added this to the Nextcloud 35 milestone Jul 14, 2026
@joshtrichards joshtrichards requested a review from a team as a code owner July 14, 2026 14:05
@joshtrichards joshtrichards requested review from Altahrim, ArtificialOwl, come-nc and icewind1991 and removed request for a team July 14, 2026 14:05
@joshtrichards joshtrichards added bug 2. developing Work in progress feature: caching Related to our caching system: scssCacher, jsCombiner... labels Jul 14, 2026

protected static function decodeValue(string $value): mixed {
return is_numeric($value) ? (int)$value : json_decode($value, true);
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
return json_decode($value, true, flags: JSON_THROW_ON_ERROR);

*/
protected static function decodeValue(string $value): mixed {
return is_numeric($value) ? (int)$value : json_decode($value, true);
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
return json_decode($value, true, flags: JSON_THROW_ON_ERROR);

@susnux susnux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but instead of using the default you can just use the named parameter so its a bit more clear why it is set (you not actually setting the depth but only the flags)

@joshtrichards joshtrichards added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review Waiting for reviews bug feature: caching Related to our caching system: scssCacher, jsCombiner...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants