项目作者: sokil

项目描述 :
Priority list
高级语言: PHP
项目地址: git://github.com/sokil/php-list.git
创建时间: 2014-11-23T18:29:29Z
项目社区:https://github.com/sokil/php-list

开源协议:MIT License

下载


Lists

Build Status
Latest Stable Version
Coverage Status
Total Downloads
Daily Downloads

Installation

You can install library through Composer:

  1. {
  2. "require": {
  3. "sokil/php-list": "dev-master"
  4. }
  5. }

Priority Map

Priority map allows you to specify priority of items and
iterate through this list in order to priority.

Add elements to list with priority:

  1. <?php
  2. $list = new \Sokil\DataType\PriorityMap();
  3. $list->set('key1', 'value1', 10);
  4. $list->set('key2', 'value2', 100);

Get elements according to priority:

  1. <?php
  2. foreach($list as $key => $value) {
  3. echo $key . ' - ' . $value;
  4. }
  5. // this will print
  6. // key2 - value2
  7. // key1 - value1

Get element by key:

  1. <?php
  2. $list = new \Sokil\DataType\PriorityMap();
  3. $list->set('key1', 'value1', 10);
  4. $list->get('key1');

Weight List

Weight list allows you to specify values and relative weights, and randomly
get value according to it’s weight.

Imagine that we have three database servers with ip addresses: 10.0.0.1, 10.0.0.2 and 10.0.0.3.
We want to balance connections between nodes with weights 60%, 30% and 10%. So
most connections goes to server 10.0.0.1, than to 10.0.0.2 and than to 10.0.0.3.

  1. <?php
  2. $list = new \Sokil\DataType\WeightList([
  3. '10.0.0.1' => 60,
  4. '10.0.0.2' => 30,
  5. '10.0.0.3' => 10,
  6. ]);
  7. $ipAddress = $list->getRandomValue();

Now we have address on every request relatively to it’s weight.