I am new to this memcache stuff. I read that it saves lot of time of page load time by caching the database key. I have my php application and I want to use memcache as it takes too much time for my application to retrieve the whole set of results from database.
Asked
Active
Viewed 1.9k times
1 Answers
21
Steps to take: Prerequisite: read the manual (at least a bit): http://php.net/manual/en/book.memcache.php and make sure you've got memcached installed.
1 Start the memcached program so there is a server that actually stores the key-value pairs.
2 Make a memcache object
3 Store a value for a key
4 Retrieve your key.
$memcache = new Memcache; //point 2.
$memcache->set($yourUniqueKey, $yourValue, false, 3600); //point 3
//later:
$memcache->get($yourUniqueKey); //point 4.

Nanne
- 64,065
- 16
- 119
- 163
-
Hmm. for some reason I can't get the formatting to work this morning. sorry :( – Nanne Jan 24 '11 at 07:22
-
what should i do if i want to use memcached while hosting my application in a public domain – R.Raj Prashanth Jan 24 '11 at 07:57
-
Wat do you mean, 'in a public domain' ? On a shared server? – Nanne Jan 24 '11 at 11:13
-
i meant something like that where we host our website. I am sorry that i am bad at terms – R.Raj Prashanth Jan 24 '11 at 12:03
-
well, you have to have memcached installed and running. You should take that up with your host, if they support that. – Nanne Jan 24 '11 at 12:07
-
You should also make sure you understand that memcache and memcached are similar, but independent extensions. See this other stackoverflow thread for an explanation: http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php – Jody Jan 24 '11 at 20:37
-
@Nanne: Are you aware if Memcache(d) can be run on shared servers? In particular, GoDaddy's shared Linux hosting? – Ayush May 21 '12 at 07:09
-
I'm not sure where you are going with that? The whole "and what to do on a shared server" discussion seems a rather strained extention of the initial question "how does memcache(d) work", I don't think there's a need to make a "how to help someone fix memcache(d) on their godaddy server" tutorial in the comments? :D – Nanne May 21 '12 at 07:51
-
I had to add $memcache->connect('localhost', 11211) or die ("Could not connect"); to get the above to work right. – Mark Dec 10 '16 at 18:26