Zend framework cache

White Paper:

A Practical Guide to Data Caching with Zend Server
By Shahar Evron, Product Manager, Zend Technologies, Inc.

Technical

April 2009

© 2009 Zend Technologies, Inc. All rights reserved.

White Paper | A Practical Guide to Data Caching with Zend Server

Table of Contents
Introduction Ok, how do I get started? But wait… How do I decide what to cache, and how? 3 4 6 6 66 7 8 10 11 11

What do I cache? How do I cache?
How long do I need to cache for? Programmatic Cache Invalidation Cache Namespacing Summary Where Next?

© Zend Technologies, Inc.

2

White Paper | A Practical Guide to Data Caching with Zend Server

Introduction
When asked about the most significant methods to speed up PHP applications, most experts would say “Cache, Cache and Cache”.There are a lot of optimizations that could be done to PHP code that would make it run faster, but the most effective method to speed up PHP execution is to simply execute less, and one of the best ways to achieve that is through caching. Zend Server provides several caching-related features, including Optimizer+ for byte-code caching, full-page caching using Zend Page Cache, and the Data CacheAPI. To avoid confusion, let’s go over them quickly: • Zend Optimizer+ performs byte-code optimization and caching. This speeds up PHP applications by eliminating the process of reading scripts from disk and compiling them, often resulting in a speedup of x2 – x3 times, depending on the application. Zend Optimizer+ runs automatically, and installing your application on top of Zend Server is all youneed to do in order to enjoy its benefits. Zend Page Cache allows caching of entire PHP web pages, or more correctly put, entire HTTP responses sent out from PHP. Page Caching permits dramatically improving the performance of pages on web applications (sometimes running as high as 10x, 20x and even 100x times faster), while maintaining dynamic capabilities through an elaborate system of cachingrules that could be based on request parameters and user session data. Page Caching also has the benefit of not requiring any code changes, and can be set up from the Zend Server UI. Zend Data Cache is a set of API functions enabling a developer to store and manage data items (PHP strings, arrays and other data) and even output elements in either disk-based cache or shared memory cache. Zend DataCache allows for precisionguided caching when Page Caching is not an option. The provided API is easy-to-use on existing code, and in many cases a developer can skip existing code sections by simply wrapping them with caching APIs.

This whitepaper will demonstrate how to use the Zend Data Cache API in order to speed up a typical PHP application.

© Zend Technologies, Inc.

3 White Paper | A Practical Guide to Data Caching with Zend Server

Ok, how do I get started?
Getting started with Zend Data Cache is extremely easy: The first functions you need to know about are zend_shm_cache_fetch() and zend_shm_cache_store(). These two functions are used for storing data in the shared-memory cache, and for fetching data from the cache. Note: Zend Data Cache offers two storagemodules: The shared memory (“shm”) module, and the disk module. We will revisit the differences later on, but for now, you should know that all functions mentioned here with the ‘zend_shm’ prefix are used for shared memory storage. For each function mentioned, there is an exact twin with the ‘zend_disk’ prefix that preforms the same actions for the disk-based cache. For the two functions mentionedabove, there are the zend_disk_cache_store() and zend_disk_cache_fetch() twin functions. For convenience, we will only mention the shared memory functions here – but keep in mind that you can (and should, in some situations) use the disk-based cache functions as well. Since code speaks louder than words, consider the following piece of code taken from an imaginary but typical blogging platform:…