Skip to content

Core\Cache

Fariz Luqman edited this page Jan 22, 2017 · 4 revisions

The Core\Cache is a caching server connector and a facade for phpFastCache\CacheManager.

phpFastCache is a high-performance, distributed object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

phpFastCache dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.

How to Use the CacheManager

Say that you have a set of products queried from the database, you can cache them like this:

$products = Model\Product::all();
$app->cachemanager->set('products', $products,600));  // 0 = never expired;

The first argument to the set method is the array 'key', while the second one is the array, and the third one is the time to cache (in seconds).

To read the cache, you can do this:

$products = $app->cachemanager->get('products');

//if $products not found in the cache or cache miss
if(is_null($products)) {
    $products = Model\Product::all();
    $app->cachemanager->set('products', $products,600));  // 0 = never expired;
}

Configure the Cache Manager

The config\cache.php contains all the configuration need to connect to the caching server.

return 
[
  'enabled'   => false,
  'settings'  =>  
  [
    /*
     * Default storage
     * if you set this storage => 'files', then $cache = phpFastCache(); <-- will be files cache
     */
    'storage'   =>  'auto', // ssdb, predis, redis, mongodb , files, sqlite, auto, apc, wincache, xcache, memcache, memcached,

    /*
     * Default Path for Cache on HDD
     * Use full PATH like /home/username/cache
     * Keep it blank '', it will automatic setup for you
     */
    'path'      =>  '' , // default path for files
    'securityKey'   =>  '', // default will good. It will create a path by PATH/securityKey

    /*
     * FallBack Driver
     * Example, in your code, you use memcached, apc..etc, but when you moved your web hosting
     * Until you setup your new server caching, use 'overwrite' => 'files'
     */
    'overwrite'  =>  'files', // whatever caching will change to 'files' and you don't need to change ur code

    /*
     * .htaccess protect
     * default will be  true
     */
    'htaccess'  =>  true,

    'server'        =>  
		[
            array('127.0.0.1',11211,1),
    ],

    'memcache' => 
		[
            array('127.0.0.1', 11211, 1),
    ],
		
    'redis' => 
		[
            'host' => '127.0.0.1',
            'port' => '6379',
            'password' => '',
            'database' => '',
            'timeout' => '',
    ],
		
    'ssdb' => 
		[
            'host' => '127.0.0.1',
            'port' => 8888,
            'password' => '',
            'timeout' => '',
    ],
		
    // use 1 as normal traditional, 2 = phpfastcache fastest as default, 3 = phpfastcache memory stable
    'caching_method' => 2,
  ]
];

Work in progress. This section will be expanded from time to time.

Visit our website for tutorials: stupidlysimple.github.io

Clone this wiki locally