PHPackages                             yiisoft/cache-apcu - 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. [Framework](/categories/framework)
4. /
5. yiisoft/cache-apcu

ActiveLibrary[Framework](/categories/framework)

yiisoft/cache-apcu
==================

Yii Caching Library - APCu Handler

1.1.0(2y ago)136.5k↓40%7[2 PRs](https://github.com/yiisoft/cache-apcu/pulls)BSD-3-ClausePHPPHP ^8.0CI passing

Since Feb 2Pushed 3mo ago15 watchersCompare

[ Source](https://github.com/yiisoft/cache-apcu)[ Packagist](https://packagist.org/packages/yiisoft/cache-apcu)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/yiisoft)[ Fund](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-cache-apcu/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (7)Used By (0)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii Cache Library - APCu Handler
================================

[](#yii-cache-library---apcu-handler)

[![Latest Stable Version](https://camo.githubusercontent.com/753734404d8f84d54c9ad20a248502b507e424109857d785412e3eb7f3024c9b/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f63616368652d617063752f762f737461626c652e706e67)](https://packagist.org/packages/yiisoft/cache-apcu)[![Total Downloads](https://camo.githubusercontent.com/5c1401acd8875596c537512cf83ee0c72a132766a42f3faa5fb9bafc82dc9d84/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f63616368652d617063752f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yiisoft/cache-apcu)[![Build status](https://github.com/yiisoft/cache-apcu/workflows/build/badge.svg)](https://github.com/yiisoft/cache-apcu/actions?query=workflow%3Abuild)[![Code Coverage](https://camo.githubusercontent.com/87072742da777554d2af9058128a6daa7e16bbda8d832bec5b420f38e43d0cbd/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f63616368652d617063752f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/yiisoft/cache-apcu)[![Mutation testing badge](https://camo.githubusercontent.com/83b059318483c801c6235794f57db2758505b3324d60273e36661c9f37704946/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f667425324663616368652d617063752532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/cache-apcu/master)[![static analysis](https://github.com/yiisoft/cache-apcu/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/cache-apcu/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/0577ba51068a6faa52c8b2de07768baacc85c4723186abbab5511410ba5e5838/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f63616368652d617063752f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/cache-apcu)

This package uses the PHP [APCu](https://www.php.net/manual/book.apcu.php)extension and implements [PSR-16](https://www.php-fig.org/psr/psr-16/) cache.

This option can be considered as the fastest one when dealing with a cache for a centralized thick application (e.g. one server, no dedicated load balancers, etc.).

Requirements
------------

[](#requirements)

- PHP 8.0 or higher.
- `APCu` PHP extension.

Installation
------------

[](#installation)

The package could be installed with [Composer](https://getcomposer.org):

```
composer require yiisoft/cache-apcu
```

General usage
-------------

[](#general-usage)

The package does not contain any additional functionality for interacting with the cache, except those defined in the [PSR-16](https://www.php-fig.org/psr/psr-16/) interface.

```
$cache = new \Yiisoft\Cache\Apcu\ApcuCache();
$parameters = ['user_id' => 42];
$key = 'demo';

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === null) {
    // $data is not found in cache, calculate it from scratch
    $data = calculateData($parameters);

    // store $data in cache for an hour so that it can be retrieved next time
    $cache->set($key, $data, 3600);
}

// $data is available here
```

In order to delete value you can use:

```
$cache->delete($key);
// Or all cache
$cache->clear();
```

To work with values in a more efficient manner, batch operations should be used:

- `getMultiple()`
- `setMultiple()`
- `deleteMultiple()`

This package can be used as a cache handler for the [Yii Caching Library](https://github.com/yiisoft/cache).

Cleaning up APCu cache
----------------------

[](#cleaning-up-apcu-cache)

Typically the web processes are separate from CLI so these do not share the same cache instance. Thus, special handling in the web is needed for the case. First, a web-acessible script `apc_clear.php` like the following:

```
