如何在没有数据库的情况下每5分钟保存Php变量

On my website there is a php function func1(), which gets some info from other resources. It is very costly to run this function.

I want that when Visitor1 comes to my website then this func1() is executed and the value is stored in $variable1=func1(); in a text file (or something, but not a database).

Then a time interval of 5 min starts and when during this interval Visitor2 visits my website then he gets the value from the text file without calling the function func1().

When Visitor3 comes in 20 min, the function should be used again and store the new value for 5 minutes.

How to make it? A small working example would be nice.

Store it in a file, and check the file's timestamp with filemtime(). If it's too old, refresh it.

$maxage = 1200; // 20 minutes...
// If the file already exists and is older than the max age
// or doesn't exist yet...
if (!file_exists("file.txt") || (file_exists("file.txt") && filemtime("file.txt") < (time() - $maxage))) {
  // Write a new value with file_put_contents()
  $value = func1();
  file_put_contents("file.txt", $value);
}
else {
  // Otherwise read the value from the file...
  $value = file_get_contents("file.txt");
}

Note: There are dedicated caching systems out there already, but if you only have this one value to worry about, this is a simple caching method.

In a text file. Oldest way of saving stuff (almost). Or do a cronjob to run the script with the function each 5 minutes independently on the visits.

Use caching, such as APC!

If the resource is really big, this may not be the best option and a file may then indeed be better.

Look at:

  • apc_store
  • apc_fetch

Good luck!

What you are trying to accomplish is called caching. Some of the other answers you see here describe caching at it's simplest: to a file. There are many other options for caching depending on the size of the data, needs of the application, etc.

Here are some caching storage options:

  • File
  • Database/SQLite (yes, you can cache to a database)
  • MemCached
  • APC
  • XCache

There are also many things you can cache. Here are a few:

  • Plain Text/HTML
  • Serialized data such as PHP objects
  • Function Call output
  • Complete Pages

For a simple, yet very configurable way to cache, you can use the Zend_Cache component from the Zend Framework. This can be used on it's own without using the whole framework as described in this tutorial.

I saw somebody say use Sessions. This is not what you want as sessions are only available to the current user.

Here is an example using Zend_Cache:

include ‘library/Zend/Cache.php’;

// Unique cache tag
$cache_tag = "myFunction_Output";

// Lifetime set to 300 seconds = 5 minutes
$frontendOptions = array(
   ‘lifetime’ => 300,
   ‘automatic_serialization’ => true
);

$backendOptions = array(
    ‘cache_dir’ => ‘tmp/’
);

// Create cache object 
$cache = Zend_Cache::factory(‘Core’, ‘File’, $frontendOptions, $backendOptions);

// Try to get data from cache
if(!($data = $cache->load($cache_tag)))
{
    // Not found in cache, call function and save it
    $data = myExpensiveFunction();
    $cache->save($data, $cache_tag);
}
else
{
    // Found data in cache, check it out
    var_dump($data);
}