tags:

views:

1494

answers:

3

Can you store an Array in Memcache?

I would like to store;

  1. A user ID number
  2. A user's photo URL
  3. A users name

Together as an array, someone told me you could and then someone told me you couldn't Which is it?

+6  A: 

Yes.

Memcache::set('someKey', array(
    'user_id' => 1,
    'url' => 'http://',
    'name' => 'Dave'
));

Please see the documentation for very detailed examples.

hobodave
Great thanks and in my case the userid,url,and name would all be an array inside an array of up to 5,000 users, is that bad?
jasondavis
Do you have a REASON to store 5K records in memcache? I think you may be missing the point of caching.
Doomspork
It depends. Given my simple example, that array would take ~500 bytes. Multiplying that by 5,000 you're at 2.5 MB. That's a pretty large object to be sticking in memcached.
hobodave
Currently on every page load I have to do a query to mysql to get posted blogs ONLY from a user that is in a user friend list, this is a large query with a few joins because it has to return a blog subject and the posters picture url and username. SO the goal was to cache the friendlist part and just update it every so often. I didn't want to cache the result of blog id's that match because this changes about every couple minutes as new items are added. I don't have many options
jasondavis
Is it possible to clear a users cache when they log out from my site?
jasondavis
Yes you can clear the cache. Read the documentation in the link above. If you have more questions please post as a new quesiton instead of commenting.
hobodave
Why clear the cache? You seem to be missing the entire point of memcached and caching in general. Memcached will automatically drop the oldest records when it runs out of memory, so if you're not requesting the object it will disappear.
askegg
A: 

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

Pascal MARTIN
+1  A: 

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?

Jason
What I am trying to accomplish is like a friend feed, the reason I would need all user ID's is becauseI can only show actions in the feed that are from the users friends. I am researching for the best way to do this and it just seems instead of doing multiple joins on every page load to get this data I was hoping to use memcache to speed it up and take some strain off mysql, again i'm just researching a good way right now so i'm open to suggestions on everything.
jasondavis
If I were able to store a users friend list and photo_url and username that would eliminate like 2 joins to fetch pic and name from user table, and then another to friend table. The bad thing is a user with 5k friends, even if I have there friendlist as a list in memory, I would still have to use IN (1,2,3,4) to lookup there friends actions to display but I think it would be faster then looking everything up with mysql, also this feed in on the user homepage to it is called very frequently, and I don't think I can cache the friend feed because it's updated constantly.
jasondavis
User's friend list is updated often but I think I could just recache it when they add a new user. I should not 5k friends will be the max I will be limiting account to only add that many and many many account won't have the full ammount some users will only have a few hundred
jasondavis
do you already have an example mysql query that you are using? I recently had a similar issue (user activity streams) and found that i was able to greatly optimize the mysql query itself.
Jason
and do you have any more details on your currently mapped out db structure? what is your base table like that you are using to lookup upon? and what would an example query on the base table look like?
Jason