Do you have a dynamic website built with MySQL and PHP? If your website and audience continue to grow, there may be problems with the performance of the website. Without caching mechanism your website becomes slow if your website gets more visitors than that can be handled by the web server. Do you want to know more about PHP Caching, Redis and Memcached? Keep reading …

Why does your website need a PHP cache?

Dynamic websites without caching cannot handle many visitors. If visitors access dynamic websites, all database queries and PHP script executions will use RAM memory and CPU power. Because all server resources are limited, your web server and website will be slow or not available.

For example…

We test the page of the website built with WordPress. We did the first test without caching activated. During the two CPU requirements to work harder and the burden is rather high. Currently 50 users together visit the website, 15 PHP processes have been used. Loading time URL (95th percentile) above 100 ms.

Next we did the same test as the cache was activated. The burden is much lower and the CPU is not used at all. Only 5 PHP processes that have been used when 50 users together have visited the site. The -Cache version is much faster with less than 50 ms, the URL load time (95th percentile).

The difference is not so large, except for the number of PHP processes (15 vs 5).

What type of php cache can you use to speed up your website?

There are various types of cache and all of them have different functions. Our list only covers the type of server side cache used together with PHP scripts. Other types of cache such as cache browser, database cache or cache proxy are not the scope of this article.

Full yard caching

Caching page is the most important type of PHP cache. As the name suggests, all PHP pages will be -Cache. In most cases, the -Cache page version is stored for a certain time on the hard disk server.

If you are looking for “Cache PHP”, you will find above the PHP Cache website. PHP Cache Company is a host for various PHP cache libraries and adapter for APC, Redis, Memcached, file systems and many others. Their PHP library was not maintained well for the last time and their advice was to use the new Symfony Cache project.

Sinds Most professional libraries are part of a larger PHP framework, it is not easy to find a good PHP cache library. But I found one …

Pagecache

This cache library may be the best choice for the initial PHP developers. Easy to install and you can adjust the required code with your existing PHP script. Even if your PHP -based website is older and written with procedural code, Pagecache is a good choice. Below is a brief summary of the most important characteristics.

  • Work outside the box with a zero configuration!
  • PSR-16 compatible cache adapter (redis or memciled).
  • The default page caching for mobile devices.

Examples of PHP Pagecache

As I said, using Pagecache is simple. Simply install the library using a composer and add the following PHP code footage at the top of your PHP script and PHP cache library will create and provide files that are -Cache.

Cache PHP Page and Popular Framework

If you start a new project, you should always consider using PHP framework such as Laravel, Symfony, Codeigniter, Yii or Zend. They offer all plugins or components of PHP Cache itself. If you use the PHP framework, you don’t need to write all the code and because the most important library has been bundled. You don’t need to choose (and test) all your own libraries.

Most PHP cache components are able to store the entire PHP pages and also provide adapter for object cache with redis or memacked.

Object caching

When caching all pages occurs on your file system, the object cache functions completely differently. Object caching is mostly used to store strings, object arrays or even set of database results. These objects are stored in RAM memory and sometimes in the database. For posting this blog I will tell you more about two storage systems that you can use as cache objects: Memcached and redis.

What is Memcached?

Memcached was originally developed in 2003 by Brad Fitzpatrick. This is quite old, in the same year we have used PHP 4 and MySQL 3.2.3. You can use Memcached with a different programming language and for PHP there are 2 extensions, one is called an object cache and the other is called memcache (pay attention to the lost “D”. Don’t use the last one, it’s not well maintained.

Memcached is very fast and can help you speed up your website while maintaining loads on your database at low levels (ER).

Example Memacked PHP

Adding and reading objects is very easy and requires less code than adding / getting the same information from the MySQL database.

Redis Object Cache

Redis is a data structure store in memory, mostly managed by Salvatore Sanfilippo and sponsored by Redis Labs. You can install phpradis extension using PECL. Currently you cannot find information about this extension through PHP documentation. That’s not really a problem, because Redis is very popular in the world of development and there is a lot of information available.

Example of PHP Redis

The function or method is very similar to the code that we use to memacked.

Which cache object you choose is up to you, both are fast and easy to use. Personally I like the structured PHP documentation for Memcached.

You understand why PHP’s library and framework uses adapter for redis or memacked. You can use the same method to store your data in a different object cache system.

Fragment cache

This cache looks somewhat like a yard caching, but is used to make a version that is used for the parts of the web page. This is very useful if some parts of the page must be dynamic. A very simple example is the text “Hello User Name” on the Portal website, where all the information is the same for all users except its name.

You can save your code fragments such as the page cache through the file system or you can cache object.

This PHP cache works differently. That does not store the resulting HTML code such as the page cache, but the PHP script code. PHP Opcache will compile a PHP code and store this version in RAM memory. In this way PHP can process PHP code faster when using less CPU power. Some important PHP opcache functions are:

This PHP cache library is one of the larger projects and is not part of several frameworks. If you need a cache for a high traffic website, phpfastcache is the library you need. You can use a library for various types of cache such as cache files, sqlite, mongodb, redis and memacked.

Example of phpfastcache code

With the following code, we get the same object as for the sample code that is dim. This is a little more code, but if you change a few values, you can use the same code for redis or other cache systems.

PHP Cache and WordPress

Like this WordPress website (or blog) I do a lot of web development work with WordPress lately. There are several adult cache plugins to make a full page cache and also some plugins for caching objects with redis or memacked. WordPress has its own object cache interface. It will say that if you connect the object cache with memacked and you store some values ​​in an object, the value is not stored in the database, but in memacked.

Here is a list of wordpress cache plugins that I often use.

WP Super Cache

If your server is based on Apache, the cache plugin this page is very fast and stable. This is managed by Automattic, a company behind WordPress. It does not offer object caching, but supports cache fragments (for advanced users).

WP Rocket

The WordPress Cache plugin is also a full page plugin, but has some good additional (cloudflare integration, user cache …) and very good interface. This plugin is not free, but in my opinion it is worth every pen. I use WP Rocket for several websites and plugins store me 1 second from loading time (compared to WP Super Cache).

Simple cache

The simple cache plugin developed by Taylor Lovett is not so popular, but has almost unique features: object cache (memcached or redis). It doesn’t have many choices, just activate the cache and you are finished.

I found a simple cache when I was looking for a plugin that played well with nginx. With a simple cache, there is only one line that you need to change in your Nginx configuration. You can use two other plugins for nginx too, but you need to change (and test) more Nginx configurations. Ask me if you want to know what I have changed in Nginx for the WP rocket.

Conclusion

Which type of php cache is best, depending on your web application, server configuration and possible server loads. PHP Caching is often not a simple task. If you like to do something simple, just use WordPress and one of the available cache plugins.

Published in: PHP script · Website development



Game Center

Game News

Review Film
Rumus Matematika
Anime Batch
Berita Terkini
Berita Terkini
Berita Terkini
Berita Terkini
review anime

Gaming Center

Kiriman serupa