Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a search function on my site and it is taking data from 6 different 3rd party resources. The problem is, it takes too long requesting the data over and over again on the results page.

I've read for questions like this on SO about session not being a good choice but for me 'memcache' is not an option, because the server doesn't have memcached installed and I have no way to install it now.

Is there any other approach to do this? Storing in the database seem inappropriate because the data depends on the search terms requested.

What I've been thinking is writing a file on the server that would act as a cache for this file but I don't know how I would know when to delete it after.

share|improve this question
Why is memcache not an option? md5 the request parameters, store the result with the md5 as the key? – Danack May 22 '13 at 20:12
unfortunately, because the server doesn't have memcached installed.. and i have no way to install it now. – reikyoushin May 22 '13 at 20:13
found this.. will be looking more into it later. thanks for the help @danack – reikyoushin May 22 '13 at 20:53

You can cache the results using the file system to store the data. The Zend Cache system is pretty simple to setup and use.

You will need to convert the search terms used to make the query into a cache key. Pretty much any hashing algorithm would work for this.

$md5Value = 0;

foreach ($searchTerms as $searchTerm) {
    $md5Value = md5($md5Value.$searchTerm);

The is a minuscule chance of a hash collision so you may choose to either used longer/strong hashes than md5, or just also store the searchTerms with the search result, and double-check they're the same after retrieving from the cache

class SearchTerms {
    public $term = array();

class SearchResult {
    public $result;

class StoredSearch {
    public $searchTerms;//instance of SearchTerms
    public $searchResult; //instance of SearchResult

i.e. StoredSearch is stored rather than just the search results.

share|improve this answer
I'll try to look into this. but my site is in wordpress.. so zend isn't much an option too. maybe i can check the implementation though. – reikyoushin May 22 '13 at 20:25

There are two methods that could be used in caching here (assuming using the file system only, as you've mentioned Memcache is not available):

Method 1: Cache life is known

If you know how long the cache file should last for, then you could probably just wrap the fetching of cache information in a call to filemtime before fetching. Example:

$filepath = './cache/' . md5($params); // path to cache file
$cachelength = 3600; // 1 hour
if (is_file($filepath) && time() - filemtime($filepath) < $cachelength) {
    $response = unserialize(file_get_contents($filepath));
} else {
    $response = fetch_data();
    file_put_content($filepath, serialize($response));

Method 2: Cache life not known

If you don't know how long the cache should last for, then your other alternative would be to have a cronjob (or scheduled task on Windows) which removes all the cache files every few minutes, so that your cache is continually being cleaned. Example:

*/5 * * * * /bin/rm -f /path/to/cache/*

In addition to the caching, there is another way that you could possibly speed up the initial requests (assuming all requests being made can be asynchronous):

Bonus tip: "Speed up" initial requests using curl_multiexec()

Something you might want to look at using is the curl_multiexec_* functions in order to process all the initial requests in parallel. I have a wrapper for this functionality on github

share|improve this answer

there is also APC, Alternative PHP Cache, it is common in shared hosting php servers.

It has also the ability to define a timeout for each variable. The syntax is pretty simple.

    if (apc_exists($s_lang)) { // array in ram
        $l = apc_fetch($s_lang);
    } else {
        //fetch the resource here. example:
        require_once(__DIR__ . "/" .$s_lang. "/language.php");
        apc_store($s_lang, $l, 60); //save for 1 min.

However, if you reboot the server, the data is lost.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.