2014-10-20 10:57:35 +02:00
|
|
|
title: Flushable
|
|
|
|
summary: Allows a class to define it's own flush functionality.
|
|
|
|
|
2014-08-16 02:51:17 +02:00
|
|
|
# Flushable
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
Allows a class to define it's own flush functionality, which is triggered when `flush=1` is requested in the URL.
|
2017-07-03 03:22:12 +02:00
|
|
|
[FlushMiddleware](api:SilverStripe\Control\Middleware\FlushMiddleware) is run before a request is made, calling `flush()` statically on all
|
|
|
|
implementors of [Flushable](api:SilverStripe\Core\Flushable).
|
2014-08-16 02:51:17 +02:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2017-07-03 03:22:12 +02:00
|
|
|
To use this API, you need to make your class implement [Flushable](api:SilverStripe\Core\Flushable), and define a `flush()` static function,
|
2014-08-16 02:51:17 +02:00
|
|
|
this defines the actions that need to be executed on a flush request.
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
### Using with Cache
|
2014-08-16 02:51:17 +02:00
|
|
|
|
2017-07-03 03:22:12 +02:00
|
|
|
This example uses [Cache](api:Cache) in some custom code, and the same cache is cleaned on flush:
|
2014-08-16 02:51:17 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
|
|
|
|
```php
|
2017-08-03 05:57:29 +02:00
|
|
|
class MyClass extends DataObject implements Flushable
|
|
|
|
{
|
2014-08-16 02:51:17 +02:00
|
|
|
|
2017-08-03 06:25:49 +02:00
|
|
|
public static function flush()
|
|
|
|
{
|
2016-09-09 08:43:05 +02:00
|
|
|
Cache::factory('mycache')->clean(Zend_Cache::CLEANING_MODE_ALL);
|
2014-08-16 02:51:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 06:25:49 +02:00
|
|
|
public function MyCachedContent()
|
|
|
|
{
|
2016-09-09 08:43:05 +02:00
|
|
|
$cache = Cache::factory('mycache')
|
2014-08-26 04:55:28 +02:00
|
|
|
$something = $cache->load('mykey');
|
2014-08-16 02:51:17 +02:00
|
|
|
if(!$something) {
|
|
|
|
$something = 'value to be cached';
|
2015-02-09 10:55:59 +01:00
|
|
|
$cache->save($something, 'mykey');
|
2014-08-16 02:51:17 +02:00
|
|
|
}
|
|
|
|
return $something;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2014-08-16 02:51:17 +02:00
|
|
|
|
|
|
|
### 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.
|
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```php
|
2017-08-03 05:57:29 +02:00
|
|
|
class MyClass extends DataObject implements Flushable
|
|
|
|
{
|
2014-08-16 02:51:17 +02:00
|
|
|
|
2017-08-03 06:25:49 +02:00
|
|
|
public static function flush()
|
|
|
|
{
|
2014-08-16 02:51:17 +02:00
|
|
|
foreach(glob(ASSETS_PATH . '/_tempfiles/*.jpg') as $file) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|