Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Caching Middleware #62

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 75 additions & 0 deletions 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());
}
}
}
55 changes: 55 additions & 0 deletions 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);
}
74 changes: 74 additions & 0 deletions 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);
}
}
6 changes: 6 additions & 0 deletions 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
);
19 changes: 19 additions & 0 deletions 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.
Expand Down