ARedisCommand.php 929 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. Yii::import("packages.redis.*");
  3. /**
  4. * A simple console command that subscribes to a redis channel
  5. * @package packages.redis.commands
  6. */
  7. class ARedisCommand extends CConsoleCommand {
  8. public function actionSubscribe($args) {
  9. $redis = $this->getConnection();
  10. $channel = new ARedisChannel(array_shift($args), $redis);
  11. $channel->onReceiveMessage = function(CEvent $event) {
  12. $message = (object) json_decode($event->sender->getLastMessage());
  13. if (preg_match_all("/in (.*) \((.*)\)/",$message->message,$matches)) {
  14. foreach($matches[1] as $filename) {
  15. $line = array(
  16. $message->time,
  17. $message->category,
  18. "M",
  19. $filename
  20. );
  21. echo implode("|",$line)."\n";
  22. }
  23. }
  24. };
  25. $channel->subscribe();
  26. }
  27. /**
  28. * Gets the connection to redis
  29. * @return ARedisConnection the connection to redis
  30. */
  31. public function getConnection() {
  32. return Yii::app()->redis;
  33. }
  34. }