Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Basic Caching Middleware #62

Closed
wants to merge 3 commits into from

2 participants

@tboronczyk

I wrote some basic caching middleware... perhaps someone may find it useful. Please consider accepting it into your repository.

@silentworks
Collaborator

Could you rename the default CacheHandler class to PDOCacheHandler? this would make it more informative. Can you also send your pull request to the develop branch instead of master.

@tboronczyk

I've made the requested change and am closing this request to issue the pull request against develop branch.

@tboronczyk tboronczyk closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
75 Middleware/Cache.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Cache
+ *
+ * Use this middleware with your Slim Framework application
+ * for basic caching abilities
+ *
+ * @author Timothy Boronczyk
+ * @version 1.0
+ *
+ * USAGE
+ *
+ * use \Slim\Slim;
+ * use \Slim\Extras\Middleware\Cache;
+ * use \Slim\Extras\Middleware\Cache\PDOCacheHandler;
+ *
+ * $db = ...
+ * $app = new Slim();
+ * $handler = new PDOCacheHandler($db);
+ * $app->add(new Cache($handler));
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+namespace Slim\Extras\Middleware;
+use \Slim\Extras\Middleware\Cache\ICacheHandler;
+
+class Cache extends \Slim\Middleware
+{
+ protected $handler;
+
+ public function __construct(ICacheHandler $handler)
+ {
+ $this->handler = $handler;
+ }
+
+ public function call()
+ {
+ $req = $this->app->request();
+ $resp = $this->app->response();
+ $handler = $this->handler;
+
+ $key = $req->getResourceUri();
+ $data = $handler->fetch($key);
+ if ($data) {
+ $resp['Content-Type'] = $data['content_type'];
+ $resp->body($data['body']);
+ return;
+ }
+
+ $this->next->call();
+
+ if ($resp->status() == 200) {
+ $handler->save($key, $resp['Content-Type'], $resp->body());
+ }
+ }
+}
View
55 Middleware/Cache/ICacheHandler.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Cache Handler Interface
+ *
+ * Implement this interface to create your own cache handler.
+ *
+ * @author Timothy Boronczyk
+ * @version 1.0
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+namespace Slim\Extras\Middleware\Cache;
+
+Interface ICacheHandler
+{
+ /**
+ * Retrieve content from cache.
+ * Looks up content in the cache for the given key and returns either
+ * an array with the content or false if the content is not found.
+ *
+ * @param string $key the cache key
+ * @return array|false array with the elements content_type and body on
+ * success, false if content not found
+ */
+ public function fetch($key);
+
+ /**
+ * Store content in the cache.
+ * Persists content in the cache storeage for later lookup.
+ *
+ * @param string $key the cache key
+ * @param string $contenType the content's Content-Type
+ * @param string $body the content
+ */
+ public function save($key, $contentType, $body);
+}
View
74 Middleware/Cache/PDOCacheHandler.php
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Example Cache Handler
+ *
+ * This example implementation uses a PDO connection to a MySQL database and
+ * the table structure included in cache.sql. You can write your own handler
+ * by implementing the ICacheHandler interface.
+ *
+ * @author Timothy Boronczyk
+ * @version 1.0
+ *
+ * MIT LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+namespace Slim\Extras\Middleware\Cache;
+
+class PDOCacheHandler implements ICacheHandler
+{
+ protected $db;
+ protected $ttl;
+
+ public function __construct(\PDO $db, $ttl = 300)
+ {
+ $this->db = $db;
+ $this->ttl = $ttl;
+ }
+
+ public function fetch($key)
+ {
+ $query = sprintf(
+ 'SELECT `content_type`, `body` FROM `cache`
+ WHERE `key` = %s AND `tstamp` > %d',
+ $this->db->quote(md5($key)),
+ time() - $this->ttl
+ );
+ $result = $this->db->query($query);
+ $row = $result->fetch(\PDO::FETCH_ASSOC);
+ $result->closeCursor();
+ return $row;
+ }
+
+ public function save($key, $contentType, $body)
+ {
+ $query = sprintf(
+ 'INSERT INTO `cache` (`key`, `content_type`, `body`, `tstamp`)
+ VALUES (%s, %s, %s, %d)
+ ON DUPLICATE KEY UPDATE
+ `content_type` = %2$s, `body` = %3$s, `tstamp` = %4$d',
+ $this->db->quote(md5($key)),
+ $this->db->quote($contentType),
+ $this->db->quote($body),
+ time()
+ );
+ $this->db->query($query);
+ }
+}
View
6 Middleware/Cache/cache.sql
@@ -0,0 +1,6 @@
+CREATE TABLE `cache` (
+ `key` CHAR(40) NOT NULL PRIMARY KEY,
+ `content_type` VARCHAR(100) NOT NULL,
+ `body` TEXT NOT NULL,
+ `tstamp` INTEGER UNSIGNED NOT NULL
+);
View
19 Middleware/README.markdown
@@ -1,5 +1,24 @@
# Slim Authentication and XSS Middlewares
+## Cache
+
+Provides basic Caching functionality.
+
+### How to use
+
+ use \Slim\Slim;
+ use \Slim\Extras\Middleware\Cache;
+ use \Slim\Extras\Middleware\Cache\PDOCacheHandler;
+
+ $db = ...
+ $app = new Slim();
+ $handler = new PDOCacheHandler($db);
+ $app->add(new Cache($handler));
+
+The example implementation `PDOCacheHandler` uses a PDO connection to a MySQL
+database and the table structure included in `Cache/cache.sql`. You can write
+your own handler by implementing the `ICacheHandler` interface.
+
## CsrfGuard
This is used to protect your website from CSRF attacks.
Something went wrong with that request. Please try again.