• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

Advice for a Content Management System that uses a Publishing Mechanism that enables caching dynamic content?

Hello everyone,

Looking for a method to use dynamic content from either a database or XML source that is retrieved using PHP, but I want the content to cache if it has not changed at all.  Basically, I don't the PHP to keep on making requests to the database if the content has been the same for the last hour.

What methods or solutions do you recommend for this?  I've heard a reverse proxy cache works well, but I heard some sort of publishing mechanism works better.
0
parlays
Asked:
parlays
  • 5
  • 3
  • 2
3 Solutions
 
Richard QuadlingSenior Software DeveloperCommented:
With any sort of cache, you need to have a way of knowing when to get new data.

There are 2 ways that I would think are relevant.

1 - Trigger the creation/update of the cache when data changes. This is the way to go if your data is changed VERY infrequently.

2 - Regularly expire the cache and refresh it automatically. If you have rapidly changing data then this is the way to go.


If you can tell me which of these 2 scenarios fit your data, I can expand on them further.
0
 
parlaysAuthor Commented:
Option #2 sounds interesting, can you please elaborate?
0
 
parlaysAuthor Commented:
I'm curious where the cache would be stored?  Inside an HTML document, and the cache controlled by the server?  I don't fully grasp how it works yet, thanks for your help.
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Richard QuadlingSenior Software DeveloperCommented:
Imagine this ...

User makes request to say report.php
Server gets report.php and simply echos from a HTML file.

Using cron or some other scheduling system, you create the HTML file just like you are currently doing (i.e . doing to the DB, getting the data, etc.)

The simplest way would be to wrap the current script in a output buffer and then write the buffer to the cached HTML file.
0
 
parlaysAuthor Commented:
Sorry to follow up again, what do you mean when you say:
"Server gets report.php and simply echos from a HTML file."

Somebody requests the php file, and it the information comes from the HTML, so the HTML is created from cron (the scheduling system), interesting.  Now that I type it, I start to understand, haha.  Is cron hard to use, how do I populate the HTML file with cron? (I was going to ask if it is better to use XML but I figure PHP will echo HTML much faster)... thanks!
0
 
AzurewaveCommented:
What I do is I have a cronjob run every hour on the hour and run a link that will update a text file with whatever results.

Example
The write to a file is the one you would cron every hour.

The read from file is the page you need tor read.

That's just an example you can save the data however you want and export it however you want.
/* WRITE TO A FILE */
$query = mysql_query("SELECT title, body FROM table LIMIT 5");
$data = array();
while($row = mysql_fetch_array($query))
{
    $data[] = array('title' => $row['title'], 'body' => $row['body']);
}
$data = serialize($data);
$file = 'myfile.txt';
$fh = fopen($file, 'w') or die('Unable to open file.');
fwrite($fh, $data);
fclose($fh);
 
/* READ FROM A FILE */
$file = 'myfile.txt';
$fh = fopen($file, 'r');
$data = fread($fh, filesize($file));
fclose($fh);
$data = unserialize($data);
foreach($data as $key => $val)
{
    echo $val['title'];
    echo $val['body'];
}

Open in new window

0
 
parlaysAuthor Commented:
Excellent stuff, thank you so much.

One last quick question.  For your option #1, what mechanism would i use to trigger update/creation of cache when data change?
0
 
AzurewaveCommented:
well I just followed what I was reading, and I gave my input we are however different users not the same guy.

Anyway the cron job you can have your host set up for you, or you can set it up through cron through cpanel.  There's many methods on implementing this, but it mostly depends on the host, I would talk to your host if you are not under cpanel or something along those lines.
0
 
parlaysAuthor Commented:
I am using Plesk right now on a Virtual Private Server, in the PLESK control panel there is an option for schedule tasks, maybe that is cron or similar.  I will research this matter more, thanks for your help Azurewave, I didn't realize you were another person, lol.
0
 
Richard QuadlingSenior Software DeveloperCommented:
To make the actual changing data initiate the cache file creation requires either an alteration to the application making the change to the data or some sort of monitoring system to see when the data changed.

This mechanism has to have a semaphore lock to stop the cache building code from running simultaneously (fast changes and a slow cache build process for example could end up with multiple and simultaneous cache build processes - bad news).

For a semaphore, I create a folder. They are atomic, the act of creating a folder will either succeed (the folder didn't exist and it does now) or not (the folder already exists).

If you can create the folder, you've got the lock/semaphore.

If you can't then someone else has.

Once you've finished with the building process, you remove the folder.

This works across all the different OS's I've worked on.

0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now