Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Alternative PHP Cache : apc_store

apc_store

Cache a variable in the data store (PECL apc:3.0.0-3.0.9)
bool apc_store ( string key, mixed var [, int ttl] )

Example 216. A apc_store() example

<?php
$bar
= 'BAR';
apc_store('foo', $bar);
var_dump(apc_fetch('foo'));
?>

The above example will output:

string(3) "BAR"

Code Examples / Notes » apc_store

php

Seems to be no (easy) way at the to know how old a value fetched is and to check whether it is out of date.
I've made these wrappers so that you can fetch and store values based on a udt returned from get_last_modified_date() which should return a udt of when your data was last changed, and hence needs junking out of the cache.
function apc_fetch_udt($key){
$g = apc_fetch($key);
if ($g){
list($udt,$val) = $g;
if (get_last_modified_date()<$udt) {
$val = unserialize($val);
return $val;
} else {
apc_delete($key);
}
}
}
function apc_store_udt($key,$g){
$udt = time();
$g   = serialize($g);
$apc = array($udt,$g);
apc_store($key, $apc);
}


sudhee

It should be noted that apc_store appears to only store one level deep.  So if you have an array of arrays, and you store it.  When you pull it back out with apc_fetch it will only have the top level row of keys with nulls as the values of each key.

Solution to this, is to serialize the data before storing it in the cache and unserialize it while retrieving from the cache.


jaskas

if you want to store array of objects in apc use ArrayObject wrapper (PHP5).
<?php
$objs = array();
$objs[] = new TestClass();
$objs[] = new TestClass();
$objs[] = new TestClass();
//Doesn't work
apc_store('objs',$objs,60);
$tmp = apc_fetch('objs');
print_r($tmp);
//Works
apc_store('objs',new ArrayObject($objs),60);
$tmp = apc_fetch('objs');
print_r($tmp->getArrayCopy());
?>


roberto spadim

be sure that setting FALSE values can be wrong returned from fetch since fetch return FALSE on errors

Change Language


Follow Navioo On Twitter
apc_add
apc_cache_info
apc_clear_cache
apc_compile_file
apc_define_constants
apc_delete
apc_fetch
apc_load_constants
apc_sma_info
apc_store
eXTReMe Tracker