How to use memcached with php
How to use memcached with php
A few months a go I showed you how you can install the memcached server and also how to get the php extension installed, but never did showed you how you can use it, right? There are a number of cases where you would use this, be it to cache mysql queries, login sessions or any type of data that could repeat themselves over an over. Using memcached server to cache such data will drastically improve performance and speed your website at least by 50%. However not everyone knows how this is working and also some don’t even know what memcached is. In this article I’m going to show you how to can use memcached with php using the already installed php extensions for it.
Making connection to memcached server
When working with standalone server from whiting a php code, it is imperative that you create the connection to the server, here this is called a client-server connection between php and memcached server. To do this we can use the bellow code:
1 2 |
$memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); |
- first line would call the memcache class
- second line uses the object from the class, connect, to open connection to your memcached server
This is of course in the situation where memcached server is running on default port 11211 and on the same server machine you run php.
Add objects to memcached
Our next step is to build ourselves a little object that we can then add it to memcache server. It is recommended to use objects instead of strings, more control can be made using it like this. So for example, using class stdClass for this tutorial we can then use:
1 2 3 |
$my_object = new stdClass; $my_object->name_attr = 'Bogdan'; $my_object->int_attr = 24; |
Next would be to store our value to our memcached server, we can do it like so:
1 2 |
$memcache->set('key_SSN', $my_object, false, 60) or die ("Failed to save data at the server"); echo "Keep data in cache (data will expire in 60 seconds)<br/>\n"; |
This will store the data temporarily for 60 seconds. I added also a false statement there, meaning I don’t want the information to be compressed. Be careful with this as you can run with errors like:
1 |
MemcachePool::get(): Failed to uncompress data |
The ‘key_SSN’ used should also be unique as this will be used to get the data from memcache. The last option here is the timeout setting, you can set a timeout to expire after a certain amount, here this is set to 60 seconds.
To always keep the object cached, you need to use 0 (zero), you can not use a number more then 2592000 (30 days) here!
Getting objects from memcached server
Getting the information from memecached is the thing we need and are interested. The speed of which we can retrieve the objects from the memcached server is much much higher if we request the information needed from a database or if we do it from disk as a file cache. It is actually the RAM’s frequency speed here so its obviously faster!
When getting information from our memcached server, we always need to be sure we are requesting the correct key value. Meaning we need to have some sort of unique key value, but which would always be the same on the course of a browser session for the same object. It can be almost anything as long as you start it with a letter first.
To get the value from our memcached server we use:
1 2 3 |
$memcache_query = $memcache->get('key_SSN'); echo "Data from the cache:<br/>\n"; var_dump($memcache_query); |
The result from this would be:
1 2 3 4 5 6 |
object(stdClass)#1 (2) { ["name_attr"]=> string(6) "Bogdan" ["int_attr"]=> int(24) } |
Of course this is just an example of object, but the possibilities are really a lot, not mention when you use mysql queries and cache them in memcache things can be see much lighter, faster.
Anyway, this is the simple store and get option you would need to use with memcache from a php script.
We will see you again in our next tutorial, that’s it for now!