Permalink
Browse files

Metadata cache powered by APC

File cache extracted and cache interface added
  • Loading branch information...
peej committed Jul 31, 2012
1 parent 94cb5cd commit a2dfc0efe837d620fac37d977ce5517577d0e57d
Showing with 122 additions and 27 deletions.
  1. +10 −26 src/Tonic/MetadataCache.php
  2. +53 −0 src/Tonic/MetadataCacheAPC.php
  3. +57 −0 src/Tonic/MetadataCacheFile.php
  4. +2 −1 web/dispatch.php
@@ -4,49 +4,33 @@
/**
* Cache resource metadata between invocations
- *
- * This class serializes the resource metadata and writes it to disk for reading in a later request.
*/
-class MetadataCache
+interface 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);
- }
+ public function isCached();
/**
* Load the resource metadata from disk
* @return str[]
*/
- public function load()
- {
- return unserialize(file_get_contents($this->filename));
- }
+ public function load();
/**
* Save resource metadata to disk
* @param str[] $resources Resource metadata
* @return boolean
*/
- public function save($resources)
- {
- return file_put_contents($this->filename, serialize($resources));
- }
+ public function save($resources);
+
+ /**
+ * Clear the cache
+ */
+ public function clear();
- public function __toString()
- {
- return $this->filename;
- }
+ public function __toString();
}
@@ -0,0 +1,53 @@
+<?php
+
+namespace Tonic;
+
+/**
+ * Cache resource metadata between invocations
+ *
+ * This class writes the resource metadata to APC for reading in a later request.
+ */
+class MetadataCacheAPC implements MetadataCache
+{
+ const CACHENAME = 'tonicCache';
+
+ /**
+ * Is there already cache file
+ * @return boolean
+ */
+ public function isCached()
+ {
+ return apc_exists(self::CACHENAME);
+ }
+
+ /**
+ * Load the resource metadata from disk
+ * @return str[]
+ */
+ public function load()
+ {
+ return apc_fetch(self::CACHENAME);
+ }
+
+ /**
+ * Save resource metadata to disk
+ * @param str[] $resources Resource metadata
+ * @return boolean
+ */
+ public function save($resources)
+ {
+ return apc_store(self::CACHENAME, $resources);
+ }
+
+ public function clear()
+ {
+ apc_delete(self::CACHENAME);
+ }
+
+ public function __toString()
+ {
+ $info = apc_cache_info('user');
+ return 'Metadata for '.count($this->load()).' resources stored in APC at '.date('r', $info['cache_list'][0]['creation_time']);
+ }
+
+}
@@ -0,0 +1,57 @@
+<?php
+
+namespace Tonic;
+
+/**
+ * Cache resource metadata between invocations
+ *
+ * This class serializes the resource metadata and writes it to disk for reading in a later request.
+ */
+class MetadataCacheFile 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 from disk
+ * @return str[]
+ */
+ public function load()
+ {
+ return unserialize(file_get_contents($this->filename));
+ }
+
+ /**
+ * Save resource metadata to disk
+ * @param str[] $resources Resource metadata
+ * @return boolean
+ */
+ public function save($resources)
+ {
+ return file_put_contents($this->filename, serialize($resources));
+ }
+
+ 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));
+ }
+
+}
View
@@ -6,7 +6,8 @@
$config = array(
'load' => array('../*.php', '../src/Tyrell/*.php'), // load example resources
#'mount' => array('Tyrell' => '/nexus'), // mount in example resources at URL /nexus
- #'cache' => new Tonic\MetadataCache('/tmp/tonic.cache') // use the metadata cache
+ #'cache' => new Tonic\MetadataCacheFile('/tmp/tonic.cache') // use the metadata cache
+ #'cache' => new Tonic\MetadataCacheAPC // use the metadata cache
);
$app = new Tonic\Application($config);

0 comments on commit a2dfc0e

Please sign in to comment.