cakephp2.x redisbehavior.php

2014年4月28日更新 view: 78 view
photoBy: http://www.victusspiritus.com/wp-content/uploads/…

Redisbehavior.php を作ってみた

スポンサードリンク

redisをcakephpから簡単に使う redisbehavior.php

https://github.com/matsubo/redis-ranking

をcakephp用に改造してみました。

RedisBehavior.php

<?php
class RedisBehavior extends ModelBehavior {

    private $namespace = 'hoge';

    public $redis;

    public function setup(Model $model, $config = array()) {
        $this->namespace = $config['namespace'];
        $this->redis=new Redis();
        $this->redis->connect('127.0.0.1', 6379);
    }

    /**
     * setUserScore
     *
     * @param int $user_id
     * @param int $score
     * @access public
     * @return int
     */
    public function setUserScore(Model $model ,$user_id, $score)
    {

        return $this->redis->zadd($this->namespace, $score, $user_id);
    }
    /**
     * getScore
     *
     * @param int $user_id
     * @access public
     * @return int
     */
    public function getScore(Model $model ,$user_id)
    {

        return $this->redis->zScore($this->namespace, $user_id);
    }
    /**
     * getRank
     *
     * @param int $user_id
     * @access public
     * @return int
     */
    public function getRank(Model $model ,$user_id)
    {
        return $this->redis->zRank($this->namespace, $user_id);
    }

    public function getRevRank(Model $model ,$user_id)
    {
        return $this->redis->zRevRank($this->namespace, $user_id);
    }
    /**
     * incrementScore
     *
     * @param int $user_id
     * @param int $score_diff
     * @access public
     * @return int
     */
    public function incrementScore(Model $model ,$user_id, $score_diff)
    {
        return $this->redis->zIncrBy($this->namespace, $score_diff, $user_id);
    }
    /**
     * getRange
     *
     * @param int $start
     * @param int $end
     * @param bool $withscores
     * @access public
     * @return array
     */
    public function getRange(Model $model ,$start = 0, $end = -1, $withscores = false)
    {
        return $this->redis->zRange($this->namespace, $start, $end, $withscores);
    }
    /**
     * getRevRange
     *
     * @param int $start
     * @param int $end
     * @param bool $withscores
     * @access public
     * @return array
     */
    public function getRevRange(Model $model ,$start = 0, $end = -1, $withscores = false)
    {
        return $this->redis->zRevRange($this->namespace, $start, $end, $withscores);
    }
    /**
     * getRangeByScores
     *
     * @param int $start
     * @param int $end
     * @param bool $withscores
     * @param int $offset
     * @param int $count
     * @access public
     * @return array
     */
    public function getRangeByScores(Model $model ,$start = 0, $end = -1, $withscores = false, $offset = null, $count = null)
    {
        $options = array();
        $options['withscores'] = $withscores;
        if ($offset && $count) {
            $options['limit'] = array($offset, $count);
        }

        return $this->redis->zRangeByScore($this->namespace, $start, $end, $options);
    }
    /**
     * deleteUser
     *
     * @param int $user_id
     * @access public
     * @return int
     */
    public function deleteUser(Model $model ,$user_id)
    {
        return $this->redis->zDelete($this->namespace, $user_id);
    }
    /**
     * deleteAllUser
     *
     * @access public
     * @return array
     */
    public function deleteAllUser(Model $model)
    {
        $deleted_user_id = array();
        foreach ($this->redis->zRange($this->namespace, 0, -1) as $user_id) {
            $this->redis->zDelete($this->namespace, $user_id);
            $deleted_user_id[] = $user_id;
        }

        return $deleted_user_id;
    }
    /**
     * countUsers
     *
     * @access public
     * @return int
     */
    public function countUsers(Model $model)
    {
        return $this->redis->zSize($this->namespace);
    }


}

User.php (使いたい任意のモデル)

<?
    App::uses('AppModel', 'Model');
    class User extends AppModel {
        var $actsAs = array(
            'Redis' => array(
                'namespace' => 'ranking'//初期値 fields の値をセット
            )
        );




    }

?>

こんな風に使う

$this->loadModel('User');

        //スコアをセット
        $this->User->setUserScore('tarou', 150);
        $this->User->setUserScore('jirou', 5000);
        $this->User->setUserScore('hanako', 80);
        $this->User->setUserScore('sakura', 80);

        //increment
        $inc_score = $this->User->incrementScore('abesouri',3);
        $this->User->setUserScore('abesouri', $inc_score);

        //スコアを取得 150
        $data = $this->User->getScore('tarou');
        pr($data);


        //指定ユーザーの 順位 0 を取得 (自分のランク -1 になる)
        pr($this->User->getRevRank('jirou'));


        //1位から順にランクと点数を取得
        //        Array
        //        (
        //            [jirou] => 5000
        //            [user1] => 500
        //            [user3] => 300
        //            [user2] => 300
        //            [tarou] => 150
        //            [user4] => 100
        //            [sakura] => 80
        //            [hanako] => 80
        //        )
        pr($this->User->getRevRange(0, -1,true));


        //最下位から順位で取得
        //        Array
        //        (
        //    [jirou] => 40
        //    [hanako] => 80
        //    [sakura] => 80
        //    [tarou] => 150
        //)
        //pr($this->User->getRange(0, 3, true));

        //参加ユーザー数 4
        pr($this->User->countUsers());

        //参加ユーザー全削除
        //$this->User->deleteAllUser();

        //最下位から指定ユーザーの順位を取得
//        pr($this->User->getRank('tarou'));//3
//        pr($this->User->getRank('jirou'));//0
//        pr($this->User->getRank('hanako'));//1

簡単ですね。

スポンサードリンク

関連記事

関連カテゴリ

コロ助

web関連の記事や制作系の記事をどんどんまとめていきます。 宜しくお願いします!

ピックアップ

パソコン・ソフトウェア ランキング

7月21日 ( 金 ) にアクセスが多かった記事はこちら!