1<?php 2 3namespace Doctrine\Common\Cache; 4 5use function apcu_cache_info; 6use function apcu_clear_cache; 7use function apcu_delete; 8use function apcu_exists; 9use function apcu_fetch; 10use function apcu_sma_info; 11use function apcu_store; 12use function count; 13 14/** 15 * APCu cache provider. 16 * 17 * @link www.doctrine-project.org 18 */ 19class ApcuCache extends CacheProvider 20{ 21 /** 22 * {@inheritdoc} 23 */ 24 protected function doFetch($id) 25 { 26 return apcu_fetch($id); 27 } 28 29 /** 30 * {@inheritdoc} 31 */ 32 protected function doContains($id) 33 { 34 return apcu_exists($id); 35 } 36 37 /** 38 * {@inheritdoc} 39 */ 40 protected function doSave($id, $data, $lifeTime = 0) 41 { 42 return apcu_store($id, $data, $lifeTime); 43 } 44 45 /** 46 * {@inheritdoc} 47 */ 48 protected function doDelete($id) 49 { 50 // apcu_delete returns false if the id does not exist 51 return apcu_delete($id) || ! apcu_exists($id); 52 } 53 54 /** 55 * {@inheritdoc} 56 */ 57 protected function doDeleteMultiple(array $keys) 58 { 59 $result = apcu_delete($keys); 60 61 return $result !== false && count($result) !== count($keys); 62 } 63 64 /** 65 * {@inheritdoc} 66 */ 67 protected function doFlush() 68 { 69 return apcu_clear_cache(); 70 } 71 72 /** 73 * {@inheritdoc} 74 */ 75 protected function doFetchMultiple(array $keys) 76 { 77 return apcu_fetch($keys) ?: []; 78 } 79 80 /** 81 * {@inheritdoc} 82 */ 83 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) 84 { 85 $result = apcu_store($keysAndValues, null, $lifetime); 86 87 return empty($result); 88 } 89 90 /** 91 * {@inheritdoc} 92 */ 93 protected function doGetStats() 94 { 95 $info = apcu_cache_info(true); 96 $sma = apcu_sma_info(); 97 98 return [ 99 Cache::STATS_HITS => $info['num_hits'], 100 Cache::STATS_MISSES => $info['num_misses'], 101 Cache::STATS_UPTIME => $info['start_time'], 102 Cache::STATS_MEMORY_USAGE => $info['mem_size'], 103 Cache::STATS_MEMORY_AVAILABLE => $sma['avail_mem'], 104 ]; 105 } 106} 107