承接 mistralys/variable-hasher 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mistralys/variable-hasher

Composer 安装命令:

composer require mistralys/variable-hasher

包简介

PHP utility that can be used to create a hash of an arbitrary set of variables.

README 文档

README

PHP utility that can be used to create a hash of an arbitrary set of variables to use for runtime caching. It has better performance than serializing the variables and hashing the result, especially when objects and resources are involved.

Requirements

  • PHP 8.4 or higher
  • Composer for installation

Installation

Use Composer to add the dependency to your project:

composer require mistralys/variable-hasher

Quick start

Any kind of variable can be passed in, from primitives (int, float, string) to objects and resources, or arrays containing any of these - indexed or associative.

use Mistralys\VariableHasher\VariableHasher;

$intHash = VariableHasher::create(123)->getHash();

$multiHash = VariableHasher::create(123, 'string', 45.67, new stdClass())->getHash();

$array = array(
    'key' => 'value',
    'another_key' => 123,
    'object' => new stdClass()
);

$arrayHash = VariableHasher::create($array)->getHash();

Real-life example

Consider a factory method that creates objects based on a set of parameters. The same instances must be returned if the same parameters are passed in again.

use Mistralys\VariableHasher\VariableHasher;

class MyObjectFactory
{
    private array $instances = array();

    public function create(array $params) : MyObject
    {
        $hash = VariableHasher::create($params)->getHash();

        if(isset($this->instances[$hash])) {
            return $this->instances[$hash];
        }

        $instance = new MyObject($params);
        $this->instances[$hash] = $instance;

        return $instance;
    }
}

Order-independence

The order of the variables you pass in does not matter. The same hash will be generated regardless of the order if the variables are the same.

To illustrate, the following will generate the same hash:

use Mistralys\VariableHasher\VariableHasher;

$hash1 = VariableHasher::create('a', 'b', 'c')->getHash();
$hash2 = VariableHasher::create('c', 'b', 'a')->getHash();  

assert($hash1 === $hash2);

Choosing the hash algorithm

By default, MD5 is used to generate the hash. If you need a different algorithm, you can specify any valid algorithm supported by PHP's hash() function.

use Mistralys\VariableHasher\HashingAlgorithms;
use Mistralys\VariableHasher\VariableHasher;

$hash = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm(HashingAlgorithms::HASH_SHA3_256)
    ->getHash();

In the example above, the algorithm is set using one of the provided constants. You can specify any valid algorithm as a string:

use Mistralys\VariableHasher\VariableHasher;

$hash = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm('murmur3f') // requires `php-murmurhash` extension
    ->getHash();

NOTE: The HashingAlgorithms class is provided for convenience. It contains constants for all algorithms that you can rely on being supported by PHP 8.4 without having to check hash_algos().

Get the raw hashing string

If you want to see the raw string that is hashed, you can set the algorithm to none, which will return the string instead of a hash.

use Mistralys\VariableHasher\HashingAlgorithms;
use Mistralys\VariableHasher\VariableHasher;

$rawString = VariableHasher::create('a', 'b', 'c')
    ->setAlgorithm(HashingAlgorithms::HASH_NONE)
    ->getHash();

Memory handling

The arguments are stored internally until you request the hash. This means that if you pass in large variables, they will remain in memory until the object is destroyed if you do not request it.

Warning: Runtime use only

The hashing uses object and resource IDs for performance reasons, which are not persistent between requests. As a result, the generated hashes cannot be used to identify a set of variables if it contains objects or resources.

If you need a persistent hash, you should use a different method, such as encoding the objects to JSON and generating a hash of this serialized data.

mistralys/variable-hasher 适用场景与选型建议

mistralys/variable-hasher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mistralys/variable-hasher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mistralys/variable-hasher 我们能提供哪些服务?
定制开发 / 二次开发

基于 mistralys/variable-hasher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 134
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-02