ユーザ用ツール

サイト用ツール


redis:sample001

sample

<?php

class RedisService {

   private $redis;

   /**
    * コンストラクタ
    */
   public function __construct() {

       $hostname = 'localhost';
       $port = 6379;

       $this->redis = new Redis();

       if (!$this->redis->connect($hostname, $port)) {
           throw new Exception('fail connect redis server.');
       }
   }

   /**
    * デストラクタ
    */
   public function __destruct() {
       $this->redis->close();
   }

   public function setValue($key, $value) {
       $this->redis->set($key, $value);
   }

   public function getValue($key) {
       return $this->redis->get($key);
   }

   public function zAdd($key, $score, $value) {
       $this->redis->zAdd($key, $score, $value);
   }

   public function zRevRange($key, $start, $end, $flg) {
       return $this->redis->zRevRange($key, $start, $end, $flg);
   }

   public function zCount($key, $score) {
       return $this->redis->zCount($key, $score, '+inf');
   }
   
   public function zIncrBy($key, $score, $value) {
       $this->redis->zIncrBy($key, $score, $value);
   }

   public function zScore($key, $value) {
       return $this->redis->zScore($key, $value);
   }

   
}
<?php
	ini_set( 'display_errors', 1 );
	
	include('./cre.php');
	
	$redis = new RedisService();
		 
	/*
	$redis->setValue('foo', 'bar');
		 
	$value = $redis->getValue('foo');
	
	var_dump($value);
	*/
	
	$redis->zAdd('fooo', 1, 'taro');
	$redis->zAdd('fooo', 2, 'hanako');
	$redis->zAdd('fooo', 3, 'jiro');
	$redis->zAdd('fooo', 4, 'yoko');
	$redis->zAdd('fooo', 5, 'yoji');
		 
	$ranking = $redis->zRevRange( 'fooo', 0, -1, true );
	echo "<pre>";
	
	foreach($ranking as $user => $score ) {
	    $score++;
	    $val = $redis->zCount('fooo', $score);
	    echo "$user=".($val+1)."\n";
	    echo "$user=".$redis->zScore('fooo', $user)."\n";
	}
	
	$redis->zIncrBy('fooo', 5, 'yoji');
		 
	$ranking = $redis->zRevRange( 'fooo', 0, -1, true );
	echo "<pre>";
	
	foreach($ranking as $user => $score ) {
	    $score++;
	    $val = $redis->zCount('fooo', $score);
	    echo "$user=".($val+1)."\n";
	    echo "$user=".$redis->zScore('fooo', $user)."\n";
	}
redis/sample001.txt · 最終更新: 2014/02/26 02:39 by clownclown

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki