Skip to content

Commit

Permalink
More SqliteCache
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Dec 3, 2008
1 parent 36bd882 commit 9e743b8
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 63 deletions.
10 changes: 10 additions & 0 deletions apps/blog/BlogApplication.class.php
Expand Up @@ -17,5 +17,15 @@ public function __construct() {
$this->routingPrefix = 'blog/';

}

public static function __set_state($array) {
$app = new BlogApplication();
$app->name = $array['name'];
$app->controllersPrefix = $array['controllersPrefix'];
$app->modelsPrefix = $array['modelsPrefix'];
$app->viewsDir = $array['viewsDir'];
$app->routingPrefix = $array['routingPrefix'];
return $app;
}
}
?>
2 changes: 1 addition & 1 deletion apps/blog/controllers/PostsController.class.php
Expand Up @@ -7,7 +7,7 @@ class PostsController extends Controller {

protected $formats = array(Formats::xhtml, Formats::json);

/** !Route GET, /blog/ */
/** !Route GET */
function home() {

$this->latestPosts = Make::a('Post')->find()->orderBy('id DESC')->range(0,5);
Expand Down
9 changes: 6 additions & 3 deletions apps/blog/views/home/comments.php
@@ -1,10 +1,12 @@
<?php
$title = 'Comments';
include_once($viewsDir . 'common/header.php'); ?>

<form method="POST">
<input type="text" name="comment"></input>
<input type="submit"></input>
</form>



<?php

echo '<a href="/blog/">Go Back</a>';
Expand All @@ -18,4 +20,5 @@

}

?>
?>
<?php include_once($viewsDir . 'common/footer.php'); ?>
12 changes: 5 additions & 7 deletions apps/blog/views/home/home.php
@@ -1,7 +1,7 @@
<html>
<head></head>
<body>
<h1>My Blog</h1>
<?php
$title = 'Home';
include_once($viewsDir . 'common/header.php');
?>
<a href="./new">Write New Post</a>
<?php

Expand All @@ -14,7 +14,5 @@
echo '<p><a href="comments/' . $post->id . '">' . $post->comments()->count() . ' Comments</a></p>';

}

?>
</body>
</html>
<?php include_once($viewsDir . 'common/footer.php'); ?>
4 changes: 2 additions & 2 deletions apps/blog/views/home/newPost.php
@@ -1,4 +1,4 @@
<?php include_once('header.php'); ?>
<?php include_once($viewsDir . 'common/header.php'); ?>

<h1>Create New Post</h1>

Expand All @@ -10,4 +10,4 @@
</table>
</form>

<?php include_once('footer.php'); ?>
<?php include_once($viewsDir . 'common/footer.php'); ?>
15 changes: 14 additions & 1 deletion blueprint.html
Expand Up @@ -7,15 +7,20 @@
<![endif]-->
<link rel="stylesheet" href="/content/css/recess.css" />

<link type="text/css" rel="stylesheet" href="/content/css/SyntaxHighlighter.css"></link>
<link type="text/css" rel="stylesheet" href="/content/css/SyntaxHighlighter.css" />
<script language="javascript" src="/content/js/shCore.js"></script>
<script language="javascript" src="/content/js/shBrushPhp.js"></script>
<script language="javascript">
window.onload = function() {
dp.SyntaxHighlighter.ClipboardSwf = '/content/flash/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
}

addField = function() {
$("modelForm").append('<p>Hello</p>');
}
</script>
<script type="text/javascript" src="/content/js/jquery/jquery-1.2.6.js"></script>
</head>
<body>
<div class="container">
Expand Down Expand Up @@ -51,6 +56,14 @@ <h3>Quick Links</h3>
</div>
<div class="span-16 last">
<h3>Package: <a href="">recess.lang</a></h3>
<form id="modelForm">
<div class="an-input"><label for="field[0]">Field:</label><input type="text" name="field[0]" id="field[0]" /></div>
</form>
<a href="javascript:addField()">Add Field</a>




<h2>RecessObject <span class="quiet">extends <a href="">StdClass</a></span></h2>
<pre name="code" class="php">&lt;?php
class RecessObject extends StdClass {
Expand Down
9 changes: 5 additions & 4 deletions recess-config.php
Expand Up @@ -16,12 +16,13 @@
);

