Yii Framework Forum: Redis Cache - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Redis Cache Use Redis as your cache system Rate Topic: -----

#1 User is offline   Gustavo 

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

Posted 18 November 2010 - 05:35 PM

Use Redis as your cache system

Redis is a great cache system, it store the cache in ram and send it to the hard drive once in a while, so you dont lose when the pc shut down

Simply add the 2 download files to your protected/components folder

##Requirements

PHP 5.2.x

##Usage

define in the application's configuration array

 'components'=>array(
 		'cache'=>array(
 			'class'=>'CRedisCache',
//if you dont use servers options it will use the default one "host=>'127.0.0.1',port=>6379"
 			'servers'=>array(
 				array(
 					'host'=>'server1',
 					'port'=>6379,
 				),
 				array(
 					'host'=>'server2',
 					'port'=>6379,
 				),
 			),
 		),
 	),



then you can use any redis function by calling

Yii::app()->cache->{function}(args);



##Resources

It uses Predis as redis php client
* [Redis page] (redis)
* [Predis page] (Predis)
--
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

#2 User is offline   Gustavo 

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

Posted 15 February 2011 - 04:33 AM

added as an extension here
--
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

#3 User is offline   canu_haha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-February 11
  • Location:Beijing, China

Posted 21 February 2011 - 10:50 PM

CException

Property "CRedisCache.servers" is read only?
0

#4 User is offline   Gustavo 

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

Posted 21 February 2011 - 10:52 PM

when ?
what did you do ?
could you show me the part of the code that throws the exception ?

Thanks
--
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   canu_haha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-February 11
  • Location:Beijing, China

Posted 21 February 2011 - 11:14 PM

nginx 0.8.54
php 5.3.4
yii 1.1.6.r2877
php redis 2.1.0

I am download rediscache from /extension/rediscache/
and copy to protected/extensions/

                protected/extensions/redis/CRedisCache.php
                protected/extensions/redis/Predis.php


conf/main.php
'cache'=>array( 
	'class'=>'ext.redis.CRedisCache', 
	'servers'=>array( 
		array( 
			'host'=>'192.168.100.206', //redis master
			'port'=>6379, 
		),
		/*
		array( 
			'host'=>'192.168.100.207',
			'port'=>6379, 
		),
		array( 
			'host'=>'192.168.100.208',
			'port'=>6379, 
		), 
		array( 
			'host'=>'192.168.100.209',
			'port'=>6379, 
		),*/
	), 
), 


create a module "user"
protected/modules/user/models/User.php

class User extends CBaseDao {
	public function testRedisCache() {
		$sql = "SELECT username FROM account";
		$data = $this->slave()->createCommand($sql);
		//return Yii::app()->cache->set(md5($sql), $data, 3600);
		return Yii::app()->cache->set('test', 12345, 3600);
	}
}


then I get the error message:
Property "CRedisCache.servers" is read only

/home/framework/YiiBase.php(214)
213 foreach ($config as $key=>$value)
214	$object->$key=$value;
215
216 return $object;

0

#6 User is offline   canu_haha 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 11-February 11
  • Location:Beijing, China

Posted 22 February 2011 - 12:30 AM

59 public function getServers()
60 {
61     if($this->servers!==null){
62          $this->_servers=$this->servers;
63          $this->servers=null;
64     }
65     return $this->_servers;
66 }

61, Undefined property: CRedisCache::$servers

add
protected $servers=array();

CRedisCache.php
require_one Yii::getPathOfAlias($this->predisPath);

require_once(/home/luoyinyou/webgame/app/protected/extensions/redis/Predis) filed to open stream: No such file or directory

require_one Yii::getPathOfAlias($this->predisPath).'.php';

oh....
Property CRedisCache.useMasterDb is not defined...

then I go to eat....
0

#7 User is offline   Gustavo 

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

Posted 22 February 2011 - 02:57 AM

Fixed the bug, new file can be found in the extension page
--
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

#8 User is offline   ice.deng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-November 10

Posted 26 February 2011 - 04:33 AM

solve :Property "CRedisCache.servers" is read only

update getRedis() method:
old:
Yii::import("application.extensions.redis.Predis.php"); 

new:
Yii::import("application.extensions.redis.Predis", true);


append method:

public function setServers($config)
{
	$this->_servers= $config;
}

0

#9 User is offline   Gustavo 

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

Posted 26 February 2011 - 04:54 AM

Thanks Ice

I have solved it another way

Did you try the new download in the extension page ?

Its working properly now.

Anyway, thanks for sharing your fix
--
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

#10 User is offline   ice.deng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-November 10

Posted 26 February 2011 - 05:18 AM

php 5.3.5
yii 1.1.6.r2877

The new version is still have problems

error:
CException

属性 "CRedisCache.servers" 是只读的.
0

#11 User is offline   Gustavo 

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

Posted 26 February 2011 - 05:21 AM

Huh ...
odd .. .
work normally here at home and at work ....
gonna check it out and give you a feedback soon
--
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

#12 User is offline   ice.deng 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 10-November 10

Posted 26 February 2011 - 05:53 AM

http://www.yiiframew...t/files/?id=348
redis.zip (18.7 KB)
version 0.3
Released on Feb 25, 2011; downloaded 0 times.

I use the above version,may not be the latest.
0

#13 User is offline   Gustavo 

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

