why? i dunno… just because… just a toy…
no sql, no flat file, no write permissions required anywhere, no fuss
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | class mc_chat { var $chan = null; var $mc = null; var $ret = 5; function __construct( $memcached , $channel , $retention =5) { $this ->mc = $memcached ; $this ->chan = $channel ; $this ->ret = $retention ; } function messages( $from =0 ) { $max = (int) $this ->mc->get( "$this->chan:max:posted" ); $min = (int) $this ->mc->get( "$this->chan:min:posted" ); $messages = array (); for ( $i = $min ; $i < = $max ; $i ++ ) { if ( $i < $from ) continue ; $m = $this ->get( $i ); if ( $m [ 'user' ] && $m [ 'message' ] ) $messages [ $i ] = $m ; } return $messages ; } function get( $id ) { return array ( 'user' =>(string) $this ->mc->get( "$this->chan:msg:$id:user" ), 'message' => (string) $this ->mc->get( "$this->chan:msg:$id" ), ); } function add( $user , $message ) { $id = (int) $this ->mc->increment( "$this->chan:max:posted" ); if ( ! $id ) { $id =1; $this ->mc->set( "$this->chan:max:posted" , 1); } $this ->mc->set( "$this->chan:msg:$id:user" , (string) $user ); $this ->mc->set( "$this->chan:msg:$id" , (string) $message ); if ( $id >= $this ->ret ) { if ( ! $this ->mc->increment( "$this->chan:min:posted" ) ) $this ->mc->set( "$this->chan:min:posted" , 1); } } } $mc = new Memcache; $mc ->connect( 'localhost' , 11211); $keep_messages = 10; $chatter_id = 1; $chat = new mc_chat( $mc , 'chat-room-id' , $keep_messages ); $chat ->add( $chatter_id , date ( "r" ). ": $chatter_id : foo" ); $chat ->messages(37); // messages only above id=37 $chat ->messages(); // all the latest messages |
Pretty slick. I was actually looking for something like this last week
Really nice and simple implementation of a chat adapter
love it!