PHP Classes

How to Implement a PHP Hash Table Using the Package Consistent Hashing: Store values in a table using consistent hashing

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-12-09 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 5 This week: 5All time: 11,501 This week: 11Up
Version License PHP version Categories
consistent-hash 1.1.0MIT/X Consortium ...8.2.0Data types, PHP 8
Description 

Author

This package can store values in a table using consistent hashing.

It provides a class to add or remove target values as entries to a consistent hash table based on the flexihash package.

The class can also look for hash table entries with a given search value. It also supports lookup for values that partially match the search value.


Picture of Eric Sizemore
  Performance   Level  
Name: Eric Sizemore <contact>
Classes: 17 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 5x

Winner: 2x

Details

ConsistentHash

Build Status Code Coverage Scrutinizer Code Quality Continuous Integration Type Coverage Psalm Level Latest Stable Version Downloads per Month License

Esi\ConsistentHash is a small PHP library which implements consistent hashing, which is most useful in distributed caching.

> [!NOTE] > This library is a fork of https://github.com/pda/flexihash v3.0.0. The original library has not seen any releases since 2020.

Installation

You can install the package via composer:

$ composer require esi/consistent-hash

Usage

use Esi\ConsistentHash;

$hash = new ConsistentHash();

// bulk add
$hash->addTargets(['cache-1', 'cache-2', 'cache-3']);

// simple lookup
$hash->lookup('object-a'); // "cache-1"
$hash->lookup('object-b'); // "cache-2"

// add and remove
$hash->addTarget('cache-4');
$hash->removeTarget('cache-1');

// lookup with next-best fallback (for redundant writes)
$hash->lookupList('object', 2); // ["cache-2", "cache-4"]

// remove cache-2, expect object to hash to cache-4
$hash->removeTarget('cache-2');
$hash->lookup('object'); // "cache-4"

Benchmarks

PHPBench

Performance can be tested with PHPBench.

$ git clone https://github.com/ericsizemore/consistent-hash.git
$ cd consistent-hash
$ ./vendor/bin/phpbench run --report=aggregate --iterations=4 --tag=branch_main

Or via composer:

$ composer run-script benchmark

There is also an option to view results as a bar chart:

$ composer run-script benchmark:chart

With opcache enabled:

# Normal, aggregate report
$ composer run-script benchmark:opcache

# Bar chart
$ composer run-script benchmark:chart:opcache

PHPUnit

Benchmarks are also available through PHPUnit, though it is a bit more rudimentary. To see these benchmarks, run:

$ composer run-script phpunit:benchmark

About

Requirements

  • PHP >= 8.2

Credits

And thanks to the library this is a fork of, flexihash/flexihash:

Contributing

See CONTRIBUTING.

Bugs and feature requests are tracked on GitHub.

Contributor Covenant Code of Conduct

See CODE_OF_CONDUCT.md

Backward Compatibility Promise

See backward-compatibility.md for more information on Backwards Compatibility.

Changelog

See the CHANGELOG for more information on what has changed recently.

License

See the LICENSE for more information on the license that applies to this project.

Roadmap/TODO

See the ROADMAP for more information what is currently being planned.

Security

See SECURITY for more information on the security disclosure process.


  Files folder image Files (37)  
File Role Description
Files folder image.github (2 files, 2 directories)
Files folder imagesrc (1 file, 2 directories)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file .php-cs-fixer.dist.php Example Example script
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file backward-compatibility.md Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file CODE_OF_CONDUCT.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpbench.json Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file psalm.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file renovate.json Data Auxiliary data
Accessible without login Plain text file ROADMAP.md Data Auxiliary data
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files (37)  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (3 files)
Files folder imageworkflows (2 files)
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file pull_request_template.md Data Auxiliary data

  Files folder image Files (37)  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file 1-bug_report.yml Data Auxiliary data
  Accessible without login Plain text file 2-feature_request.yml Data Auxiliary data
  Accessible without login Plain text file config.yml Data Auxiliary data

  Files folder image Files (37)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file continuous-integration.yml Data Auxiliary data
  Accessible without login Plain text file release.yml Data Auxiliary data

  Files folder image Files (37)  /  src  
File Role Description
Files folder imageException (1 file)
Files folder imageHasher (6 files)
  Plain text file ConsistentHash.php Class Class source

  Files folder image Files (37)  /  src  /  Exception  
File Role Description
  Plain text file TargetException.php Class Class source

  Files folder image Files (37)  /  src  /  Hasher  
File Role Description
  Plain text file Crc32Hasher.php Class Class source
  Plain text file Fnv1AHasher.php Class Class source
  Plain text file HasherInterface.php Class Class source
  Plain text file Md5Hasher.php Class Class source
  Plain text file Murmur3Hasher.php Class Class source
  Plain text file Xxh32Hasher.php Class Class source

  Files folder image Files (37)  /  tests  
File Role Description
Files folder imageBench (1 file)
Files folder imageHasher (2 files)
  Plain text file BenchmarkTest.php Class Class source
  Plain text file ConsistentHashTest.php Class Class source

  Files folder image Files (37)  /  tests  /  Bench  
File Role Description
  Plain text file LookupBench.php Class Class source

  Files folder image Files (37)  /  tests  /  Hasher  
File Role Description
  Plain text file HasherTest.php Class Class source
  Plain text file MockHasher.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:5
This week:5
All time:11,501
This week:11Up