Posted 26 February 2011 - 06:03 AM

Thanks

I just deleted the old one to not cause confusion

the version 0.3 is the right one ..

This one is the one that is working to me
--
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

#14 User is offline   Gustavo 

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

Posted 20 March 2011 - 07:15 AM

Quote

CException

Property "CRedisCache.servers" is read only


That is because you must set cache inside 'components' index in the config
--
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

#15 User is offline   Kutsy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-August 10

Posted 21 March 2011 - 05:45 AM

How I can set Redis database when I need store/get data?
Can I flush only one Redis database (by setting dbindex)?

I mean smth like this:
Yii::app()->cache->setValue($key, $value, $expire, $dbindex);
Yii::app()->cache->getValue($key, $dbindex);
Yii::app()->cache->flush($dbindex);

0

#16 User is offline   Gustavo 

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

Posted 21 March 2011 - 06:01 AM

Hi Kutsy
You should have no problem by using any redis default function ( predis client implemented it all ) since the extension uses __call() magic method

$cache=Yii::app()->getCache();
//selects a database
$cache->select($db);
//flushs the database
$cache->flushdb();
//or set/get to/from this database
$cache->set('index','value',60);
$cache->get('index');


Anyway, I'll think about to implement the methods you suggested

Thanks
--
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

#17 User is offline   Kutsy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-August 10

Posted 21 March 2011 - 06:50 AM

OK, thanks.
0

#18 User is offline   pringlized 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 11-April 11

Posted 27 November 2011 - 04:29 AM

What do you do when the redis connection is down? I've been pulling my hair out on this. Everything works great when redis is up and running. So I wanted to test and build a solid fall back to the DB when redis was down. The problem became apparent when I realized that the init() of CRedisCache does not create a connection or ping for the sign of a connection. But once I execute a command against the cache, if the connection is down Predis will throw the exception Predis_CommunicationException.

I never thought this would be an issue. Anytime I called a Yii::app()->cache-> command I wrapped it in try/catch blocks and caught Predis_CommunicationException. My problem is that even if I catch the error, it still exists in the Yii exception head space and I kills the stack at a later point.

When I put the following in my config/main.php, it eliminates the display of any error data which was an earlier problem. Unfortunately, the exception is still sitting in the errorHandler on CApplication(i think that's the obj location). And because of that, CApplication->handleError is still called down the stack and it exits the app instead of skipping over the exception.
'onException' => function($event)
{
    if ($event->exception instanceof Predis_CommunicationException)
    {
        $event->handled = true;
    }
},


When I attempt to get a key from redis, I call this. Be reminded, when redis is up this works like a charm. It's only the persistence of the catch (when redis is down) in Yii's errorHandler that is the problem site wide.
try
{
    $debtorCache = Yii::app()->cache->get($key);
    if(empty($debtorCache))
    {
        // this works great....
        // get data from db..
	// set cache data
	// return data
    }
    else
    {
        // this works great....
        list($debtorData, $caseData, $paymentData, $activityData) = $debtorCache;
    }
}
catch(Predis_CommunicationException $e)
{
    // displays just fine before CApplication handleError exits....
    echo "{$e->getFile()}:{$e->getLine()}: Redis is down..<br>";
}


I kept getting a standard Yii uncaught exception that gave the error name and message which killed the app.

FYI: I use PHPStorm with xdebug. I ran through the stack on this one line by line, and stepped into every call. I couldn't figure out why the Predis exception was still being called after I caught it. Looks like even setting an event->exception to "handled" still gets a exit(1). Curious why.

I'm starting to think this has something to do with how Yii manages "uncaught" exceptions in the handleError() in CApplication. Not sure what to make of this since I have caught the error everytime I call Yii::app()->cache, and by using CApplication->onException in config/main.php, set $event->handled.

Any help would be greatly appreciated.

Attached File(s)


0

#19 User is offline   muayyad alsadi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 27-April 11

Posted 03 June 2012 - 08:20 AM

updated to use the latest stable Predis 7.2 and it's attached

Attached File  redis.zip (444.02K)
Number of downloads: 128

put redis so that its Autoloader.php is redis/Autoloader.php where redis directory is next to CRedisCache.php

here is the diff

@@ -50,9 +60,12 @@
                if($this->_cache!==null)
                        return $this->_cache;
                else{
-            require_once Yii::getPathOfAlias($this->predisPath).".php";
+            // to use the old single file appreach comment out the next two lines and uncomment the 3rd
+            require_once Yii::getPathOfAlias($this->predisPath).'/Autoloader.php';
+            Predis\Autoloader::register();
+            //require_once Yii::getPathOfAlias($this->predisPath).'.php'; // old single file approach,no autoloading
             Yii::log('Opening Redis connection',CLogger::LEVEL_TRACE);
-                       return $this->_cache=new Predis_Client($this->servers);
+                       return $this->_cache=new Predis\Client($this->servers);
         }
        }

0

#20 User is offline   danvan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 14-August 09

Posted 06 November 2012 - 10:51 PM

apache 2.2
yii 1.1.12
redis server 2.4.6

I've got NotSupportedException while trying to use 'watch' command within this extension.

By my debugging, the watch command cannot be hashed. Is there anyone can tell me how to apply this command? Thanks a lot.

Here is the debug info:

Attached File(s)


0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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