Skip to content

[apcu] Clarify apcu_fetch() behaviour when an array of keys is given - #60

Open
officialasishkumar wants to merge 2 commits into
php:mainfrom
officialasishkumar:fix/apcu-fetch-array-keys
Open

officialasishkumar wants to merge 2 commits into
php:mainfrom
officialasishkumar:fix/apcu-fetch-array-keys

Conversation

@officialasishkumar

Copy link
Copy Markdown
Contributor

The apcu_fetch() page described the return value as "the stored variable or array of variables". That wording suggests the function sets variables, and it doesn't explain what you get back when you pass an array of keys.

This change documents what php_apc.c actually does:

  • When key is an array, the function returns an associative array that maps each key found in the cache to its value. Keys that aren't in the cache are left out of the result.
  • In that case success is always set to true, even if none of the keys exist.
  • Because false can be stored as a value, a note explains that success is how you tell a stored false apart from a failure.

It also:

  • types key as string|array instead of mixed, matching the extension stub;
  • changes the purpose line to talk about values rather than variables, as suggested in the issue discussion;
  • adds an example that fetches several entries at once and destructures the result.

Fixes #19

@jordikroon jordikroon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you. This looks great. Some minor nits to clarify things a bit better.

Comment thread reference/apcu/functions/apcu-fetch.xml Outdated
Comment on lines +29 to +31
<function>apcu_store</function>). If an array of keys is passed then
each key is fetched, and the entries which are found in the cache are
returned as an array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
<function>apcu_store</function>). If an array of keys is passed then
each key is fetched, and the entries which are found in the cache are
returned as an array.
<function>apcu_store</function>). If an array of keys is passed then
each key is fetched, and the entries which are found in the cache are
returned as an array. Array elements that are not strings are skipped
and an <constant>E_WARNING</constant> is raised.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the note about non-string elements being skipped with an E_WARNING in 3bcebfe.

Comment thread reference/apcu/functions/apcu-fetch.xml Outdated
Comment on lines +50 to +51
If <parameter>key</parameter> is a string, returns the stored value on
success, or &false; on failure.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
If <parameter>key</parameter> is a string, returns the stored value on
success, or &false; on failure.
If <parameter>key</parameter> is a string, returns the stored value, or
&false; if the key does not exist in the cache or has expired.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied, the return value now says it's false when the key doesn't exist or has expired. Done in 3bcebfe.

The return values section described the result as "the stored variable
or array of variables", which suggested that apcu_fetch() would somehow
create variables, and did not explain what is actually returned for an
array of keys.

Document the behaviour as implemented in php_apc.c:

- when key is an array, an associative array mapping each key found in
  the cache to its value is returned, and missing keys are omitted;
- in that case the success parameter is always set to true, even if
  none of the keys exist;
- since false can be stored as a value, success is the way to tell a
  stored false apart from a failure.

Also type the key parameter as string|array instead of mixed, matching
the extension stub, describe the function in terms of values rather than
variables, and add an example fetching several entries at once and
destructuring the result.

Fixes php#19
Mention that non-string elements of an array of keys are skipped with an
E_WARNING, and state that a string key returns false when the entry does
not exist in the cache or has expired.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

apcu_fetch returns associative array and not variables

2 participants