PHPackages                             laruence/yac - 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. laruence/yac

ActivePhp-ext

laruence/yac
============

Yac is a shared and lockless memory user data cache for PHP.

8280197[12 issues](https://github.com/laruence/yac/issues)CCI passing

Since Aug 1Pushed today71 watchersCompare

[ Source](https://github.com/laruence/yac)[ Packagist](https://packagist.org/packages/laruence/yac)[ RSS](/packages/laruence-yac/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

Yac - Yet Another Cache
=======================

[](#yac---yet-another-cache)

[![Build status](https://camo.githubusercontent.com/0e9165d308dce4223a28b2ae3d5fba683e836ac8eec554426a1f48a969f0e8bc/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f3662753039707738756b797836316d322f6272616e63682f6d61737465723f7376673d74727565)](https://ci.appveyor.com/project/laruence/yac/branch/master) [![Build Status](https://github.com/laruence/yac/workflows/integrate/badge.svg)](https://github.com/laruence/yac/actions?query=workflow%3Aintegrate)

Yac is a shared and lockless memory user data cache for PHP.

It can be used to replace APC or local memcached.

Requirement
-----------

[](#requirement)

- PHP 7+

Install
-------

[](#install)

```
$ /path/to/phpize
$ ./configure --with-php-config=/path/to/php-config
$ make && make install
```

Note
----

[](#note)

1. Yac is a lockless cache, you should try to avoid or reduce the probability of multiple processes setting the same key simultaneously.
2. Yac uses partial CRC for integrity checks. You'd better re-arrange your cache content and place the most important (mutable) bytes at the head or tail of the value.

Restrictions
------------

[](#restrictions)

1. Cache key cannot be longer than 48 (YAC\_MAX\_KEY\_LEN) bytes.
2. Cache value cannot be longer than 64M (YAC\_MAX\_VALUE\_RAW\_LEN) bytes.
3. Cache value after compression cannot be longer than 1M (YAC\_MAX\_RAW\_COMPRESSED\_LEN) bytes.

INI Settings
------------

[](#ini-settings)

```
yac.enable = 1

yac.keys_memory_size = 4M  ; 4M can hold ~30K key slots, 32M can hold ~100K key slots

yac.values_memory_size = 64M

yac.compress_threshold = -1 ; -1 means no compression. A positive value N means
                            ; values larger than N bytes will be compressed before storage.

yac.enable_cli = 0  ; whether to enable Yac in CLI mode, default 0

yac.serializer = php ; since Yac 2.2.0, specify which serializer Yac uses.
                      ; Available: php (--enable-json), msgpack (--enable-msgpack),
                      ; or igbinary (--enable-igbinary)
```

Constants
---------

[](#constants)

```
YAC_VERSION

YAC_MAX_KEY_LEN = 48
; If your key is longer than 48 bytes, consider using md5() of the key instead.

YAC_MAX_VALUE_RAW_LEN = 67108864   ; 64M in bytes

YAC_MAX_RAW_COMPRESSED_LEN = 1048576  ; 1M in bytes

YAC_SERIALIZER_PHP      = 0  ; since Yac 2.2.0
YAC_SERIALIZER_JSON     = 1  ; since Yac 2.2.0
YAC_SERIALIZER_MSGPACK  = 2  ; since Yac 2.2.0
YAC_SERIALIZER_IGBINARY = 3  ; since Yac 2.2.0

YAC_SERIALIZER ; the serializer in use, determined by yac.serializer.
               ; Default is YAC_SERIALIZER_PHP.
```

Methods
-------

[](#methods)

### Yac::\_\_construct

[](#yac__construct)

```
Yac::__construct([string $prefix = ""])
```

Constructor of Yac. You can specify a prefix which will be prepended to every key in subsequent `set`/`get`/`delete` calls. The prefix is concatenated directly onto the key — no separator is added automatically. If you need a separator, include it in the prefix.

```

```

### Yac::set

[](#yacset)

```
Yac::set(string $key, mixed $value[, int $ttl = 0]): bool
Yac::set(array $kvs[, int $ttl = 0]): bool
```

Store a value into Yac cache. Keys are cache-unique, so storing a second value with the same key will overwrite the original value.

`$ttl` is the time-to-live in seconds. `0` means the value never expires.

Returns `true` on success, `false` on error (e.g. no memory left, or cannot obtain CAS write lock).

```

```

#### Note

[](#note-1)

Since Yac 2.1, `set()` may fail if CAS competition occurs. For critical values, retry until success:

```
while (!$yac->set("important", "value"));
```

### Yac::get

[](#yacget)

```
Yac::get(string|array $key[, int &$cas = null]): mixed
```

Fetches a stored variable from the cache. If an array is passed, each element is fetched and returned as a key-value array.

`$cas` is an output parameter that receives the CAS token of the retrieved value, useful for implementing compare-and-swap patterns.

Returns the cached value on success, `false` on failure.

```

```

### Yac::delete

[](#yacdelete)

```
Yac::delete(string|array $keys[, int $delay = 0]): bool
```

Removes a stored variable from the cache. If `$delay` is specified (in seconds), the value will be deleted after `$delay` seconds — a delayed deletion.

Returns `true` on success, `false` on failure.

### Yac::flush

[](#yacflush)

```
Yac::flush(): bool
```

Immediately invalidates all existing items. This does not actually free any resources — it only marks all items as invalid.

### Yac::info

[](#yacinfo)

```
Yac::info(): array
```

Get cache info.

```
