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 6050809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:43:49+00:00 2026-05-23T07:43:49+00:00

Is there a way to cache a PHP include effectively for reuse, without APC,

  • 0

Is there a way to cache a PHP include effectively for reuse, without APC, et al?

Simple (albeit stupid) example:

// rand.php
return rand(0, 999);

// index.php
$file = 'rand.php';
while($i++ < 1000){
    echo include($file);
}

Again, while ridiculous, this pair of scripts dumps 1000 random numbers. However, for every iteration, PHP has to hit the filesystem (Correct? There is no inherit caching functionality I’ve missed, is there?)

Basically, how can I prevent the previous scenario from resulting in 1000 hits to the filesystem?

The only consideration I’ve come to so far is a goofy one, and it may not prove effective at all (haven’t tested, wrote it here, error prone, but you get the idea):

// rand.php
return rand(0, 999);

// index.php
$file = 'rand.php';
$cache = array();
while($i++ < 1000){
    if(isset($cache[$file])){
        echo eval('?>' . $cache[$file] . '<?php;');
    }else{
        $cache[$file] = file_get_contents($file);
        echo include($file);
    }
}

A more realistic and less silly example:

When including files for view generation, given a view file is used a number of times in a given request (a widget or something) is there a realistic way to capture and re-evaluate the view script without a filesystem hit?

  • 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-23T07:43:49+00:00Added an answer on May 23, 2026 at 7:43 am

    This would only make any sense if the include file was accessed across a network.

    There is no inherit caching functionality I’ve missed, is there?

    All operating systems are very highly optimized to reduce the amount of physical I/O and to speed up file operations. On a properly configured system in most cases, the system will rarely revert to disk to fetch PHP code. Sit down with a spreadsheet and have a think about how long it would take to process PHP code if every file had to be fetched from disk – it’d be ridiculous, e.g. suppose your script is in /var/www/htdocs/index.php and includes /usr/local/php/resource.inc.php – that’s 8 seek operations to just locate the files – @8ms each, that’s 64ms to find the files! Run some timings on your test case – you’ll see that its running much, much faster than that.

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

Sidebar

Related Questions

Is there a simple way to cache MySQL queries in PHP or failing that,
We are using APC as an opcode cache. Is there a way to get
Is there some way to cache a generated report in the reporting services, so
Is there a way to cache per-request data in Rails? For a given Rails/mongrel
Is there an easy way to cache things when using urllib2 that I am
Is there a way to look through the cache for all objects in the
Is there a way to monitor the size of the cache on your web
Is there any way to force a browser to refresh or empty it's cache,
Rails caches (file cache) per default domain-based, but is there a way to disable
I know that you can cache the WSDL but is there a way to

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.