Yii Framework Forum: Redis 2 (Rediska 0.5) integration - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Redis 2 (Rediska 0.5) integration code snippet for integration with rediska client Rate Topic: -----

#1 User is offline   dyabr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 24-November 10

Posted 24 November 2010 - 06:54 AM

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.

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.


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
0

#2 User is offline   diggy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 112
  • Joined: 18-November 09
  • Location:Ukraine, Khmelnitsky

Posted 24 November 2010 - 07:12 AM

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

#3 User is offline   dyabr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 24-November 10

Posted 24 November 2010 - 07:24 AM

View Postdiggy, on 24 November 2010 - 07:12 AM, said:

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
0

#4 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 24 November 2010 - 09:33 AM

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',
    	),

--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#5 User is offline   dyabr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 24-November 10

Posted 24 November 2010 - 09:55 AM

View PostGustavo, on 24 November 2010 - 09:33 AM, said:

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


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.
0

#6 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 24 November 2010 - 10:51 AM

View Postdyabr, on 24 November 2010 - 09:55 AM, said:

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 :)
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#7 User is offline   dyabr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 24-November 10

Posted 25 November 2010 - 05:52 AM

View PostGustavo, on 24 November 2010 - 10:51 AM, said:

sounds great
good luck with that :)

obrigado :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users