In this tutorial you will learn how to set and get cache values in PHP APC. I will explain how PHP functions apc_add, apc_fetch and apc_store works.
Code used during this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# PHP APC Set and get cache values # PHP scripts used apc_add.php apc_fetch.php apc_store.php # Cached data are ttl-based and persists until HTTP web server/system reboot # Restart Apache HTTP web server sudo service apache2 restart Useful links: http://php.net/manual/en/ref.apc.php http://php.net/manual/en/function.apc-add.php http://php.net/manual/en/function.apc-fetch.php http://php.net/manual/en/function.apc-store.php |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php /** * apc_add * Cache a new variable in the data store * http://php.net/manual/en/function.apc-add.php */ $ttl = 5; $key = 'CUR_DATE_5s_1'; $var = date('c'); $result = apc_add($key, $var, $ttl); var_dump($result); $var = date('c') . ' overwrite'; $result = apc_add($key, $var, $ttl); var_dump($result); $key = 'CUR_DATE_0s_1'; $var = date('c'); $result = apc_add($key, $var); var_dump($result); $values = array( 'CUR_DATE_5s_2' => date('c'), 'CUR_DATE_5s_3' => rand(), ); $result = apc_add($values, null, $ttl); var_dump($result); $values = array( 'CUR_DATE_0s_2' => date('c') . ' overwrite', 'CUR_DATE_0s_3' => rand() . ' overwrite', ); $result = apc_add($values, null); var_dump($result); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php /** * apc_fetch * Fetch a stored variable from the cache * http://php.net/manual/en/function.apc-fetch.php */ var_dump(apc_fetch(array( 'CUR_DATE_5s_1', 'CUR_DATE_5s_2', 'CUR_DATE_5s_3', 'CUR_DATE_0s_1', 'CUR_DATE_0s_2', 'CUR_DATE_0s_3', ))); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php /** * apc_store * Cache a variable in the data store * http://php.net/manual/en/function.apc-store.php */ $ttl = 5; $key = 'CUR_DATE_5s_1'; $var = date('c'); $result = apc_store($key, $var, $ttl); var_dump($result); $var = date('c') . ' overwrite'; $result = apc_store($key, $var, $ttl); var_dump($result); $key = 'CUR_DATE_0s_1'; $var = date('c'); $result = apc_store($key, $var); var_dump($result); $values = array( 'CUR_DATE_5s_2' => date('c'), 'CUR_DATE_5s_3' => rand(), ); $result = apc_store($values, null, $ttl); var_dump($result); $values = array( 'CUR_DATE_0s_2' => date('c') . ' overwrite', 'CUR_DATE_0s_3' => rand() . ' overwrite', ); $result = apc_store($values, null); var_dump($result); ?> |
Useful links:
http://php.net/manual/en/ref.apc.php
http://php.net/manual/en/function.apc-add.php
http://php.net/manual/en/function.apc-fetch.php
http://php.net/manual/en/function.apc-store.php