Version 6 pre-stable
This version of Silverstripe CMS has not yet been given a stable release. See the release roadmap for more information. Go to documentation for the most recent stable version.

Flushable

Introduction

Allows a class to define it's own flush functionality, which is triggered when flushing the kernel by calling flush() statically on all implementors of Flushable.

Flushable implementers might also be triggered automatically on deploy if you have SS_FLUSH_ON_DEPLOY environment variable defined. In that case even if you don't manually flush the kernel, the first request after deploy will still be calling flush() on those entities.

Usage

To use this API, you need to make your class implement Flushable, and define a flush() static function, this defines the actions that need to be executed on a flush request.

Using with cache

This example uses Cache in some custom code, and the same cache is cleaned on flush:

namespace App\Model;

use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;

class MyClass extends DataObject implements Flushable
{
    public static function flush()
    {
        Injector::inst()->get(CacheInterface::class . '.mycache')->clear();
    }

    public function getMyCachedContent()
    {
        $cache = Injector::inst()->get(CacheInterface::class . '.mycache')
        $something = $cache->get('mykey');
        if (!$something) {
            $something = 'value to be cached';
            $cache->set('mykey', $something);
        }
        return $something;
    }
}

Using with filesystem

Another example, some temporary files are created in a directory in assets, and are deleted on flush. This would be useful in an example like GD or Imagick generating resampled images, but we want to delete any cached images on flush so they are re-created on demand.

namespace App\Model;

use SilverStripe\Core\Flushable;
use SilverStripe\ORM\DataObject;

class MyClass extends DataObject implements Flushable
{
    public static function flush()
    {
        foreach (glob(ASSETS_PATH . '/_tempfiles/*.jpg') as $file) {
            unlink($file);
        }
    }
}