The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. December 05, 2008, 10:41:26 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Really Fast Site Configuration  (Read 773 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« on: April 19, 2007, 04:50:39 PM »

There are many ways to get "configuration" data to a site... the most common being a database or flat text file.

But both of these has the drawback of *some* type of access - TCP/IP, file system... something. I suggest that another way to do it, is to literally write values into source code of a PHP file. This does NOT make sense unless you have a code-cacher running, like APC - if you don't, then you've encurred the cost of file system access (one of the most painful) and compiling the PHP code again. But if you DO have APC running, then the code is already compiled and sitting in memory.

Consider this:
Code:
<?php

config_Value1 
10;
config_AnotherValue 'This is a test';
config_MyArray = array(0=>'Hello'1=>'World');

?>


If you were to simply include/require this code in your application, the values would be instantly available to the routine that included it. If this code were already compiled and sitting in RAM, then this becomes and extremely quick way of "configuring a site." How do we manage it though?

Regexes, file_get_contents and file_put_contents. Consider this function:

Code:
define('CONFIGFILE', '/www/sites/aFileName.txt');
function getConfigValue($valueName)
{
if (preg_match("/config_{$valueName}[\s]*=[\s]*(.*)$"/, file_get_contents(CONFIGFILE), $matches))
{
return $matches[1];
} else {
return false;
}
}

With this simple little example, we can see how a value could be extracted from a file and returned. Note that this does not handle case sensitivity, nor the ability to have multiple files or anything, but you get the idea. It also puts the burden of processor time on the updater  - rather than the reader. This is important: by doing it this way, it takes a little longer to read and write values to the config file - but the execution of the website will be way faster.

Writing configuration data could be easier than this, but I like this method because it creates nice orderly files every time:
Code:
define('CONFIGFILE', '/www/sites/aFileName.txt');
function setConfigValue($valueName, $aValue)
{
$lines = explode(chr(10), file_get_contents(CONFIGFILE));
foreach($lines as $line)
{
if (!preg_match('/config_([^\s]+)[\s]*=[\s]*(.*$)', $line, $parts)) { continue; }
$values[$parts[1]] = $parts[2];
}
$parts[$valueName] = $aValue;
ksort($parts);
$output[] = '<?';
foreach($values as $name=>$value) { $output[] = "config_$name = $value"; }
$output[] = '?>';
file_put_contents(implode(chr(10), $output);
}

Note that this little example does not handle multi-user issues, nor even some of the more ticklish issues of what type of data is being sent and such. The handy part of APC here is that much like JBoss and other Java systems, the acto of modifying the mtime on a file will invalidate it in the cache - so the very next pull it will be recompiled and cached again - and stay that way until it is modified again.
Logged

If I can't be Mr. Root then I don't want to play.
dee
Journeyman
***
Offline Offline

Posts: 52


View Profile
« Reply #1 on: July 10, 2008, 02:31:48 AM »

i know this is an old thread... but i just found it.

Im starting out with php and thought that includes were a great way of doing things.Especially for multiple sites as you can change 1 config file and of you go. You mention caching.Is that something that i would need to install on the server or instigate in the code? At present the stuff im doing is very very simple so it probably not too noticable, but obviously im hoping to change the simplicity that over time.

I guess what im asking is if you use includes, do you need to also implement caching of some sort if your on a standard hosting server?

cheers
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #2 on: July 10, 2008, 09:07:23 AM »

I guess what im asking is if you use includes, do you need to also implement caching of some sort if your on a standard hosting server?

Absolutely not. Includes are just part of PHP, and they will alone help out your speed, although not nearly so much as if you are caching. APC is my cache of choice, it is a module you must download and install into your PHP instance, so if a host is not providing it, it will be near impossible to do. VPS, COLO and Dedi obviously are no problem since they're your machines.

The include method is just good programming technique - when appropriate. You could go way overboard with it as well (just like anything) but you're on the right track when you say, "Just include one file and off you go" - that's what a lot of my frameworks are... a big body of code that includes the customization parameters. It's fast, light and strong.
Logged

If I can't be Mr. Root then I don't want to play.
dee
Journeyman
***
Offline Offline

Posts: 52


View Profile
« Reply #3 on: July 10, 2008, 11:27:53 AM »

thanks perkiset...

nice to know im doing something right
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Online Online

Posts: 636



View Profile
« Reply #4 on: July 10, 2008, 11:39:47 AM »

Incidentally, it's 90% certain that APC will be included as a part of PHP6.
Logged

perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #5 on: July 10, 2008, 11:43:39 AM »

nice to know im doing something right

You signed up here, didn't you? Wink
Logged

If I can't be Mr. Root then I don't want to play.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5230


:sniffle: Humor was so much easier before.


View Profile
« Reply #6 on: July 10, 2008, 11:44:03 AM »

Incidentally, it's 90% certain that APC will be included as a part of PHP6.

That will be sweet, but I wonder how long it will be before the cheap hosts will upgrade?
Logged

If I can't be Mr. Root then I don't want to play.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!