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

ActiveLibrary[Caching](/categories/caching)

windwork/cache
==============

Windwork cache component

1.2.0(8y ago)0842MITPHPPHP &gt;=5.5.0

Since Jun 18Pushed 8y agoCompare

[ Source](https://github.com/windwork/wf-cache)[ Packagist](https://packagist.org/packages/windwork/cache)[ RSS](/packages/windwork-cache/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (3)DependenciesVersions (4)Used By (2)

Windwork 缓存组件
=============

[](#windwork-缓存组件)

服务器端缓存是将程序需要频繁访问的网络内容存放在本地文件或缓存服务器中，缓存过期前直接从缓存中读取内容，以提高访问速度。 如频繁访问但不经常更新的一些数据库查询、微信接口令牌等。

Windwork 缓存组件提供简易健壮的缓存组件，5分钟即可完全掌握。 目前支持文件缓存、Memcache、Memcached、Redis缓存存储。

安装
--

[](#安装)

该组件已包含在Windwork框架中，如果你已安装Windwork框架则可以直接使用。

- 安装方式一：通过composer安装（推荐）

```
composer require windwork/wf

```

- 安装方式二：传统方式安装
    [下载源码](https://github.com/windwork/wf/releases)后，解压源码到项目文件夹中，然后require\_once $PATH\_TO\_WF/core/lib/Loader.php文件，即可自动加载组件中的类。

缓存读写
----

[](#缓存读写)

默认使用文件缓存

```
// 缓存参数
$cfg = [
    'enabled'    => 1,             // 是否启用缓存
    'dir'        => 'data/cache',  // 缓存文件夹，如果使用缓存服务器，则是缓存变量的前缀
    'expire'     => 3600,          // 缓存更新周期(默认：3600s)
    'compress'   => 0,             // 是否启用缓存内容压缩后存贮
    'class'    => 'File',        // 缓存模式，File）文件缓存；Memcache）使用Memcache缓存；Memcached）使用Memcached缓存；Redis）使用Redis缓存
];
$class = "\\wf\\cache\\adapter\\{$cfg['class']}";
$cache = new $class($cfg);

// 从缓存读取数据
if(null === ($ret = $cache->read('cache/key'))) {
    // 缓存中无数据则初始化并写入缓存，下次就可以直接从缓存中获取
    $ret = '缓存内容'; // 可以是标量或数组内容，不能是资源类型
    $cache->write('cache/key', $ret);
}

// 在这里使用缓存过的$ret变量

```

删除缓存内容
------

[](#删除缓存内容)

```
// 删除一条缓存
$cache->delete('cache/key');

// 清空全部缓存
$cache->clear();

// 通过前缀清空缓存
$cache->clear('user/info'); // 清空所有以 'user/info/' 开头的缓存内容

```

使用memcache/memcached缓存
----------------------

[](#使用memcachememcached缓存)

```
// Memcache
$cfg = [
    'enabled'    => 1,             // 是否启用缓存
    'dir'        => 'data/cache',  // 缓存文件夹，如果使用缓存服务器，则是缓存变量的前缀
    'expire'     => 3600,          // 缓存更新周期(默认：3600s)
    'compress'   => 0,             // 是否启用缓存内容压缩后存贮
    'class'    => '\\wf\\cache\\adapter\\Memcache',        // 缓存模式，File）文件缓存；Memcache）使用Memcache缓存；Memcached）使用Memcached缓存；Redis）使用Redis缓存

    ['memcache'] => [
        'host'        => '127.0.0.1',     //
        'port'        => 11211,           //
        'pconnect'    => 1,               //
        'timeout'     => 1,               // 超时时间（秒）
    ],
];
$class = "\\wf\\cache\\adapter\\{$cfg['class']}";
$cache = new $class($cfg);

// Memcached
$cfg = [
    'enabled'    => 1,             // 是否启用缓存
    'dir'        => 'data/cache',  // 缓存文件夹，如果使用缓存服务器，则是缓存变量的前缀
    'expire'     => 3600,          // 缓存更新周期(默认：3600s)
    'compress'   => 0,             // 是否启用缓存内容压缩后存贮
    'class'      => '\\wf\\cache\\adapter\\Memcached',        // 缓存模式，File）文件缓存；Memcache）使用Memcache缓存；Memcached）使用Memcached缓存；Redis）使用Redis缓存

    ['memcached'] => [
        'host'        => '127.0.0.1',     //
        'port'        => 11211,           //
        'pconnect'    => 1,               //
        'timeout'     => 1,               // 超时时间（秒）
    ],
];

$class = "\\wf\\cache\\adapter\\{$cfg['class']}";
$cache = new $class($cfg);

```

使用Redis缓存
---------

[](#使用redis缓存)

使用phpredis扩展进行操作

Windows php\_redis.dll 模块下载（根据你的PHP版本选择下载）

```
$cfg = [
    'enabled'    => 1,             // 是否启用缓存
    'dir'        => 'data/cache',  // 缓存文件夹，如果使用缓存服务器，则是缓存变量的前缀
    'expire'     => 3600,          // 缓存更新周期(默认：3600s)
    'compress'   => 0,             // 是否启用缓存内容压缩后存贮
    'class'      => 'Redis',        // 缓存模式，File）文件缓存；Memcache）使用Memcache缓存；Redis）使用Redis缓存

    'redis' => [
        'host'           => '127.0.0.1',     //
        'port'           => 6379,            //
        'pconnect'       => 1,               //
        'password'       => '',              // redis密码，不需要密码验证则留空
        'timeout'        => 0,               // 超时时间（秒），0为不限
    ],
];

$class = "\\wf\\cache\\adapter\\{$cfg['class']}";
$cache = new $class($cfg);

```

在Windwork中使用缓存组件
================

[](#在windwork中使用缓存组件)

在Windwork项目中设置好缓存配置后，使用cache()函数访问缓存对象实例。

1、在配置文件 config/app.php中设置缓存配置（默认使用文件缓存）

```
// config/app.php
return [
    // 缓存组件设置
    'cache' => [
        'enabled'          => 1,                   // 是否启用缓存
        'class'            => 'File',              // 缓存模式，File|Memcache|Memcached|Redis
        'dir'              => dirname(__DIR__) . '/data/cache',        // 缓存文件夹
        'expire'           => 7200,                // 缓存更新周期(默认：7200s)
        'compress'         => 0,                   // 是否启用缓存内容压缩后存贮（建议只在虚拟主机中使用文件缓存时启用，以便省出存储空间）

        // redis
        'redis' => [
            'host'         => '127.0.0.1',     //
            'port'         => 6379,            //
            'pconnect'     => 1,               //
            'timeout'      => 0,               // 超时时间（秒），0为不限
        ],
        // memcache
        'memcache' => [
            'host'         => '127.0.0.1',     //
            'port'         => 11211,           //
            'pconnect'     => 1,               //
            'timeout'      => 1,               // 超时时间（秒），0为不限
        ],
        // memcached
        'memcached' => [
            'host'         => '127.0.0.1',     //
            'port'         => 11211,           //
            'pconnect'     => 1,               //
            'timeout'      => 1,               // 超时时间（秒），0为不限
        ],
    ],
];

```

2、使用缓存组件

```
// 读取
cache()->read('some/var/data');

// 写入
cache()->write('some/scalar/data/key', 'some data');
cache()->write('some/array/data/key', ['some data 1', 'some data 2']);

// 删除'some/scalar/data/key'缓存
cache()->delete('some/scalar/data/key');

// 清除some/*缓存
cache()->clear('some');

// 清除全部缓存
cache()->clear();

```

### 要了解更多？

[](#要了解更多)

> - [官方完整文档首页](http://docs.windwork.org/manual/)
> - [官方源码首页](https://github.com/windwork)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~4 days

Total

3

Last Release

3244d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/500cd48271ed61b12fbac17502e33a2b7ff364948c9ffe2d4a86d5d4d63540fb?d=identicon)[windwork](/maintainers/windwork)

---

Top Contributors

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

---

Tags

cachecachingmemcache-cachememcached-cacheperformanceredis-cacheperformancerediscachecachingmemcachedmemcache

### Embed Badge

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

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

###  Alternatives

[tedivm/stash

The place to keep your cache.

9824.8M124](/packages/tedivm-stash)[tedivm/stash-bundle

Incorporates the Stash caching library into Symfony.

841.4M16](/packages/tedivm-stash-bundle)[alekseykorzun/memcached-wrapper-php

Optimized PHP 5 wrapper for Memcached extension that supports dog-piling, igbinary and local storage

2984.6k1](/packages/alekseykorzun-memcached-wrapper-php)[robinn/phpcacheadmin

A web dashboard for your favorite caching system.

4441.1k1](/packages/robinn-phpcacheadmin)[ihor/cachalot

Cache a lot in a proper way (APC, XCache, Memcached, Redis, Couchbase)

2528.1k](/packages/ihor-cachalot)[avantarm/memcached-emulator

Memcached PHP extension emulator for Windows environments.

101.3k](/packages/avantarm-memcached-emulator)

PHPackages © 2026

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