Download the PHP package migears/data-structure without Composer

On this page you can find all versions of the php package migears/data-structure. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package data-structure

migears/data-structure

Version

A standalone Redis data structure service: explicit Hash / List / Set / Sorted Set (ZSet) semantics plus a distributed lock, targeting Redis-compatible servers (Redis, Valkey, KeyDB).

Sister package of migears/cache: migears/cache stays a pure PSR-16 key-value store, while this package exposes the data-structure layer. Both operate on the same Redis connection but keep separate interfaces and abstractions.

Background: miGears is the open-source successor of TinyGears, a self-developed PHP framework. It was renamed and open-sourced recently because the name TinyGears is already taken in the open-source community.

Features

Boundaries

In scope

Not in scope (by design)

Installation

The Redis implementations require the redis extension: pecl install redis

Handling the Redis Connection

The classes never connect to Redis themselves. They only wrap a connection you provide, so the connection — and its life-cycle — stays with the caller:

In a miGears web environment, inject the connection in MiRest and obtain it through the service registry:

Value Semantics

Values are scalars only (int|float|string). There is no transparent serialization of arrays or objects:

Store anything more complex through the base PSR-16 cache (migears/cache) instead.

Quick Start

Distributed lock

unlock() is safe: it releases only when the key still holds this instance's owner token (Lua compare-and-delete). A lock that expired and was re-acquired is never deleted by a stale holder.

Key prefix — namespacing

Share one Redis instance across apps/modules without key collisions:

API Reference

Hash

Method Returns Description
hashSet($key, $field, $value) bool Set the value of a field
hashGet($key, $field) int\|float\|string\|null Get a field's value (null when missing)
hashMultiGet($key, $fields) array Get several fields, existing ones only
hashGetAll($key) array Get all fields of the hash
hashDel($key, $field\|fields) int Delete one or more fields
hashExists($key, $field) bool Check whether a field exists
hashLen($key) int Number of fields
hashIncrBy($key, $field, $increment = 1) int Atomic increment of a numeric field

List

Method Returns Description
listPush($key, ...$values) int Push value(s) to the tail (rPush), returns new length
listPop($key) string\|null Pop from the head (lPop), null when empty
listRange($key, $start = 0, $end = -1) array Range of elements (supports negative indexes)
listLen($key) int Length of the list
listIndex($key, $index) string\|null Element at a given index (negates count from tail), null if out of range

Set

Method Returns Description
setAdd($key, $member\|members) int Add one or more members, returns count added
setRemove($key, $member\|members) int Remove one or more members, returns count removed
setMembers($key) array All members
setSize($key) int Number of members
setIsMember($key, $member) bool Check membership

Sorted Set (ZSet)

Method Returns Description
zAdd($key, $score, $member) int Add a member with a score
zRemove($key, $member\|members) int Remove one or more members
zSize($key) int Number of members
zScore($key, $member) int\|float\|null Score of a member, null when missing
zRange($key, $start = 0, $end = -1) array Members ordered by score ascending (by index range)
zSelect($key, $min = 0, $max = 9999999999, $limit = 0, $order = 'DESC') array Members filtered by score bounds and sorted; $limit 0 = unlimited
zBatchAdd($key, $set) bool Batch add from a flat [score, member, score, member, ...] array
zInterStore($destKey, $keys, $aggregate = 'MIN') int Intersection of multiple ZSets into $destKey
zIncrBy($key, $member, $increment = 1) int\|float Atomic score increment

$min / $max are inclusive score bounds. zSelect returns scores as float, keys ordered by score (default DESC), and $order is case-insensitive — 'ASC' and 'DESC' only: anything else raises DataStructureException instead of being read as DESC.

The default bounds form a bounded window (0 to 9999999999), so members scored below 0 or above that ceiling are not returned; pass explicit bounds to widen it. zBatchAdd answers false only for an empty list, i.e. nothing to do: an odd number of entries, or a command that fails inside the pipeline, raises DataStructureException instead of being flattened into the same false. It uses a pipeline rather than a transaction, so a failing pair leaves the pairs before it applied.

Key level (TTL)

Method Returns Description
ttl($key) int Remaining TTL in seconds; -1 = no expiry, -2 = key missing
expire($key, $ttl) bool Set a TTL in seconds; a non-positive value deletes the key
persist($key) bool Remove the expiry

Error Handling & Logging

Every operation is wrapped in try/catch: underlying exceptions are logged through the injected PSR-3 logger and re-thrown as a MiGears\DataStructure\Exception\DataStructureException. The logger is required — the class substitutes no NullLogger of its own, so a call site that forgets it fails at assembly time:

When the client reports a failure without throwing — which phpredis does by answering false — methods that return a count or a score raise DataStructureException rather than letting an impossible false reach the return type, or quietly turning into 0 behind a cast. Methods that answer a yes/no question (hashExists, setIsMember, expire, persist) pass the client's false through, because there it means "no" rather than "failure".

Testing

The integration suite auto-resolves a connection (first match wins) and starts a throwaway container if none is present:

If none is available, integration tests are skipped.

