Redis 2 (Rediska 0.5) integration

Hi all

I’ve just finished updating to redis 2.0.4 and rediska 0.5.1.

Below are snippets for integration with Yii project.

[i]Note: you should enclosure contents of Rediska.php with




spl_autoload_unregister(array('YiiBase','autoload'));

spl_autoload_register(array('YiiBase', 'autoload'));



to enable correct behavior of Rediska implemented autoloader.

[/i]

Note: all redis related models are stored in lists/arrays.

Components.RediskaConnection




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


class RediskaConnection

{

	public $params;

	

	private $rediska_;

	

	public function init()

	{

		$this->rediska_ = new Rediska( $this->params );

	}

	public function connect()

	{

		return $this->rediska_;		

	}

}



Models.RediskaModel (base class for all models)




require_once dirname(__FILE__).'/../../Rediska/Key/List.php';


class RediskaModel extends CModel

{

	private static $_names=array();

	

	protected $prefix;

	protected $id;

	

	public function __construct( $prefix, $id = '', $scenario = '' )

	{

		$this->prefix = $prefix;

		$this->id = $id;

		

		$this->setScenario($scenario);

		$this->init();

		$this->attachBehaviors($this->behaviors());

	}

	public function init()

	{}

	public function attributeNames()

	{

		$className = get_class( $this );

		if( !isset( self::$_names[ $className ] ) )

		{

			$class = new ReflectionClass( get_class( $this ) );

			$names = array();

			foreach( $class->getProperties() as $property )

			{

				$name = $property->getName();

				if( $property->isPublic() && !$property->isStatic() )

					$names[] = $name;

			}

			return self::$_names[ $className ] = $names;

		}

		else

			return self::$_names[ $className ];

	}

	/*

	 * find/findAll

	 */

	public function find( $id )

	{

		$model = new $this( $id );

		if( $model->load() )

			return $model;

		return null;		

	}

	public function findAll()

	{

		return $this->findSelection( 0, 0 );

	}

	/*

	 * finds specified "amount" of records starting at position "start"

	 * if "amount" == 0 returns all records

	 */

	public function findSelection( $start, $amount )

	{

		$i = 0;

		

		$rediska = Yii::app()->rediskaConnection->connect();		

		$objects = $rediska->GetKeysByPattern( $this->prefix . ':*' );

		

		foreach( $objects as $object )

		{

			$id = str_replace( $this->prefix . ':', '', $object );

			

			if( !$amount )

				$model = $this->find( $id );

			else if( $i >= $start && $i < ( $start + $amount ) )

				$model = $this->find( $id );

			else if( $i >= ( $start + $amount ) )

				break;

				

			if( $model )

				$result[] = $model;

				

			++$i;

		}


		return $result;

	}

	/*

	 * load/save

	 */

	public function load()

	{

		if( $this->id != null )

		{

			$object_id = $this->prefix . ':' . $this->id;

			$rediska = Yii::app()->rediskaConnection->connect();

				

			if( $rediska->Exists( $object_id ) ) 

			{

				$object = new Rediska_Key_List( $object_id );

				return $this->fromArray( $object->toArray() );

			}			

		}

		return false;

	}

	public function save()

	{

		if( $this->validate() )

		{

			$object_id = $this->prefix . ':' . $this->id;

	    	

			$object = new Rediska_Key_List( $object_id );

			$object->truncate( 1, 0 ); //delete old values

			$object->fromArray( $this->toArray() );

			

			return true;

		}

		return false;

	}

	

	protected function get( $values, $index, $default )

	{

		if( isset( $values[ $index ] ) )

			return $values[ $index ];

			

		return $default;

	}

	protected function fromArray( $values )

	{

		$attr = self::getAttributes();

		if( !is_array( $values ) )

			Yii::log( 'Input parameter is not array', 'error', 'application.models.RediskaModel::' . $this->prefix . ':' . $this->id );

		if( count( $attr ) != count( $values) )

			Yii::log( 'Input array wrong size', 'error', 'application.models.RediskaModel::' . $this->prefix . ':' . $this->id );


		$i = 0;

		foreach( $attr as &$v )

			$v = self::get( $values, $i++, '' );


		self::setAttributes( $attr, false );

		

		return true;

	}

	protected function toArray()

	{

		$attr = self::getAttributes();

		$values = array();

		foreach( $attr as $v )

		{

			$values[] = $v;

		}

		return $values;

	}

	/*

	 * getters for base params

	 */

	public function getId() { return $this->id; }

	public function getPrefix() { return $this->getPrefix; }

	/*

	 * expire. timeout in minutes

	 */

	public function setExpire( $timeout )

	{

		$object_id = $this->prefix . ':' . $this->id;

	    	

		$rediska = Yii::app()->rediskaConnection->connect();		

		if( $rediska->Exists( $object_id ) ) 

		{

			$object = new Rediska_Key_List( $object_id );

			$object->expire( intval( $timeout ) );

			return true;

		}

		return false;

	}

	/*

	 * delete

	 */

	public function delete( $id = '' )

	{

		if( $id == null )

			$object_id = $this->prefix . ':' . $this->id;

		else

			$object_id = $this->prefix . ':' . $id;

		

		$rediska = Yii::app()->rediskaConnection->connect();		

		if( $rediska->Exists( $object_id ) )

		{

			$rediska->Delete( $object_id );

			return true;

		}

		return false;		

	}

	/*

	 * model

	 */

	public static function model($className=__CLASS__)

	{

		return new $className();

	}	

}



The project is not completed, so I’ll update snippets ASAP :)

dyabr

I think you should write what is rediska? Is it library or what?

It’s a php wrapper for Redis interface.

Rediska - PHP client for Redis

if you dont mind about using another redis php client I also implemented a client for Yii framework

You might want to check it out here : redis cache

It uses predis as redis php client and is implemented as part of Yii, so you can use by setting up your settings like this




    	'cache'=>array(

        	'class'=>'CRedisCache',

    	),



Hi, I’ll check you solutio but my goal is not caching but specific data storage and I’m going to provide partial interchangeability with CActiveRocord.

sounds great

good luck with that :)

obrigado :)