Rediska

Follow me on twitter…

Extending

Very useful practice - extend key objects and use them as your application models.

Example

<?php

require_once 'Rediska/Key/Set.php';
class Friends extends Rediska_Key_Set
{
    protected $_userId;
    public function __construct($userId)
    {
        $this->_userId = $userId;
        parent::__construct("users:$userId:friends");
    }

    public function addFriend($friendId)
    {
        $this->add($friendId);
        // Add me too
        $friend = new self($friendId);
        $friend->add($this->_userId);
    }
}

?>

Also you can extend single-value keys for caching:

<?php

require_once 'Rediska/Key.php';
class Cache_LastPosts extends Rediska_Key
{
    public function __construct()
    {
        parent::__construct('last_posts', 60 * 2); // expire after 2 minutes
    }
}

// Get from Db with Posts::getLast() and save to cache
// or get from cache if key present and not expired
$lastPostsCache = new Cache_LastPosts();
$lastPosts = $lastPostsCache->getOrSetValue(new Posts())->getLast();
foreach($lastPosts as $post) {
    echo $post;
}

?>

Comments