Rediska

Follow me on twitter…

Hashes implemented!

August 2, 2010

Let me introduce you Hashes implemented in Rediska:

<?php

$user = new Rediska_Key_Hash('users:1');

// Fields accessible as object properties
$user->firstName = 'John';

// Fields accessible as array keys
$user['lastName'] = 'Silver';

// You can check if field is set
if (!isset($user->nick)) {
    // You can set several fields at a time
    $user->set(array('nick' => 'Josi', 'confirmed' => 1, 'viewed' => 3));
}
// You can remove fields
unset($user->confirmed);

// You can get fields count
count($user);

// You can increment field's value
$user->increment('viewed');

// You can iterate fields
foreach($user as $field => $value) {
    print "$field => $value";
}

// You can create models extending Rediska_Key_Hash
class User extends Rediska_Key_Hash
{
    public function __construct($id)
    {
        parent::__construct("users:$id");
    }

    public function isAdmin()
    {
        return $this->role == 'admin';
    }
}

$user = new User(1);
if ($user->isAdmin()) {
    print 'Welcome ' . $user->login;
}

?>

Rediska Hash commands:

  • Rediska#setToHash($name, $fieldOrData, $value = null, $overwrite = true)
  • Rediska#getFromHash($name, $fieldOrFields)
  • Rediska#incrementInHash($name, $field, $amount = 1)
  • Rediska#existsInHash($name, $field)
  • Rediska#deleteFromHash($name, $field)
  • Rediska#getHashLength($name)
  • Rediska#getHash($name)
  • Rediska#getHashFields()
  • Rediska#getHashValues()

Comments

Fork me on GitHub