Types of caching in a PHP web application

Posted on Tuesday, October 06, 2020 @ 20:07:50 UTC in Tutorials
by neralex

southern writes:  

As a web developer, you must have heard the term "cache" in various situations. To help you fully understand what cache means, we wrote this article.

Table Of Content

    OpCode Cache
    Application Cache
    HTTP Cache
    Proxy Cache
    End

OpCode Cache

When running a PHP script. The interpreter will parse the script into a series of operation codes, commonly known as "opcodes".

By caching the opcode in memory, we can gain significant performance improvement. This is known as opCode cache.

There is a list of well-know opCode cache engines and they are:

    Zend OpCache
    APC User Cache
    Alternative PHP Cache
    Xcache

By default, PHP ships with Zend OpCache.

Application Cache

When building an application, we can utilize some cache strategies to avoid slow response:

    Store data that is not changed often in a cache.
    Store time-consuming computed data in a cache.

Above are known as application cache.

When implementing the application cache, we have a lot of options in where to store the cache values. Following solutions are typically used to store the cache values:

    Redis
    Memcached
    Database
    File system

HTTP Cache

Most of our PHP applications are built for the web and accessed by a browser. The browser interacts with our application via HTTP protocol.

By utilizing the HTTP cache headers, a browser stores the response locally in its cache, this saves it from requesting the same data again via the network. This is known as HTTP cache.

Commonly used HTTP Cache headers are:

    Cache-control
    Pragma
    Expires
    Etag
    Last-Modified

Proxy Cache

OpCode cache, application cache, and HTTP cache are great to help with application speed improvement. However, all the requests still go through the webserver. When the concurrency of the application is high, the web server may have a difficult time handling the requests.

By placing a proxy server in front of the webserver, we can not only significantly reduce the web server's load, but also we can improve the speed greatly because proxy server stores the cached response inside its memory.

The strategy above is known as a proxy cache.

Well-known proxy cache servers are:

    Nginx
    Squid
    Varnish

End

We hope you have understood different cache means through this article.

Star Tutorial