krakjoe / apcu

APCu - APC User Cache
Other
957 stars 193 forks source link

Idea: Add `apcu_fetch_field(string $key, int|string|array $field, bool $force = false): [int $status, mixed $value]` #461

Open TysonAndre opened 1 year ago

TysonAndre commented 1 year ago

Related to https://github.com/krakjoe/apcu/issues/175

Motivation: Similar to 175, there may be large arrays that are expensive to compute which would be slow to fully unserialize. Storing them in a single APCu entry ensures that the cache would be complete and not partially evicted. Using apc.serializer=default (but not other serializers), apcu is already storing the large arrays in a way that individual fields can be queried and unserialized (HashTable) without unserializing the other fields

I use apcu to store big arrays that cost a lot to produce from xml config files. These arrays are pure configuration data : my source only use them in a ready-only way. I suppose that this is a very common use of the apcu extension.

Moreover the content of these big arrays is the same for a lot of sites served by the same php-fpm process. Which means that where I could have only one instance foreach configuration array I've got hundreds...

Alternately, output references could be used

// example usage
apcu_store('mykey', ['a' => 123, 'b' => ['x']]);
var_dump(apcu_fetch_field('mykey', 'b')); // [APCU_FOUND_ENTRY, ['x']]

https://www.php.net/apcu_store

This would probably require adding a bool $prevent_serialize = false parameter to apcu_store/apcu_add (to either store the array in shared memory without calling a *serialize() function, or throw)