Using cache will be very easy in Windwalker, the cache configuration is located at /etc/config.yml.

    enabled: 0
    storage: file
    serializer: php
    time: 15

The enabled property controls global cache start or not, can be close if you want to test your application.


Set stoeage so we can use different storage engine to save cache data, available storages:

  • file --> Basic file storage
  • php_file --> Will use include to load file.
  • forever_file --> Never expired file.
  • array --> Use array to store data.
  • runtime_array --> Use static array to store data in one life cycle
  • memcached
  • redis
  • null

See Cache Storage


The default php_serializer will make our data be php serialized string, if you want to use other format, just change serializer option.

  • php --> Use php serialize()
  • php_file --> Save as array and return data so we can include it.
  • json --> Json encode data
  • string --> Will force convert object to string.
  • raw --> Just save raw string

Use Global Cache

Windwalker contains a global main cache object that you can configure it in config.yml.

$cache = Ioc::getCache();

// OR

$cache = $container->get('cache');

// Store cache item
$cache->set('key', 'value');

// Get cache
$value = $cache->get('key');

// Check cache item

Custom Cache Object

We can still create our own custom cache objects:

$cacheManager = $container->get('cache.manager');

$cache = $cacheManager->getCache('mycache_name', 'file'); // Every name will be singleton object.

// OR use Facade

use Windwalker\Core\Cache\CacheFactory;

CacheFactory::getCache('mycache_name', 'file');

If you set cache.enabled to false or in DEBUG mode, all cache created from getCache() will be NullStorage, it won't cache any data.

You can set ignore global to make cache manager ignore config settings.


Disable Cache or Debug

When debug property in global config se to true or cache.disabled set to true, the cache storage will auto set to NullStorage, cache can still be used but no work.

// If cache disabled
$cache = \Windwalker\Ioc::getCache();

// Cache object can be get and operate, but the key will always not exists.
if (!$cache->exists('flower'))
    $data = $flowerMapper->loadAll();

    $cache->set('flower', $data);
    $data = $cache->get('flower');

return $data;


Use CacheFactory::getCache()

CacheFactory is a cache creator, it will store each cache object as singleton by different name and options.

use Windwalker\Core\Cache\CacheFactory;

$myFileCache = CacheFactory::getCache('cache_name', 'file');
$myArrayCache = CacheFactory::getCache('cache_name', 'array');

The default cache is array cache, it means our data only keep in array but will not save as files.

Custom Cache Options

$options = array(
    'cache_dir' => WINDWALKLER_CACHE, // Only for file storage
    'cache_time' => 999 // minutes

$cache = CacheFactory::getCache('cache_name', 'file', 'php', $options);


ArrayStorage and RuntimeArrayStorage

This is default storage, which will store data in itself and will not depends on any outside storage engine.

The RuntimeArrayStorage use static property to storage data, which means all data will live in current runtime no matter how many times you create it.

$cache = CacheFactory::getCache('my_cache', 'array');
$cache = CacheFactory::getCache('my_cache', 'runtime_array');


Create a cache with FileStorage and set a path to store files.

$cache = CacheFactory::getCache('my_cache', 'file');

$cache->set('flower', array('sakura'));

The file will store at {ROOT}/cache/my_cache/, and the data will be serialized string.


File Group

Group is a subfolder of your storage path.

$cache = CacheFactory::getCache('mygroup', 'file');

$cache->set('mygroup', array('sakura'));

The file wil store at {ROOT}/cache/mygroup/ that for organize your cache folder.

PHP File Format and Deny Access

If your cache folder are exposure on web environment, we have to make our cache files unable to access. The argument 3 of FileStorage is use to deny access.

$cache = CacheFactory::getCache('mygroup', 'file', 'php', ['deny_access' => true]);

$cache->set('flower', array('sakura'));

The stored file will be a PHP file with code to deny access:


<?php die("Access Deny"); ?>a:1:{i:0;s:6:"sakura";}

Available Storages

  • ArrayStorage (array)
  • RuntimeArrayStorage (runtime_array)
  • FileStorage (file)
  • PhpFileStorage (php_file)
  • ForeverFileStorage (forever_file)
  • MemcachedStorage (memcached)
  • RedisStorage (redis)
  • NullStorage (null)


The default PhpSerializer (php) will make our data be php serialized string, if you want to use other format, just change serializer at second argument of Cache object.

$cache = CacheFactory::getCache('my_cache', 'file', 'json');

$cache->set('flower', array('flower' => 'sakura'));

The stored cache file is:


Full Page Cache

Sometimes we may need to store whole html as static page cache. StringSerializer (string) or RawSerializer (raw) helps us save raw data as string:

$cache = CacheFactory::getCache('my_cache', 'file', 'string');

$url = '';

if ($cache->exists($url))
    echo $cache->get($url);


$html = $view->render('html.layout');

$cache->set($url, $html);

echo $html;


This serializer can save array data as a php file, will be useful when we need to cache config data.

$cache = CacheFactory::getCache('my_cache', 'file', 'php_file');

$config = array('foo' => 'bar');

$cache->set('', $config);

$cache->get(''); // Array( [foo] => bar )

The cache file will be:


return array (
  'foo' => 'bar',

Available Serializers

  • PhpSerializer (php)
  • PhpFileSerializer (php_file)
  • JsonSerializer (json)
  • StringSerializer (string)
  • RawSerializer (raw)

PSR6 Cache Interface

Windwalker Cache Storage are all follows PSR6, so you can use other libraries' CacheItemPool object as storage, you can also directly use Storage object.

use Windwalker\Cache\Item\CacheItem;
use Windwalker\Cache\Storage\FileStorage;

$cachePool = new FileStorage(__DIR__ . '/cache');

$cachePool->save(new CacheItem('foo', 'Bar', 150));

// OR save differed
$cachePool->saveDeferred(new CacheItem('baz', 'Yoo', 150));

See Windwalker Cache Package

If you found a typo or error, please help us improve this document.