The resolved database is flushed. Every integration test method calls flushDB(), so point these variables at a dedicated test database only. A database that is named is used exactly as given, including db 0 (redis://host:6379/0 or REDIS_DB=0); when no database is named the suite falls back to db 15.

License

MIT


migears/data-structure

Version

一个独立的 Redis 数据结构服务:显式的 Hash / List / Set / 有序集合(ZSet) 语义,外加一个分布式锁,面向 Redis 兼容服务器(Redis、Valkey、KeyDB)。

它是 migears/cache 的姊妹包:migears/cache 保持纯粹的 PSR-16 键值缓存,本包则提供数据结构层。二者共用同一条 Redis 连接,但保持独立的接口与抽象。

背景:miGears 源自自研 PHP 框架 TinyGears,因 TinyGears 这一名字 已被开源社区占用,故近期更名并开源发布。

特性

边界

范围内

范围外(刻意不做)

安装

Redis 实现需要 redis 扩展:pecl install redis

连接的建立

这些类自身不会连接 Redis。它们只包装你传入的连接,连接及其生命周期由调用方掌控:

在 miGears 的 web 环境中,把连接注入 MiRest,再经服务注册中心取得:

取值语义

值为仅标量(int|float|string),不对数组或对象做透明序列化:

如需存储更复杂的结构,请改用基础 PSR-16 缓存(migears/cache)。

快速开始

分布式锁

unlock() 是安全的:只有当 key 仍持有本实例的属主令牌时才会释放(Lua 比较后删除)。已过期并被重新获取的锁,绝不会被陈旧的持有者误删。

键前缀——命名空间

多应用/模块共用一条 Redis 时避免键冲突:

API 参考

Hash

方法 返回 说明
hashSet($key, $field, $value) bool 设置某个字段的值
hashGet($key, $field) int\|float\|string\|null 读取某个字段的值(不存在返回 null)
hashMultiGet($key, $fields) array 批量读取字段,仅返回已存在的
hashGetAll($key) array 读取哈希的全部字段
hashDel($key, $field\|fields) int 删除一个或多个字段
hashExists($key, $field) bool 字段是否存在
hashLen($key) int 字段数量
hashIncrBy($key, $field, $increment = 1) int 数值字段原子自增

List

方法 返回 说明
listPush($key, ...$values) int 向尾部入队(rPush),返回新长度
listPop($key) string\|null 从头部出队(lPop),空时返回 null
listRange($key, $start = 0, $end = -1) array 元素区间(支持负索引)
listLen($key) int 列表长度
listIndex($key, $index) string\|null 指定索引处的元素(负值从尾部计数),越界返回 null

Set

方法 返回 说明
setAdd($key, $member\|members) int 添加一个或多个成员,返回新增数量
setRemove($key, $member\|members) int 删除一个或多个成员,返回删除数量
setMembers($key) array 全部成员
setSize($key) int 成员数量
setIsMember($key, $member) bool 是否包含某成员

有序集合(ZSet)

方法 返回 说明
zAdd($key, $score, $member) int 添加一个带分数的成员
zRemove($key, $member\|members) int 删除一个或多个成员
zSize($key) int 成员数量
zScore($key, $member) int\|float\|null 某成员的分数,不存在返回 null
zRange($key, $start = 0, $end = -1) array 按分数升序返回索引区间内的成员
zSelect($key, $min = 0, $max = 9999999999, $limit = 0, $order = 'DESC') array 按分数区间筛选并排序;$limit 为 0 表示不限条数
zBatchAdd($key, $set) bool 批量添加,$set 为扁平数组 [score, member, score, member, ...]
zInterStore($destKey, $keys, $aggregate = 'MIN') int 多个 ZSet 求交集写入 $destKey
zIncrBy($key, $member, $increment = 1) int\|float 分数原子自增

$min / $max 为闭区间分数边界。zSelect 返回的分数均为 float,默认按分数降序,$order 不区分大小写——只接受 'ASC' 与 'DESC';其他取值会抛出 DataStructureException,而不是被当作 DESC。

默认边界构成一个有界窗口(0 到 9999999999):分数低于 0 或高于该上界的成员不会被返回,需要更宽的区间请显式传参。zBatchAdd 仅在传入空数组(无事可做)时返回 false:元素个数为奇数、或 pipeline 内某条命令失败,都会抛出 DataStructureException,而不是被压成同一个 false。它使用 pipeline 而非事务,因此某一对失败时前面已生效的成员会保留。

Key 级(TTL)

方法 返回 说明
ttl($key) int 剩余 TTL(秒);-1 = 无过期,-2 = key 不存在
expire($key, $ttl) bool 设置以秒为单位的过期时间;非正值会删除该键
persist($key) bool 移除过期时间

错误处理与日志

每个操作都用 try/catch 包裹:底层异常会通过注入的 PSR-3 日志器记录,然后以 MiGears\DataStructure\Exception\DataStructureException 重新抛出。日志器为必填——类自身不会用 NullLogger 兜底,忘记传入会在装配期失败:

当客户端不抛异常、而以 false 表示失败时(phpredis 的做法):返回计数或分数的方法会抛出 DataStructureException,而不是让一个不可能的 false 落到返回类型上,也不再经由强制转换悄悄变成 0。返回是非判断的方法(hashExists、setIsMember、expire、persist)会原样透传客户端的 false,因为在那里它表示「否」而不是「失败」。

测试

集成套件会自动解析连接(按顺序取首个可用),若无可用连接则会自动拉起一个临时容器:

若均不可用,集成测试会被跳过。

被解析出的库会被清空。 每个集成测试方法都会调用 flushDB(),因此请只把这些变量指向专用的测试库。显式指定的库会按原值使用,包括 db 0(redis://host:6379/0 或 REDIS_DB=0);未指定库时回退到 db 15。

License

MIT


All versions of data-structure with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
psr/log Version ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package migears/data-structure contains the following files

Loading the files please wait ...