Permalink
Browse files

first commit

  • Loading branch information...
phpnode committed Nov 1, 2011
0 parents commit 22d9129410ec48461bfcc814aeb5ed1b7e40ce4b
@@ -0,0 +1,114 @@
+<?php
+/**
+ * A cache component that allows items to be cached using redis.
+ * @author Charles Pick
+ * @package packages.redis
+ */
+class ARedisCache extends CCache {
+ /**
+ * Holds the redis connection
+ * @var ARedisConnection
+ */
+ protected $_connection;
+
+
+ /**
+ * Sets the redis connection to use for caching
+ * @param ARedisConnection|string $connection the redis connection, if a string is provided, it is presumed to be a the name of an applciation component
+ */
+ public function setConnection($connection)
+ {
+ if (is_string($connection)) {
+ $connection = Yii::app()->{$connection};
+ }
+ $this->_connection = $connection;
+ }
+
+ /**
+ * Gets the redis connection to use for caching
+ * @return ARedisConnection
+ */
+ public function getConnection()
+ {
+ if ($this->_connection === null) {
+ if (!isset(Yii::app()->redis)) {
+ throw new CException("ARedisCache expects a 'redis' application component");
+ }
+ $this->_connection = Yii::app()->redis;
+ }
+ return $this->_connection;
+ }
+
+ /**
+ * Retrieves a value from cache with a specified key.
+ * This is the implementation of the method declared in the parent class.
+ * @param string a unique key identifying the cached value
+ * @return string the value stored in cache, false if the value is not in the cache or expired.
+ */
+ protected function getValue($key) {
+ return $this->getConnection()->getClient()->get($key);
+ }
+
+ /**
+ * Retrieves multiple values from cache with the specified keys.
+ * @param array a list of keys identifying the cached values
+ * @return array a list of cached values indexed by the keys
+ * @since 1.0.8
+ */
+ protected function getValues($keys) {
+ return $this->getConnection()->getClient()->mget($keys);
+ }
+
+ /**
+ * Stores a value identified by a key in cache.
+ * This is the implementation of the method declared in the parent class.
+ *
+ * @param string the key identifying the value to be cached
+ * @param string the value to be cached
+ * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
+ * @return boolean true if the value is successfully stored into cache, false otherwise
+ */
+ protected function setValue($key,$value,$expire = 0) {
+
+ $this->getConnection()->getClient()->set($key,$value);
+ if ($expire) {
+ $this->getConnection()->getClient()->expire($key,$expire);
+ }
+ }
+
+ /**
+ * Stores a value identified by a key into cache if the cache does not contain this key.
+ * This is the implementation of the method declared in the parent class.
+ *
+ * @param string the key identifying the value to be cached
+ * @param string the value to be cached
+ * @param integer the number of seconds in which the cached value will expire. 0 means never expire.
+ * @return boolean true if the value is successfully stored into cache, false otherwise
+ */
+ protected function addValue($key,$value,$expire) {
+ if($expire>0)
+ $expire+=time();
+ else
+ $expire=0;
+
+ return $this->getConnection()->getClient()->add($key,$value,$expire);
+ }
+
+ /**
+ * Deletes a value with the specified key from cache
+ * This is the implementation of the method declared in the parent class.
+ * @param string the key of the value to be deleted
+ * @return boolean if no error happens during deletion
+ */
+ protected function deleteValue($key) {
+ return $this->getConnection()->getClient()->delete($key);
+ }
+
+ /**
+ * Deletes all values from cache.
+ * Be careful of performing this operation if the cache is shared by multiple applications.
+ */
+ public function flush() {
+ return $this->getConnection()->getClient()->flush();
+ }
+}
@@ -0,0 +1,154 @@
+<?php
+/**
+ * Represents a redis pub/sub channel.
+ *
+ * Publishing messages:
+ * <pre>
+ * $channel = new ARedisChannel("myChannel");
+ * $channel->publish("hello world"); // sends a message to the channel
+ * </pre>
+ *
+ * Subscribing to channels:
+ * <pre>
+ * $channel = new ARedisChannel("myChannel");
+ * $channel->onReceiveMessage = function($redis, $channel, $message) {
+ * echo "Message Received:".$message."\n";
+ * };
+ * $channel->subscribe(); // blocks, the callback is triggered when a message is received
+ * </pre>
+ * @author Charles Pick
+ * @package packages.redis
+ */
+class ARedisChannel extends ARedisIterableEntity {
+ /**
+ * Holds the data in the entity
+ * @var array
+ */
+ protected $_data = array();
+ /**
+ * Subscribes to the channel
+ * @return ARedisIterableChannel $this subscribed to the channel
+ */
+ public function subscribe() {
+ if ($this->name === null) {
+ throw new CException(get_class($this)." requires a name!");
+ }
+ $this->getConnection()->getClient()->subscribe(array($this->name),array($this,"receiveMessage"));
+ return $this;
+ }
+ /**
+ * Unsubscribes from the channel
+ * @return ARedisIterableChannel $this unsubscribed from the channel
+ */
+ public function unsubscribe() {
+ if ($this->name === null) {
+ throw new CException(get_class($this)." requires a name!");
+ }
+ $this->getConnection()->getClient()->unsubscribe(array($this->name));
+ return $this;
+ }
+
+ /**
+ * Publishes a message to the channel
+ * @param string $message The message to publish
+ * @return integer the number of clients that received the message
+ */
+ public function publish($message) {
+ if ($this->name === null) {
+ throw new CException(get_class($this)." requires a name!");
+ }
+ $this->_data[] = $message;
+ return $this->getConnection()->getClient()->publish($this->name,$message);
+ }
+ /**
+ * Receives a message from a subscribed channel
+ * @param Redis $redis the redis client instance
+ * @param string $channel the name of the channel
+ * @param string $message the message content
+ */
+ public function receiveMessage($redis, $channel, $message) {
+ $this->_data[] = $message;
+ $event=new CEvent($this);
+ $this->onReceiveMessage($event);
+ }
+ /**
+ * Gets the last received / sent message
+ * @return mixed the last message received, or null if no messages have been received yet
+ */
+ public function getLastMessage() {
+ $count = count($this->_data);
+ if (!$count) {
+ return null;
+ }
+ return $this->_data[$count - 1];
+ }
+
+ /**
+ * This event is raised after a message is received
+ * @param CEvent $event the event parameter
+ */
+ public function onReceiveMessage($event)
+ {
+ $this->raiseEvent('onReceiveMessage',$event);
+ }
+
+ /**
+ * Gets the number of items in the channel
+ * @return integer the number of items in the channel
+ */
+ public function getCount() {
+ return count($this->_data);
+ }
+ /**
+ * Gets all the members in the sorted set
+ * @param boolean $forceRefresh whether to force a refresh or not, IGNORED!
+ * @return array the members in the set
+ */
+ public function getData($forceRefresh = false) {
+ return $this->_data;
+ }
+
+ /**
+ * Returns whether there is an item at the specified offset.
+ * This method is required by the interface ArrayAccess.
+ * @param integer $offset the offset to check on
+ * @return boolean
+ */
+ public function offsetExists($offset)
+ {
+ return isset($this->data[$offset]);
+ }
+
+ /**
+ * Returns the item at the specified offset.
+ * This method is required by the interface ArrayAccess.
+ * @param integer $offset the offset to retrieve item.
+ * @return mixed the item at the offset
+ * @throws CException if the offset is invalid
+ */
+ public function offsetGet($offset)
+ {
+ return $this->_data[$offset];
+ }
+
+ /**
+ * Sets the item at the specified offset.
+ * This method is required by the interface ArrayAccess.
+ * @param integer $offset the offset to set item
+ * @param mixed $item the item value
+ */
+ public function offsetSet($offset,$item)
+ {
+ $this->_data[$offset] = $item;
+ }
+
+ /**
+ * Unsets the item at the specified offset.
+ * This method is required by the interface ArrayAccess.
+ * @param integer $offset the offset to unset item
+ */
+ public function offsetUnset($offset)
+ {
+ unset($this->_data[$offset]);
+ }
+}
Oops, something went wrong.

0 comments on commit 22d9129

Please sign in to comment.