CI project design Redis queue

Source: Internet
Author: User

During project development, you must design a queue that provides a balance to process multiple user requests.

Requirements:

After a user logs on, view the logged-on administrator queue in the system, and then view the processing capability of the background administrator. If new requests cannot be processed, delete the administrator from the queue, otherwise

This administrator is assigned to this user. The Administrator's processing capability is reduced by one. The Administrator + 1 currently directs to the next administrator who can handle the problem.

Analysis:

The simplest design is to maintain the queue of a circular linked list, which can easily Delete and modify information. However, Redis cannot process the copied structure information. Therefore, we can only find a new path.

Double structure to convert the circular linked list structure. First look at the original structure:


This structure can easily handle problems, but it is not easy to store such a structure in redis. Therefore, we consider using the user ID to create a queue list and then using the user ID to create a queue list (key, value), of course, you can also

The user information creates a list with ID as the KEY. After such a list structure conversion, Redis can process the original complex structure, first check the element of the main queue column, and then

Key to obtain other complex information for processing. The structure is as follows:


Such a structure, no matter whether to delete or add, must operate on two places... in exchange for the complexity of the storage with the complexity of the operation, may not be well designed, but first to implement the function.

Redis class:

<?phpif (!defined('BASEPATH'))    exit('No direct script access allowed');class Redisdb {    private $size ;    private $redis ;    private $channel_queue;    private $current_index ;    public function __construct() {        $this->size = 100;        $this->redis = new Redis();        $this->channel_queue = 'staffs';        $this->redis->connect('127.0.0.1', '6379');        $this->set_index();    }    public function set_index($index=0){        $this->redis->set('current_index',$index);    }    public function en_queue($key,$value) {             return  $this->redis->rpush($this->channel_queue, $key) && $this->redis->set($key,$value);;    }    public function is_empty(){       return  $this->redis->lsize('admins')<=0;    }    public function is_full(){        return $this->redis->lsize($this->channel_queue) >= $this->size;    }    public function remove($value){        return $this->redis->lrem($this->channel_queue,$value);    }    public function get_list(){        return $this->redis->lrange($this->channel_queue,0,-1);    }    public function delete_key($key){        return $this->redis->delete($key);    }    public function get_value($key){        return  $this->redis->get($key);    }  public function allocate_admin(){$index = $this->redis->get('current_index');        $size  = $this->redis->lsize('admins');if($size ==0){return false;}        if($index<$size){            $key =  $this->redis->lindex('staffs',$index);            if($this->redis->get($key)<=1){               $this->remove($key);               return $key;            }else{$this->redis->decr($key);$this->redis->incr('current_index');        return $key ;                            }                     }else{    $this->redis->set('current_index',0);            $this->allocate_admin();        }}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.