Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Memcached tutorial

memcached php tutorial

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 18 July 2014 - 09:20 AM

Hello,

 

I wanted to know if any of you ever read a great tutorial about memcached and php.

All the one I can find is only 1 or 2 scripts exemples and keeping it very basic (too basic to really learn anything)

I'm looking for a tutorial that would go in depth about everything (installing, optimization, design pattern...)

 

Anyone know where I could find this magic tutorial?


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 18 July 2014 - 02:26 PM

What about this one ?

 

And how did it go with the table size reduction?



#3 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 20 July 2014 - 07:54 PM

Doesn't seem to have any real options, pretty much only how to use their admin

 

And for the table size reduction, for the time being, I'll leave it like that, and when it will become even bigger, I'll split my table with sql partitions (never worked with that, but always wanted to find a reason to do it)


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts

Posted 23 July 2014 - 05:46 AM

I think the question is, what do you wish to do? As PHP is an interpreted language, it must compile an intermediate object to be ran, and storing that in a memory cache (it may directly store it in a tmpfs within the shared memory, on Linux, being /dev/shm) will increase the performance of scripts. As mentioned, the simplicity of an admin interface to handle the storage/flushing of web page objects, is the most common use case and requires little intervention/no programming, with great gain in performance.

 

If you are referring to the API to leverage its use for other means, there is nowhere better to start than the PHP manual: http://php.net/manua...k.memcached.php

 

There you will find functions to connect to the local memcached server through libmemcached, store objects in shared memory if need be, and retrieve them. You will have to have a script, or objects, which can benefit from having such a library and service however and I cannot personally find too much off the top of my head.

 

If you are spitting out a huge database list or similar as I gather from your guys' database speak, website caching through the appropriate HTTP headers for example is a common method of preserving database loading by having them load static content, or taking advantage of the appropriate caching techniques your database actually provides (i.e. SELECT caching with MySQL http://dev.mysql.com...uery-cache.html ), or a relational database containing these features if there are any available.

 

If you are using memcached for this, for an exercise, or however, then simply checking if the content is cached through the method ->get('desired key')is not false, you can send the user that content instead of anything within the if statement (such as opening a database connection, or file, whatever it may be.) from shared memory.

echo "Some dynamic content: " . time();

$retrieved = $memcached->get('something');
if($retrieved === false) {
  //do intensive stuff, load a file, do query ...'
  $data = file_get_contents('foobar.txt');
  $memcached->set('something', $data, $expiration);
}

echo "\nSome possibly static content: "; 
echo $retrieved;

Do these help any?

 

Alexander.


Edited by Alexander, 23 July 2014 - 06:04 AM.

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts

Posted 25 July 2014 - 07:33 AM

(sorry for the delay to answer)

 

What do I wish to do: simply speed up my application

I read a lots of article that with it some compagny has X numbers of server and since memcached they only needed Y servers

 

So you are saying that simply installing it, no need to change my code, I should see a increase in speed?

 

I saw some article about the get and set options, but what if I have multiple servers, do I need to setup some sort of master/slave or master/master replication between the memcached server?

Should it be on a separated server or it is recommanded to be on the same server?

 

And what about naming convention for the cached data?

 

I have some sql request that take a lots of power, and rarely change, if I could cache the result, and maybe have a way to delete the cache when I know the result has changed it would be great!


You can now stalk me on linkedin: http://ca.linkedin.c...elle/24/b44/88/ !


#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts

Posted 25 July 2014 - 12:59 PM

Unless your site has so much to cache in memory it will require so much memory you will need multiple servers (and this is only a theory, as I have not seen any examples of such a use) then you will still benefit from just installing and activating the memcached plugin of PHP and finding what it can be used for.

 

For more basic and convenient performance as I had mentioned above, you may cache the intermediate code generated by PHP through a more conventional way with little setup necessary, through installing APC (preferably, it will be included within PHP 6!), Xcache, or Eaccellerator, and then all PHP intermediate code will be stored in memory.

 

If you have a huge library, wp.php, etc, it will for example remain compiled in its ready-to-load state and all following pages using it will not need to reinterpret it.

 

For database alone, remember I had mentioned cached queries above? One part of successful caching is determining how stale it is, and if you use the built in caching abilities of your database management system it should automatically flush the cache when your database entries change.

 

I think memcached is only particularly useful if you have a specific use to leverage its caching ability, and the ability to script it, which I cannot think too much off the top of my head of uses beyond an drop-in accelerator mentioned above.

 

Fairly static sites using cPanel or MediaWiki or Joomla or WP, etc, will have very little change in content hour-by-hour, which can mean thousands of requests use the exact same queries and code, and that is where the real benefit of this comes from. If some take advantage of specific things with Memcached over a global, and equally equitable, code caching plugin like APC, then some extra thought was put in to optimising every last bit - but then again, others seem to be more popular.

 

Alexander.


Edited by Alexander, 25 July 2014 - 01:06 PM.

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.