Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Tonic/MetadataCacheInclude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tonic;

/**
* Cache resource metadata between invocations
*
* This class exports the resource metadata and writes it to disk for including in a later request.
*/
class MetadataCacheInclude implements MetadataCache
{
private $filename;

public function __construct($filename)
{
$this->filename = $filename;
}

/**
* Is there already cache file
* @return boolean
*/
public function isCached()
{
return is_readable($this->filename);
}

/**
* Load the resource metadata
* @return str[]
*/
public function load()
{
return include $this->filename;
}

/**
* Save resource metadata to disk
* @param str[] $resources Resource metadata
* @return boolean
*/
public function save($resources)
{
return file_put_contents($this->filename, '<?php return ' . var_export($resources, true) . ';');
}

public function clear()
{
@unlink($this->filename);
}

public function __toString()
{
return 'Metadata for '.count($this->load()).' resources stored in file "'.$this->filename.'" at '.date('r', filemtime($this->filename));
}

}
1 change: 1 addition & 0 deletions web/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#'mount' => array('Tyrell' => '/nexus'), // mount in example resources at URL /nexus
#'cache' => new Tonic\MetadataCacheFile('/tmp/tonic.cache') // use the metadata cache
#'cache' => new Tonic\MetadataCacheAPC // use the metadata cache
#'cache' => new Tonic\MetadataCacheInclude('/tmp/tonic-cache.php') // use the metadata cache
);

$app = new Tonic\Application($config);
Expand Down