Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7566897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:31:41+00:00 2026-05-30T14:31:41+00:00

On my website there is a php function func1() , which gets some info

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:31:43+00:00Added an answer on May 30, 2026 at 2:31 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy some images from a public website with the PHP function
For a homework project, I'm creating a PHP driven website which main function is
On my website various php codes run from various programmers from whom I have
once we send email from php using mail() function, is there any way to
Is there any website/service which will enable me to add RSS subscription to any
Is there a website or documentation that details the changes made from each version?
Is there any website which provides core java based programming challenges that tests programming
I want to create a PHP function that goes through a website's homepage, finds
Read some texts about locking in PHP. They all, mainly, direct to http://php.net/manual/en/function.flock.php .
On my website there is a button that just used to call a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.