Config::$cacheProviders
= array( // 'Apc',
// 'Memcache'
// 'Disk'
= array(
// 'Apc',
// 'Memcache',
'Sqlite',
);

Config::$useTurboSpeed = false; // I wanna go FAST! (Note: Experimental feature.)
Config::$useTurboSpeed = true; // I wanna go FAST! (Note: Experimental feature.)

Config::$applications
= array( 'recess.apps.tools.RecessToolsApplication',
Expand Down
11 changes: 11 additions & 0 deletions recess/lib/recess/apps/tools/RecessToolsApplication.class.php
Expand Up @@ -22,6 +22,17 @@ public function __construct() {
$this->routingPrefix = 'recess/';

}


public static function __set_state($array) {
$app = new RecessToolsApplication();
$app->name = $array['name'];
$app->controllersPrefix = $array['controllersPrefix'];
$app->modelsPrefix = $array['modelsPrefix'];
$app->viewsDir = $array['viewsDir'];
$app->routingPrefix = $array['routingPrefix'];
return $app;
}
}

?>
Expand Up @@ -103,7 +103,7 @@ function packageInfo ($package) {
}


/** !Route GET, model/$fullyQualifiedModel/create */
/** !Route GET, class/$fullyQualifiedModel/create */
function createTable ($fullyQualifiedModel) {
if(!Library::classExists($fullyQualifiedModel)) {
return new NotFoundResponse($this->request);
Expand Down
84 changes: 84 additions & 0 deletions recess/lib/recess/cache/Cache.class.php
Expand Up @@ -110,4 +110,88 @@ function clear() {
$this->reportsTo->clear();
}
}

class SqliteCacheProvider implements ICacheProvider {
protected $reportsTo;
protected $pdo;
protected $setStatement;
protected $getStatement;
protected $deleteStatement;
protected $time;

const VALUE = 0;
const EXPIRE = 1;

function __construct() {
$this->pdo = new Pdo('sqlite:' . $_ENV['dir.temp'] . 'sqlite-cache.db');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$this->setStatement = $this->pdo->prepare('INSERT OR REPLACE INTO cache (key,value,expire) values (:key,:value,:expire)');
$this->getStatement = $this->pdo->prepare('SELECT value,expire FROM cache WHERE key = :key');
} catch(PDOException $e) {
$this->pdo->exec('CREATE TABLE "cache" ("key" TEXT PRIMARY KEY NOT NULL , "value" TEXT NOT NULL , "expire" INTEGER NOT NULL)');
$this->pdo->exec('CREATE INDEX "expiration" ON "cache" ("expire" ASC)');
$this->setStatement = $this->pdo->prepare('INSERT OR REPLACE INTO cache (key,value,expire) values (:key,:value,:expire)');
$this->getStatement = $this->pdo->prepare('SELECT value,expire FROM cache WHERE key = :key');
}
$this->time = time();
}

function reportsTo(ICacheProvider $cache) {
if(!$cache instanceof ICacheProvider) {
$cache = new NoOpCacheProvider();
}

if(isset($this->reportsTo)) {
$temp = $this->reportsTo;
$this->reportsTo = $cache;
$this->reportsTo->reportsTo($temp);
} else {
$this->reportsTo = $cache;
}
}

function set($key, $value, $duration = 0) {
$this->setStatement->execute(array(':key' => $key, ':value' => var_export($value, true), ':expire' => $duration == 0 ? 0 : time() + $duration));
$this->reportsTo->set($key, $value, $duration);
}

function clearStaleEntries() {
$this->pdo->exec('DELETE FROM cache WHERE expire != 0 AND expire < ' . $this->time);
}

function get($key) {
$this->getStatement->execute(array(':key' => $key));
$result = $this->getStatement->fetch(PDO::FETCH_NUM);

if($result !== false) {
if($result[self::EXPIRE] == 0 || $result[self::EXPIRE] <= $this->time) {
echo $key . ' ';
echo $result[self::VALUE];
echo '<br /><br />';
eval('$result = ' . $result[self::VALUE] . ';');
} else {
$this->clearStaleEntries();
}
} else {
$result = $this->reportsTo->get($key);
}

return $result;
}

function delete($key) {
if($this->deleteStatement == null) {
$this->deleteStatement = $this->pdo->prepare('DELETE FROM cache WHERE key = :key OR (expire != 0 AND expire < ' . $this->time . ')');
}
$this->deleteStatement->execute(array(':key' => $key));
$this->reportsTo->delete($key);
}

function clear() {
$this->pdo->exec('DELETE FROM cache');
$this->reportsTo->clear();
}
}

?>
11 changes: 11 additions & 0 deletions recess/lib/recess/framework/controllers/Controller.class.php
Expand Up @@ -240,6 +240,17 @@ class ControllerDescriptor extends RecessObjectDescriptor {
public $routesPrefix = '';
public $viewClass = 'recess.framework.views.NativeView';
public $viewPrefix = '';

public static function __set_state($array) {
$descriptor = new ControllerDescriptor();
$descriptor->routes = $array['routes'];
$descriptor->methodUrls = $array['methodUrls'];
$descriptor->routesPrefix = $array['routesPrefix'];
$descriptor->viewClass = $array['viewClass'];
$descriptor->viewPrefix = $array['viewPrefix'];
$descriptor->attachedMethods = $array['attachedMethods'];
return $descriptor;
}
}

?>
7 changes: 7 additions & 0 deletions recess/lib/recess/framework/routing/Route.class.php
Expand Up @@ -23,5 +23,12 @@ public function __construct($class, $function, $methods, $path) {
else { $this->methods[] = $methods; }
$this->path = $path;
}

public static function __set_state($array) {
$route = new Route($array['class'], $array['function'], $array['methods'], $array['path']);
$route->app = $array['app'];
$route->args = $array['args'];
return $route;
}
}
?>
9 changes: 9 additions & 0 deletions recess/lib/recess/framework/routing/RoutingNode.class.php
Expand Up @@ -34,6 +34,15 @@ class RoutingNode {
protected $static_children = array();
protected $parametric_children = array();

public static function __set_state($array) {
$node = new RoutingNode();
$node->condition = $array['condition'];
$node->methods = $array['methods'];
$node->static_children = $array['static_children'];
$node->parametric_children= $array['parametric_children'];
return $node;
}

/**
* Used to add a route to the routing tree.
*
Expand Down

0 comments on commit 9e743b8

Please sign in to comment.