Reputation: 55
I have to create an application in PHP which will store the phone contacts of different users.
The table for storing the phone numbers is of the following format:
id,user-id,phone-number
Whenever an user tries to view all his contacts, first of all the contents from the cache should be shown and then the remaining ones from the database.
In implementing this, I have the following problem:
How to store the phone numbers in the cache? By this I mean what should be the key for inserting the phone numbers into memcache so that I can easily extract the data from it for any user. Remember, each user can have many phone numbers and there are many users.
Upvotes: 2
Views: 509
Reputation: 47311
the key should use user id
user id { mobile number ... + list of contact ...[which is the user id]}
// by accessing this key able to get all mobile number
// and his contact list
suggestion value should be as simple as possible,
no JSON, no serialize data
user id {m:xxx,xxxx..., c:yyy,yyy,yyy}
this is to save spaces and also save processing for json_decode / unserailize
when getting all contacts for an user
1. fetch cache using user id (one call)
2. iterate the list of contact
3. fetch parellel of all the contact (user) key * function handling
4. iterate to build a multi dimensional array
when user adding/update/delete a mobile number,
or adding/update/delete a mobile number
1. fetch cache using user id (one call)
2. expire the key using user id
3. iterate the cache result
your function should be if cache not exist in memcache,
fetch from database,
set memcache,
return the database result
Upvotes: 1
Reputation: 282
cache key format should be
12_contacts
where 12 is is the user id.
inside the key you would store the full list of contacts for that user as an array. generally this means that whenever the users contacts change you either clear or rebuild the memcache key. you need to store the full contact list inside the key as an array, storing a partial list and then querying for any others defeats the purpose.
Upvotes: 0