Created
August 6, 2011 16:41
-
-
Save AoJ/1129494 to your computer and use it in GitHub Desktop.
Nette permanent repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is permanent repository on disk that can be used in Nette Framework projects. | |
The repository is based on Nette\Caching\Storages\FileStorage and Nette\Caching\Storages\FileJournal. | |
If you want to use the same, copy contents of both of the classes and do the changes, which are | |
mentioned in individual files. | |
I would like to take a moment in order to thank to both David Grudl and Jakub Onderka for these classes. | |
- Pavel "Birdie" Ptacek | |
------------------------------------------------------------------------------------------------------------- | |
Toto je trvalé úložiště dat na disku, které může být použito pro projekty postavené nad Nette Framework. | |
Repository je postaveno na základě Nette\Caching\Storages\FileStorage a Nette\Caching\Storages\FileJournal. | |
Pokud chcete repositář použít také, zkopírujte obě classes do samostatného souboru a udělejte změny zde popsané. | |
Rád bych se pozastavil a poděkoval jak Davidovi Grudlovi, tak Jakubovi Onderkovi za jejich práci na těchto | |
classách. | |
- Pavel "Birdie" Ptáček |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// FileJournal for the permanent repository | |
// Copy contents of the "FileJournal.php" class, and do the changes mentioned bellow. | |
// FileJournal pro permanentní repozitář. | |
// Zkopírujte obsah souboru "FileJournal.php" a proveďte níže uvedené změny | |
namespace App; | |
class FileJournal { | |
/** | |
* Find node by tag | |
*/ | |
public function findNodesByTag($tag) { | |
// Get indexing node for the tag | |
list($nodeId, $node) = $this->findIndexNode(self::TAGS, $tag); | |
$keys = array_keys($node[$tag]); | |
// Load the nodes within index & return them | |
$return = array(); | |
foreach($keys as $key) { | |
$id = $key >> self::BITROT; | |
$one = $this->getNode($id); | |
$one[self::DATA] = $one[$key]; | |
$return[] = $one; | |
} | |
return $return; | |
} | |
// the contents of the original class goes here | |
// obsah originální classy doplňte zde | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// FileStorage for the permanent repository | |
// Copy contents of the "FileStorage.php" class, and do the changes mentioned bellow. | |
// FileStorage pro permanentní repozitář. | |
// Zkopírujte obsah souboru "FileStorage.php" a proveďte níže uvedené změny | |
namespace App; | |
class FileStorage { | |
/** | |
* Get journal | |
* | |
* @return \App\FileJournal | |
* @author Pavel Ptacek | |
*/ | |
public function getJournal() { | |
return $this->journal; | |
} | |
/** | |
* Usable in conjunction with the AppFileJournal | |
* | |
* @param string $filename | |
* @return mixed|null | |
* @author Pavel Ptacek | |
*/ | |
public function readByFilename($filename) | |
{ | |
$meta = $this->readMetaAndLock($filename, LOCK_SH); | |
if ($meta && $this->verify($meta)) { | |
return $this->readData($meta); // calls fclose() | |
} else { | |
return NULL; | |
} | |
} | |
// the contents of the original class goes here | |
// obsah originální classy doplňte zde | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage of the classes for permanent storage: | |
Použití classes pro permanentní úložiště: | |
<?php | |
$journal = new AppFileJournal(DATA_DIR . '/user/'); | |
$driver = new AppFileStorage(DATA_DIR . '/user/', $journal); | |
// Get the cache tags | |
$tags = array(); | |
$tags[] = self::USERID . self::SEPARATOR . $data->user->id; | |
$tags[] = self::TIMESTAMP . self::SEPARATOR . $data->timestamp; | |
// Get the key | |
do { | |
$key = sha1(uniqid(time())); | |
$has = $driver->read($key); | |
} while(!empty($has)); | |
// Store the metrics into cache | |
return $driver->write($key, $data, array(\Nette\Caching\Cache::TAGS => $tags)); | |
?> | |
Searching within the storage: | |
Vyhledávání v rámci storage: | |
<?php | |
class Mine { | |
const USERID = 'userId'; | |
const TIMESTAMP = 'timestamp'; | |
const SEPARATOR = '/'; | |
/** | |
* Find data by the user id | |
* | |
* @param int $userId | |
*/ | |
public function getDataByUserId($userId) { | |
$journal = new App\FileJournal(DATA_DIR . '/user/'); | |
$driver = new App\FileStorage(DATA_DIR . '/user/', $journal); | |
$tag = self::USERID . self::SEPARATOR . $userId; | |
$nodes = $journal->findNodesByTag($tag); | |
foreach($nodes as $node) { | |
dump($driver->readByFilename($node[App\FileJournal::DATA][App\FileJournal::KEY])); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment