Can you store an Array in Memcache?
I would like to store;
- A user ID number
- A user's photo URL
- A users name
Together as an array, someone told me you could and then someone told me you couldn't Which is it?
Can you store an Array in Memcache?
I would like to store;
Together as an array, someone told me you could and then someone told me you couldn't Which is it?
Yes.
Memcache::set('someKey', array(
'user_id' => 1,
'url' => 'http://',
'name' => 'Dave'
));
Please see the documentation for very detailed examples.
Hi,
You can store almost whatever you want in memcached.
See the documentation of memcache::set
, which says :
bool Memcache::set(string $key, mixed $var [, int $flag [, int $expire ]] )
var
The variable to store. Strings and integers are stored as is, other types
are stored serialized.
So, yes, you can store an array, or even an object if needed :-)
BTW, it's the same with Memcache::add
and Memcache::replace
max storage size of an item in memcache is 1,048,576 Bytes (1MB), serializing an array does take a bit of space.
if you were to structure your array as simply as:
array(
[0] => 1,
[1] => 2,
[2] => 3
)
key being auto generated and value being the user id.
a serialized array of 5000 users numbered 1-5000 using this structure has a string length of 67792 characters, 50000 users produces an array of 777794 characters.
numbering 100000 to 150000 produces a serialized string of 838917 characters.
so at 50k users (as referenced in your previous question), you will likely be under the 1MB limit. if you have a local cache though (APC, etc...), use that instead, or if for any reason do NOT need all the IDs at once, I would highly suggest splitting up the results or simply using the DB.
also think about the data structure you are storing in memcached. If you are using the cache simply to give you a list of primary keys to lookup, do you need the other data?