Skip to content

Commit

Permalink
- added Nette::Caching & test
Browse files Browse the repository at this point in the history
- modified Config & Environment to solve chicken&egg problem with environment variables
- preliminary support for Environment caching
- new features in RobotLoader: 'netterobots.txt' files, integrated with Nette::Caching, ...
  • Loading branch information
dg committed Apr 21, 2008
1 parent c13ed82 commit 4417f7a
Show file tree
Hide file tree
Showing 45 changed files with 1,466 additions and 218 deletions.
172 changes: 172 additions & 0 deletions Nette/Caching/Cache.php
@@ -0,0 +1,172 @@
<?php

/**
* Nette Framework
*
* Copyright (c) 2004, 2008 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "Nette license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://nettephp.com/
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @category Nette
* @package Nette::Caching
*/

/*namespace Nette::Caching;*/



require_once dirname(__FILE__) . '/../Object.php';



/**
* Implements the cache for a application.
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @package Nette
* @version $Revision$ $Date$
*/
class Cache extends /*Nette::*/Object implements ArrayAccess
{
/** @var ICacheStorage */
private $storage;



public function __construct(ICacheStorage $storage = NULL)
{
if ($storage === NULL) {
require_once dirname(__FILE__) . '/../Caching/FileCache.php';
$this->storage = new FileCache;
} else {
$this->storage = $storage;
}
}



/**
* Inserts (replaces) item into the cache.
* @param string key
* @param mixed
* @param array
* @param int
* @return void
* @throws ::InvalidArgumentException
*/
public function add($key, $data, $tags = NULL, $lifeTime = 0)
{
if (!is_string($key)) { // prevents NULL
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

$cache = $this->offsetGet($key);
if ($cache === NULL) {
$this->storage->write($key, $data, $tags, $lifeTime, NULL);
return NULL;

} else {
return $cache;
}
}



/**
* Inserts (replaces) item into the cache.
* @param string key
* @param mixed
* @param array
* @param int
* @return void
* @throws ::InvalidArgumentException
*/
public function insert($key, $data, $tags = NULL, $lifeTime = 0)
{
if (!is_string($key)) {
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

$this->storage->write($key, $data, $tags, $lifeTime, NULL);
}


/********************* interface ::ArrayAccess ****************d*g**/



/**
* Inserts (replaces) item into the cache (::ArrayAccess implementation).
* @param string key
* @param mixed
* @return void
* @throws ::InvalidArgumentException
*/
public function offsetSet($key, $data)
{
if (!is_string($key)) { // prevents NULL
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

$this->storage->write($key, $data, NULL, 0, NULL);
}



/**
* Retrieves the specified item from the cache or NULL if the key is not found (::ArrayAccess implementation).
* @param string key
* @return mixed|NULL
* @throws ::InvalidArgumentException
*/
public function offsetGet($key)
{
if (!is_string($key)) {
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

return $this->storage->read($key);
}



/**
* Exists item in cache? (::ArrayAccess implementation).
* @param string key
* @return bool
* @throws ::InvalidArgumentException
*/
public function offsetExists($key)
{
if (!is_string($key)) {
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

return $this->storage->read($key) !== NULL;
}



/**
* Removes the specified item from the cache.
* @param string key
* @return void
* @throws ::InvalidArgumentException
*/
public function offsetUnset($key)
{
if (!is_string($key)) {
throw new /*::*/InvalidArgumentException('Key must be a string.');
}

$this->storage->remove($key);
}

}
152 changes: 152 additions & 0 deletions Nette/Caching/FileCache.php
@@ -0,0 +1,152 @@
<?php

/**
* Nette Framework
*
* Copyright (c) 2004, 2008 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "Nette license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://nettephp.com/
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @category Nette
* @package Nette::Caching
*/

/*namespace Nette::Caching;*/



require_once dirname(__FILE__) . '/../Object.php';

require_once dirname(__FILE__) . '/../Caching/ICacheStorage.php';



/**
* Cache file storage
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @package Nette
* @version $Revision$ $Date$
*/
class FileCache extends /*Nette::*/Object implements ICacheStorage
{
/** @var string */
private $dir;

/** @var string last query cache */
private $key;

/** @var mixed last query cache */
private $data;



public function __construct($dir = NULL)
{
if ($dir === NULL) {
$dir = /*Nette::*/Environment::getVariable('cacheDir');
require_once dirname(__FILE__) . '/../IO/SafeStream.php';
}

if (!is_dir($dir) || !is_writable($dir)) {
throw new /*::*/InvalidStateException("Temporary directory '$dir' is not writable.");
}

$this->dir = $dir . '/';
}



/**
* Read from cache.
* @param string key
* @return void
*/
public function read($key)
{
if ($this->key === $key) {
return $this->data;
}

$this->key = $key;
$cache = @unserialize(file_get_contents($this->getFile($key))); // intentionally @
if (is_array($cache)) {
return $this->data = $cache['data'];
} else {
return $this->data = NULL;
}
}



/**
* Writes item into the cache.
* @param string key
* @param mixed
* @param array
* @param int
* @param int
* @return void
*/
public function write($key, $data, $tags, $lifeTime, $priority)
{
if ($data === NULL) {
$this->remove($key);
return;
}

$this->key = $key;
$this->data = $data;

$s = serialize(array(
'data' => $data
));
$file = $this->getFile($key);
$len = file_put_contents($file, $s); // intentionally @
if ($len !== strlen($s)) {
unlink($file);
$this->data = NULL;
}
}



/**
* Removes item from the cache.
* @param string key
* @return void
*/
public function remove($key)
{
$this->key = $key;
$this->data = NULL;
@unlink($this->getFile($key)); // intentionally @
}



/**
* Removes items from the cache by dependencies.
* @param array tags
* @return void
*/
public function clean($tags)
{
$this->key = $this->data = NULL;
throw new /*::*/NotImplementedException();
}


private function getFile($key)
{
return $this->dir . urlencode($key) . '.cache';
}

}
43 changes: 43 additions & 0 deletions Nette/Caching/ICacheStorage.php
@@ -0,0 +1,43 @@
<?php

/**
* Nette Framework
*
* Copyright (c) 2004, 2008 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "Nette license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://nettephp.com/
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com/
* @category Nette
* @package Nette::Caching
*/

/*namespace Nette::Caching;*/



/**
* Cache storage (EXPERIMENTAL).
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @package Nette
* @version $Revision$ $Date$
*/
interface ICacheStorage
{

function read($key);

function write($key, $data, $tags, $lifeTime, $priority);

function remove($key);

function clean($tags);

}
2 changes: 1 addition & 1 deletion Nette/Collections/Collection.php
Expand Up @@ -214,7 +214,7 @@ public function count()
*/
public function getIterator()
{
return new /*::*/ArrayIterator($this->toArray());
return new /*::*/ArrayIterator($this->data);
}


Expand Down

0 comments on commit 4417f7a

Please sign in to comment.