Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

memcached - How can I store data in RAM memory using PHP?

Is there a way to store small data in RAM memory using PHP so that I can have access to the data between different session instead of regenerating it. Something similar to memcached (I don't have access to memcahced). My current solution is just to save the data in file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

APC?

It works differents from memcached; in memcached you can access the data from various languages (c, python, etc..) while APC works only for PHP.

EDIT are you sure that APC is installed correctly? Did you add extension=apc.so in your php.ini? And to restart apache (im assuming youre on a lamp server with apache2)? What does you phpinfo(); say about APC?

This is a simply test that work perfectly for me:

<?php
/*
 * page.php
 * Store the variable for 30 seconds,
 * see http://it.php.net/manual/en/function.apc-add.php
 * */
if(apc_add('foo', 'bar', 30)){
    header('Location: page2.php');
}else{
    die("Cant add foo to apc!");
}

<?php
/*
 * page2.php
 * */
echo 'foo is been set as: ' . apc_fetch('foo');

p.s: i prefer to use apc_add over apc_store, but the only difference between them is that apc_add doesnt overwrite the variable but will fail if called twice with the same key:

Store the variable using this name. keys are cache-unique, so attempting to use apc_add() to store data with a key that already exists will not overwrite the existing data, and will instead return FALSE. (This is the only difference between apc_add() and apc_store().)

It's a matter of taste/task of the script, but the example above works with apc_store too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...