A fast shared memory user data cache for PHP
C PHP M4 C++ Other
Latest commit 9021ae4 Feb 13, 2017 @laruence committed on GitHub Merge pull request #77 from remicollet/patch-1
Fix for 7.1
Failed to load latest commit information.
compressor/fastlz Initial commit Mar 18, 2013
serializer Fixed issue #63 May 2, 2016
storage cleanup Jul 17, 2015
tests Fix for 7.1 Sep 14, 2016
travis Add travis Mar 18, 2013
.travis.yml update travis Oct 27, 2015
CREDITS Add credits Jun 24, 2015
EXPERIMENTAL Initial commit Mar 18, 2013
LICENSE Add LICENSE Mar 23, 2013
README.md Update README.md Nov 24, 2016
config.m4 Attempt to fix issue #54 Dec 24, 2015
config.w32 fix compile failure on windows Jul 25, 2014
package.xml preparing for 2.0.1 Jul 2, 2016
php_yac.h back to dev Jul 2, 2016
yac.c update write error Aug 11, 2016
yac.php Initial commit Mar 18, 2013


#Yac - Yet Another Cache Build Status

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

it can be used to replace APC or local memcached.


Yac is lockless, that means, there might be a chance that you will get a wrong data(depends on how many key slots are allocated and how many keys are stored), so you'd better make sure that your product is not very sensitive to that.

According to the test(https://github.com/laruence/yac/blob/master/tests/yac_conflict.php), there is 1/10000000 chance you will get a wrong data, however this test script is designed to make conflicts, in the real application, this chance must be much less.


  • PHP 5.2 +


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


  1. Yac is a lockless cache, you should try to avoid or reduce the probability of multiple processes set one same key
  2. Yac use partial crc, you'd better re-arrange your cache content, place the most mutable bytes at the head or tail


  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 compressed cannot be longer than 1M (YAC_MAX_VALUE_COMPRESSED_LEN) bytes


yac.enable = 1

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

yac.values_memory_size = 64M

yac.compress_threshold = -1

yac.enable_cli = 0 ; whether enable yac with cli, default 0



YAC_MAX_KEY_LEN = 48 ; if your key is longer than this, maybe you can use md5 result as the key





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

Constructor of Yac, you can specify a prefix which will used to prepend to any keys when doing set/get/delete

   $yac = new Yac("myproduct_");


   Yac::set($key, $value[, $ttl])
   Yac::set(array $kvs[, $ttl])

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

$yac = new Yac();
$yac->set("foo", "bar");
        "dummy" => "foo",
        "dummy2" => "foo",


   Yac::get(array|string $key)

Fetches a stored variable from the cache. If an array is passed then each element is fetched and returned.

$yac = new Yac();
$yac->set("foo", "bar");
        "dummy" => "foo",
        "dummy2" => "foo",
$yac->get(array("dummy", "dummy2"));


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

Removes a stored variable from the cache. If delay is specified, then the value will be deleted after $delay seconds.



Immediately invalidates all existing items. it doesn't actually free any resources, it only marks all the items as invalid.



Get cache info

  /* will return an array like:
  array(11) {
      ["memory_size"]=> int(541065216)   
      ["slots_memory_size"]=> int(4194304)
      ["values_memory_size"]=> int(536870912)
      ["segment_size"]=> int(4194304)     
      ["segment_num"]=> int(128)
      ["miss"]=> int(0)
      ["hits"]=> int(955)
      ["fails"]=> int(0)
      ["kicks"]=> int(0)
      ["slots_size"]=> int(32768)
      ["slots_used"]=> int(955)