robert.little
robert.little

Reputation: 410

PHP memcache - store whole objects?

I have some application translations to be cached, for example:

$translations = [
    'en' => [
        'key_1' => 'translation 1',
        'key_2' => 'translation 2',
        ...
        'key_10000' => 'translation 10000',
    ],
    'de' => [
        'key_1' => 'translation 1',
        'key_2' => 'translation 2',
        ...
        'key_10000' => 'translation 10000',
    ],
];
$memcache->set('translations', $translations);

If I want to access just few keys on a page, I have to load the whole array and so it consumes a lot of memory. Isn't it better to store each translation as an extra key of memcache? Isn't it slower to access it many times from memcache?

Like here:

$memcache->set('translations_en_key_1', 'translation 1');
$memcache->set('translations_en_key_2', 'translation 2');
...
$memcache->set('translations_en_key_10000', 'translation 10000');

$memcache->set('translations_de_key_1', 'translation 1');
$memcache->set('translations_de_key_2', 'translation 2');
...
$memcache->set('translations_de_key_10000', 'translation 10000');

Thanks!

Upvotes: 1

Views: 371

Answers (1)

pratikvasa
pratikvasa

Reputation: 2045

Well It will depend on how many individual keys you actually use. So for example if you use 100 keys each time you access a page then it would make more sense to do a multiget request rather than getting such a huge object.

Also you should know that there is a limit of 1MB for the size of the object that you store. So if each translation is taking 25 Bytes then you will not be able to store more than 40000 translations.

Also If your translation objects are as small as 25B you can do some optimiztions to store more objects in memcache. You can take a look at this link

And you can read this to better understand the internals of memcache

Upvotes: 1

Related Questions