简体   繁体   中英

Cache PHP variables between pages

Is it possible to store php variables in some kind of cache, so they only need to be generated once, but available on every web page? Wihtout using POST, GET, COOKIES or SESSIONS

I have heaps of URL's stored as variabled which I want to keep handy.

I am aware of caching HTML, but id like to somehow cache php variables. Not sure there is a solution?

I use the zend-server implementation of the php stack. Even the community edition comes with:

zend_shm_cache_fetch();
zend_shm_cache_store();
zend_disk_cache_fetch();
zend_disk_cache_store();

Things set in those variables live across script executions, and clean themselves up after a timeout specified on creation.

Complex variables are allowed, so you can store associative arrays and such without encoding them first.

I use them extensively.
The shm methods can store and retrieve 32k chunks in the 0.0005 second range.

To store the contents of the $results variable in a cache named blah, with a timeout of 120 seconds:

zend_shm_cache_store('blah',$results,120);

To later retrieve the contents of $results:

$results = zend_shm_cache_fetch('blah');
if ( $results === false ) { # It expired, or never existed..

Zend documentation on this: http://files.zend.com/help/Zend-Platform/zend_cache_functions.htm

You can turn up the amount of space you have in the zend server console. I think it defaults to like 32mb. I have mine at 256mb. You may need to reboot or clear your shared memory segments to make the larger space actually work though. Making it larger does make it slower though. It seems to be faster if you store large arrays of data instead of small individually referenced bits but ymmv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM