mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-06 17:51:34 +02:00
* First cut * Temporarily disable composer.json for netlify build * POC * New recursive directory query, various refinements * Fix flexbox * new styled components plugin * Apply frontmatter delimiters * Mobile styles, animation * Search * Redesign, clean up * Nuke the cache, try again * fix file casing * Remove production env file * ID headers * Move app to new repo * Add frontmatter universally * Hide children changelogs * Add how to title * New callout tags * Revert inline code block change * Replace note callouts * Fix icons * Repalce images * Fix icon * Fix image links * Use proper SQL icon
82 lines
2.3 KiB
Markdown
82 lines
2.3 KiB
Markdown
---
|
|
title: Flushable
|
|
summary: Allows a class to define it's own flush functionality.
|
|
---
|
|
|
|
# Flushable
|
|
|
|
## Introduction
|
|
|
|
Allows a class to define it's own flush functionality, which is triggered when `flush=1` is requested in the URL.
|
|
[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).
|
|
|
|
|
|
[notice]
|
|
Flushable implementers might also be triggered automatically on deploy if you have `SS_FLUSH_ON_DEPLOY` [environment
|
|
variable](../configuration/environment_variables) defined. In that case even if you don't manually pass `flush=1` parameter, the first request after deploy
|
|
will still be calling `Flushable::flush` on those entities.
|
|
[/notice]
|
|
|
|
|
|
## Usage
|
|
|
|
To use this API, you need to make your class implement [Flushable](api:SilverStripe\Core\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](api:Cache) in some custom code, and the same cache is cleaned on flush:
|
|
|
|
|
|
```php
|
|
use SilverStripe\ORM\DataObject;
|
|
use SilverStripe\Core\Injector\Injector;
|
|
use SilverStripe\Core\Flushable;
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
class MyClass extends DataObject implements Flushable
|
|
{
|
|
|
|
public static function flush()
|
|
{
|
|
Injector::inst()->get(CacheInterface::class . '.mycache')->clear();
|
|
}
|
|
|
|
public function MyCachedContent()
|
|
{
|
|
$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.
|
|
|
|
```php
|
|
use SilverStripe\ORM\DataObject;
|
|
use SilverStripe\Core\Flushable;
|
|
|
|
class MyClass extends DataObject implements Flushable
|
|
{
|
|
|
|
public static function flush()
|
|
{
|
|
foreach(glob(ASSETS_PATH . '/_tempfiles/*.jpg') as $file) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
|
|
}
|
|
```
|