PHPackages                             jamespi/php-redis - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. jamespi/php-redis

ActiveLibrary[Caching](/categories/caching)

jamespi/php-redis
=================

php基于redis实现分布式锁、分布式缓存及对redis客户端api操作封装

58PHP

Since Nov 24Pushed 4y ago1 watchersCompare

[ Source](https://github.com/jamespjz/php-redis)[ Packagist](https://packagist.org/packages/jamespi/php-redis)[ RSS](/packages/jamespi-php-redis/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

PP分布式锁(php-redis-lock)
======================

[](#pp分布式锁php-redis-lock)

php基于redis实现分布式锁、分布式缓存及对redis客户端api操作封装

> 使用版本说明：Redis v5.0.5 - PHP &gt;=7.0 - Composer v1.10

简要说明：
=====

[](#简要说明)

公司目前正在全面转微服务架构，为了让PHPER在分布式高并发场景下保证数据的准确性，特封装了含有redis分布式锁、分布式缓存及redis操作API功能的插件，目前为version 0.1-dev。

分布式锁具备以下四个特性：
=============

[](#分布式锁具备以下四个特性)

- 互斥性（一把锁只能加一次）
- 原子性
- 防止死锁产生（被客户端锁住用不释放，其他客户端没法使用）
- 同一个客户端只能解锁自己加的锁

功能简介：
=====

[](#功能简介)

- 获取锁
- 删除锁
- redis(key、string、hash、list、set)API
- 发布/订阅
- 分布式缓存
- 其余特性参考

部署安装
====

[](#部署安装)

- github下载

```
git clone https://github.com/jamespjz/php-redis.git

```

已经加入对composer支持，根目录下有个composer.json，请不要随意修改其中内容如果你不明白你在做什么操作。

- composer下载

```
composer require jamespi/php-redis dev-master

```

分布式锁使用方式
========

[](#分布式锁使用方式)

> 创建锁实例

```
$servers = [
    ['192.168.87.54', '6379', 3, 1.5],
    ['192.168.87.56', '6379', 3, 1.5],
    ['192.168.87.194', '6379', 3, 1.5],
    'auth' => '123456'
];
new RedisLock($servers)

```

> 获取redis分布式锁例子

```
$params = [
    'token_key' => 'redis_key',
    'acquire_number' => 3,
    'requests_number' => 3,
    'lock_timeout' => 300 //单位s
];
(new RedisLock($servers))->acquireLock($params);

```

> 释放redis分布式锁例子

```
$params = [
    'token_key' => 'redis_key',
    'identifier' => '4a3068a14f90554383dcaedf59c367a3',
];
(new RedisLock($servers))->unlock($params);

```

> 检测分布式锁key是否已被定义例子

```
$params = [
    'token_key' => 'redis_key'
];
(new RedisLock($servers))->isLock($params);

```

分布式缓存使用方式
=========

[](#分布式缓存使用方式)

> 调用redis分布式缓存例子

```
//获取分布式缓存
use Jamespi\Redis\Start;
$type = 3;//1：redis客户端Api 2：分布式锁 3：分布式缓存
$config = [
    'cache' => [
        'host' => '192.168.109.54',
        'port' => 7002,
        'auth' => '123456',
        'redis_setting' => 2 //redis环境（1：单机 2：集群）
    ],
    'mysql' => [
        //redis缓存持久化时插件回调接入方持久化(查询、删除)方法
        'namespace' => 'App\IndexController',
        'action' => 'test_select', //select表：test_select；add表：test； delete表：test_delete
    ]
];
/****  更新缓存 ****/
//string时msg为字符串；hash、list、set、sorted_set时msg为json_encode；
$params = [
    'cache_mode' => 1,//(目前只支持Aside模式)缓存更新模式 >> 1：Cache Aside模式 2：Through模式 3：Write Back模式
    'key' => 'token_key', //缓存key
    'msg' => '4a3068a14f90554383dcaedf59c367a3', //缓存key值
    'type' => 'string' //数据类型：string、hash、list、set、sorted_set
];
//echo (new Start())->run($type, $config)->write($params);
/****  读取缓存 ****/
$params_read = [
    'cache_mode' => 1,//(目前只支持Aside模式)缓存更新模式 >> 1：Cache Aside模式 2：Through模式 3：Write Back模式
    'key' => 'token_key', //缓存key
    'field' => 'field', //缓存二级key
    'msg' => '4a3068a14f90554383dcaedf59c367a3', //缓存key值
    'type' => 'string', //数据类型：string、hash、list、set、sorted_set
    'cache_timeout' => 50 //缓存失效时间（为防止缓存雪崩尽量时间采用基于时间段随机时间）
];
echo (new Start())->run($type, $config)->read($params_read);

```

***注意：配置数组的下标键名是约定好的，请不要定制个性化名称，如果不想系统报错或系统使用默认配置参数而达不到您想要的结果的话***

联系方式
====

[](#联系方式)

- wechat：james-pi
- email：

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity28

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/34019532?v=4)[jamespjz](/maintainers/jamespjz)[@jamespjz](https://github.com/jamespjz)

---

Top Contributors

[![hnust](https://avatars.githubusercontent.com/u/13633566?v=4)](https://github.com/hnust "hnust (5 commits)")

### Embed Badge

![Health badge](/badges/jamespi-php-redis/health.svg)

```
[![Health](https://phpackages.com/badges/jamespi-php-redis/health.svg)](https://phpackages.com/packages/jamespi-php-redis)
```

###  Alternatives

[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21114.2k](/packages/iazaran-smart-cache)[phpfastcache/phpssdb

PHP SSDB Driver for phpFastCache

14736.9k1](/packages/phpfastcache-phpssdb)[rapidwebltd/rw-file-cache

RW File Cache is a PHP File-based Caching Library. Its syntax is designed to closely resemble the PHP memcache extension.

15196.8k7](/packages/rapidwebltd-rw-file-cache)[yuzuru-s/redis-recommend

Wrapping Redis's sorted set APIs for specializing recommending operations.

392.9k](/packages/yuzuru-s-redis-recommend)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
