Background
[wp_ad_camp_5]
Most of the PHP Frameworks I worked on or experimented with have their own ways of caching things to reduce calls to other systems (e.g., RDBMS) or interfaces to plug in third-party caching libraries. But what if you had an application that did not use any known framework? There are simple-to-use caching libraries that can be easily integrated into your existing project.
Software Requirements
- Windows 7 Professional SP1
- PHP 5.5.9 / Zend Engine v2.5.0
- NetBeans IDE 8.0.1
- ApacheFriends XAMPP Version 1.8.3
- Apache/2.4.7 (Win32) / Open SSL/1.0.1e
Download phpfastcache
You may download it directly from https://github.com/Turreta/phpfastcache. This is a fork of https://github.com/khoaofgod/phpfastcache. Unzip the file and copy the phpfastcache directory into your application in Apache web server.
For instance, phpfastcachetest is an application for this post.
[wp_ad_camp_4]
The Codes
The contents of mytest.php are as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | include("phpfastcache/phpfastcache.php"); $config = array( "storage" => "files", "path" => "C:/xampp/htdocs/phpfastcachetest/phpfastcache/cache" , "securityKey" => "5tQzKg4O6XAPAOkt2tIi", "fallback" => array(), "htaccess" => true, "server" => array(), ); phpFastCache::setup($config); $cache = new phpFastCache(); $products = $cache->my_products; if($products == null) { echo 'doing db call' . "\n"; $products = ['a', 'b', 'c', ]; $cache->my_products = array($products, 600); } else { echo 'not doing db call' . "\n"; } print_r($products); |
Test It
Note that the test is via a web browser. Simply use the URL below:
1 | http://localhost/phpfastcachetest/mytest.php |
Upon display, it displays:
1 | doing db call Array ( [0] => a [1] => b [2] => c ) |
On subsequent page display:
1 | not doing db call Array ( [0] => a [1] => b [2] => c ) |
The data is cached to a directory in the file system.
[wp_ad_camp_3]