PHPackages                             lysice/laravel-smartcache - 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. lysice/laravel-smartcache

ActiveLibrary[Caching](/categories/caching)

lysice/laravel-smartcache
=========================

a secondary cache tool for laravel

v1.6.4(4y ago)385MITPHPPHP &gt;=7.0

Since May 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Lysice/laravel-smartcache)[ Packagist](https://packagist.org/packages/lysice/laravel-smartcache)[ RSS](/packages/lysice-laravel-smartcache/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (5)Versions (11)Used By (0)

Laravel-smartcache 一款基于yac/apcu的laravel二级缓存扩展包
----------------------------------------------

[](#laravel-smartcache-一款基于yacapcu的laravel二级缓存扩展包)

[![Latest Stable Version](https://camo.githubusercontent.com/8db31c7aeb4ef5547527a22ad9a579a7ccba2e6c5e1d4f5ab9ff3178ccf7ff54/68747470733a2f2f706f7365722e707567782e6f72672f4c79736963652f6c61726176656c2d736d61727463616368652f762f737461626c65)](https://packagist.org/packages/Lysice/laravel-smartcache)[![Total Downloads](https://camo.githubusercontent.com/7e1b81f3ba607b06ff0444f50486baceb72ff29a348db773b4d00f528753f02a/68747470733a2f2f706f7365722e707567782e6f72672f4c79736963652f6c61726176656c2d736d61727463616368652f646f776e6c6f616473)](https://packagist.org/packages/Lysice/laravel-smartcache)[![Latest Unstable Version](https://camo.githubusercontent.com/f953ef38bb06362ec025218d79da2d5a652a3e2771683ce26fef8f8d412095e5/68747470733a2f2f706f7365722e707567782e6f72672f4c79736963652f6c61726176656c2d736d61727463616368652f762f756e737461626c65)](https://packagist.org/packages/Lysice/laravel-smartcache)[![License](https://camo.githubusercontent.com/6ff5177d9d4c5eb70d040042623db350f3ef782771a7e9a27a9920d08f75e207/68747470733a2f2f706f7365722e707567782e6f72672f4c79736963652f6c61726176656c2d736d61727463616368652f6c6963656e7365)](https://packagist.org/packages/Lysice/laravel-smartcache)

二级缓存器，基于APCu/Yac。

#### 如果本扩展帮助到了你 欢迎star。[git](https://github.com/Lysice/laravel-smartcache)

[](#如果本扩展帮助到了你-欢迎stargit)

#### 如果本扩展有任何问题或有其他想法 欢迎提 issue与pull request。

[](#如果本扩展有任何问题或有其他想法-欢迎提-issue与pull-request)

[Yac官方文档](https://www.php.net/manual/zh/book.yac)

[APCu官方文档](https://www.php.net/manual/zh/book.apcu)

#### 安装

[](#安装)

```
    composer require lysice/laravel-smartcache

```

将服务提供者添加入`app.php`

```
    'providers' => [
    ...
    \Lysice\Cache\CacheServiceProvider::class
    ]

```

#### 配置

[](#配置)

- `data_connection` Redis的数据连接,指定要同步数据到哪个Redis连接。
- `cache_type`内存缓存选择两种模式

```
  \Lysice\Cache\Constants::CACHE_TYPE_YAC 基于Yac 该选项需要安装php的yac扩展
  \Lysice\Cache\Constants::CACHE_TYPE_APCU 基于APCu 该选项需要安装apcu的扩展

```

- `sync_mode` 同步模式

```
    const SYNC_MODE_PUBSUB = 1; 异步订阅模式， 使用Redis的订阅来同步数据到Redis 该选项需要执行`php artisan 2cache:sync &` 且保证该命令高可用。
    const SYNC_MODE_SYNC = 2;   同步模式，当设置缓存时直接设置Redis数据。
    const SYNC_MODE_JOB = 3;    队列模式 将Redis的缓存设置任务分配到队列 该模式需要您开启队列且保证队列高可用。一般使用`supervisor`

```

- `pub_connection`Redis的订阅连接 当同步模式为`SYNC_MODE_PUBSUB`时使用。注意，Redis的订阅连接为阻塞连接 需要保证数据连接`data_connection`与`pub_connection`不是同一个连接。 这种模式的缺点是会浪费一个`Redis`的`DB`
- `redis_channel`Redis的订阅渠道 当同步模式为`SYNC_MODE_PUBSUB`时使用。
- `log` 是否记录简单日志

#### 使用

[](#使用)

`CacheManager`的方法定义如下

```
remember(string $key, int $ttl, Callable $callback){}

```

如果您习惯使用`Facade`模式 首先加入Facade

```
    'aliases' => [
            'SecondaryCache' => \Lysice\Cache\SecondaryCache::class
        ],

```

然后使用:

```
        $result = SecondaryCache::remember('your key', $ttl, function () {
            return  [
                'cached' => true
            ];
        });

```

或者您也可以直接按照如下使用

```
        app(CacheManager::class)->remember('your key', $ttl, function () {
            return  [
                'cached' => 1
            ];
        });

```

需要注意的是 首次返回的数据为您自己定义的 `$callback`中的数据。如果您返回的是数组 则缓存时会将数据 `json_encode`后存储。 当您第二次访问，取到的数据为缓存数据，此时您应该将数据反序列化为数组。 另外，在laravel中大量使用了 collection，由于collection数据量太大，因此在开发中并未考虑`callback`返回`collection`的情况。建议使用者直接返回数组。

#### 单独使用

[](#单独使用)

如果您觉得本扩展的remember方法不好用可以基于本扩展提供的类直接操作。 以下本扩展提供的类

- RedisInstance
- YacInstance
- APCu 以上三个类实现了`CacheConcern`接口 因此提供方法:

```
/**
    public function set($key, $value, $ttl); 设置缓存 支持数组。$key = ['key' => 'value'] $value = null

    public function update($key, $old, $new); 更新缓存

    public function clear(); 清除缓存

    public function decrease($key = '', $step = 1, $ttl = 0); 计数减少

    public function delete($keys); 删除缓存

    public function exists($keys); 是否存在

    public function get($key); 获取缓存

    public function increase($key = '', $step = 1, $ttl = 0); 计数增加

    public function info(); 返回缓存信息

```

#### 如果本扩展帮助到了你 欢迎star。

[](#如果本扩展帮助到了你-欢迎star)

#### 如果本扩展有任何问题或有其他想法 欢迎提 issue与pull request。

[](#如果本扩展有任何问题或有其他想法-欢迎提-issue与pull-request-1)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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

Every ~14 days

Recently: every ~25 days

Total

10

Last Release

1695d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f077a192556cd9c34d866d3a65c0e4ba9d7f3b06403e431f58fd975ac3b7ed9?d=identicon)[Lysice](/maintainers/Lysice)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/lysice-laravel-smartcache/health.svg)

```
[![Health](https://phpackages.com/badges/lysice-laravel-smartcache/health.svg)](https://phpackages.com/packages/lysice-laravel-smartcache)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M682](/packages/barryvdh-laravel-ide-helper)[laravel/wayfinder

Generate TypeScript representations of your Laravel actions and routes.

1.7k4.1M69](/packages/laravel-wayfinder)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[laravel/folio

Page based routing for Laravel.

608453.9k27](/packages/laravel-folio)

PHPackages © 2026

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