Skip to content

Instantly share code, notes, and snippets.

@kolunar
Last active September 1, 2017 10:00
Show Gist options
  • Save kolunar/93af5da3b4720a80a947a3552850746c to your computer and use it in GitHub Desktop.
Save kolunar/93af5da3b4720a80a947a3552850746c to your computer and use it in GitHub Desktop.
Caching

Caching in PHP using the filesystem, APC, Memcached and Redis.

Cache per script data in associative array.

If you have some data read from database which you need several time during page creation cache it locally do not depend on any other types of caches.

use APC Cache for single node applications

If you have just one web server or your data set is small so it fits in memory in each of the servers APC Cache may be the most efficient way for you to cache the data.

Use Memcached for large scale applications

If local node cache is not large enough to cache good amount of data caching on network by using memcached is very good way to go.

File Cache is good for long term storage

If you need something stored long term or something which needs to be cached but does not fit even in distributed memory you can use file cache (ie on shared storage).

Query Cache is good when there is no other cache

If you do not do any other caching for certain object or if you cache on different level (ie single object constructed from multiple query results) MySQL Query Cache may improve performance of your application.

Multiple layers of caching may do well

Same as CPUs have multiple layer of caching and then same data may be stored in OS file cache, than SAN Cache you may implement multiple levels of caching for your application. In different circumstances different layering may make sense. For example you might wish to use APC Cache as L1 cache and File cache as L2 cache if you have large amount of data in long term cache. If you need something like this you might take a look at Eaccelerator which is APC alternative which supports caching user data both on disk and in shared memory. Ref: https://www.percona.com/blog/2006/08/09/cache-performance-comparison/

For more information about Caching:

https://www.digitalocean.com/community/tutorials/how-to-configure-apache-content-caching-on-centos-7 https://www.digitalocean.com/community/tags/caching?type=tutorials

Redis vs Memcached

https://www.digitalocean.com/community/tutorials/how-to-configure-redis-caching-to-speed-up-wordpress-on-ubuntu-14-04

Is Joomla ready for Redis ?

https://developer.joomla.org/joomlacode-archive/issue-33542.html

File Cache vs Memcached vs APC:

https://evertpot.com/107/

PHP performance and memory usage

PHP caching: shm vs. apc vs. memcache vs. mysql vs. file cache (update: fill apc from cron)

Lessons learned:

  • shm/apc are 32-60 times faster than memcached or mysql
  • shm/apc are 2 times faster than php file cache with apc
  • php file cache with apc is 15-24 times faster than memcached or mysql
  • mysql is 2 times faster than memcached when storing more than 400 bytes
  • memcached is 2 times faster than mysql when storing less than 400 bytes
  • php file cache with apc is 2-3 times faster than normal file cache
  • php file cache without apc is 8 times slower than normal file cache

Tests were made with PHP 5.3.10, MySQL 5.5.29, memcached 1.4.13, 64bit, 3.4GHz (QEMU): http://we-love-php.blogspot.com/2013/02/php-caching-shm-apc-memcache-mysql-file-cache.html

Files are on disk :

  • Not quite fast ; and concurrent access are not great at all, if several processes try to read/write at the same time
  • Local to one server (if you have several servers, you'll have to store the files on each one of them -- NFS being slow)
  • But you have a lot of space

APC is in memory :

  • Really fast
  • But you have less space
  • And it's local to each server too

memcached is in memory, on a network cluster :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment