PHPackages                             yeszao/cache - 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. yeszao/cache

ActiveLibrary[Caching](/categories/caching)

yeszao/cache
============

General redis and memcache class

v1.0(7y ago)36465MITPHPPHP &gt;=5.4.0

Since May 30Pushed 7y ago2 watchersCompare

[ Source](https://github.com/yeszao/php-redis-cache)[ Packagist](https://packagist.org/packages/yeszao/cache)[ Docs](https://www.awaimai.com/)[ RSS](/packages/yeszao-cache/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

1 使用方法
------

[](#1-使用方法)

1. 使用composer下载 yeszao/cache 组件：

    ```
    composer install yeszao/cache

    ```
2. 在MVC框架`Model`层或者`Service`层的基类添加`__call()`方法，例如：

    ```
    namespace app\models;
    use yeszao\cache;

    class Base
    {
        public function __call($name, $arguments)
        {
            $redis = new \Redis();
            $redis->connect('redis');

            // ***主要是这一行***
            return (new Cache($redis))->get($this, $name, $arguments);
        }
    }

    ```

    其中，`yeszao\cache\Cache`对象创建时必须传入 Redis 连接句柄， 然后再通过`get`方法获取缓存。
3. 让子类继承`Base`类，例如：

    ```
    namespace app\models;
    use app\models\Base;

    class Book extends Base
    {
        public function getById($id)
        {
            return ['id' => 100, 'title' => 'PHP documents']
        }
    }

    ```
4. 然后就可以在`Controllers`类或其他`Model`类中使用：

    ```
    (new Book)->getById(100);           // 原始的、不用缓存的调用方式，一般是读取数据库的数据。
    (new Book)->getByIdCache(100);      // 使用缓存的调用方式，缓存键名为：app_models_book:getbyid: + md5(参数列表)
    (new Book)->getByIdClear(100);      // 删除这个缓存
    (new Book)->getByIdFlush();         // 删除 getById() 方法对应的所有缓存，即删除 app_models_book:getbyid:*。这个方法不需要参数。

    ```

2 构造函数参数
--------

[](#2-构造函数参数)

`yeszao\cache\Cache`构造函数参数：

- `$redis`: 必须。Redis连接对象。
- `$config`: 可选。缓存的一些配置。
    - `$config['prefix']`: 缓存键名前缀，默认空字符串。
    - `$config['expire']`：缓存过期时间，默认`3600`秒。
    - `$config['emptyExpire']`：原函数返回空值是的缓存过期时间，默认`10`秒。

3. 方法
-----

[](#3-方法)

除了构造函数，主要就是2个方法。

### 3.1 `get()`方法

[](#31-get方法)

获取缓存，如果未缓存，则调用原对象的原方法，拿到数据后保存到Redis中，并返回数据。 有3个参数，基本就是固定的：

- `$object`: 当前调用的类对象，传入`$this`。
- `$name`： 欲调用的方法名称，由`__call`自动获取后传入。
- `$arguments`: 欲调用的方法的参数，由`__call`自动获取后传入。

### 3.2 `expire()`方法

[](#32-expire方法)

设置缓存过期时间。 默认的缓存过期时间是`3600`秒，也可以用`expire()`方法动态设置。 只有1个参数：

- `$time`: 缓存过期时间，单位为秒。

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2907d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a5278d1bc1dc3f42f355d567b0faf9d9be3a5894c80f9f40cdbeb3d920ab821?d=identicon)[yeszao](/maintainers/yeszao)

---

Top Contributors

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

---

Tags

phprediscache

### Embed Badge

![Health badge](/badges/yeszao-cache/health.svg)

```
[![Health](https://phpackages.com/badges/yeszao-cache/health.svg)](https://phpackages.com/packages/yeszao-cache)
```

###  Alternatives

[awssat/laravel-visits

Laravel Redis visits counter for Eloquent models

975163.6k2](/packages/awssat-laravel-visits)[cache/redis-adapter

A PSR-6 cache implementation using Redis (PhpRedis). This implementation supports tags

523.9M27](/packages/cache-redis-adapter)[eftec/cacheone

A Cache library with minimum dependency

103.5k4](/packages/eftec-cacheone)

PHPackages © 2026

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