Coding…
I’ve been doing a bunch of PHP stuff lately. One interesting project is the template engine, which has seem some really awesome progress over the last weeks. But more on that later…
Something else I’ve tried out a few days ago is using memcached. Memcached is a small server program that runs in the background (as a service on Windows machines, or as a daemon on Linux) and uses spare RAM to store data. It can also run distributed, with a bunch of servers all throwing their spare memory into a big pool which can conveniently be accessed as if it was on a single machine. PHP has a standard plugin that can communicate with memcached – what can you do with that, you wonder? Well, as the name suggest, memcached is a cache which stores data (in key/value) pairs in memory. Basically your script can store data in it so you don’t have to fetch it from the database (saving you from executing a time-consuming query and putting load on the database server).
It is actually quite efficient – all of the world’s big websites (especially a lot of social networks and such) use it. Some sites even have massive amounts of servers dedicated to it. In such setups getting everything right is tricky (you want to be sure that everything is consistent – with a large distributed system it’s easy for something to go out of sync, potentially causing a lot of trouble), but it is quite useful.
I played with it for a bit and it turns out that memcached is really very easy to work with. Installation on my Windows box was a matter of putting the executable in a folder and running it command-line with a few options that make it install itself as a service, after which it is permanently available; accessing it from PHP takes no more than adding a standard plug-in to your php.ini file, restarting Apache, and calling the right functions. It’s actually very easy to work with. A quick measurement told me that, when loading some configuration data using memcache vs. loading it from the database, doing it from memcache is actually twice as fast – and that’s for a trivial task.
Unfortunately you’re not going to get memcached support with any basic shared hosting plans (and VPSes generally don’t have the spare memory capacity). It takes a bunch of setting up, securing, and firewalling to make it run securely in a live environment and you can’t let multiple users share the same memcached host without them being able to access eachother’s data. It’s a bit of a downside, but memcached was never intended to be used in a shared environment, so it’s not a big deal.
The project I’m working on right now is actually getting full-blown memcache support, so it will cache anything that can reasonably be cached (within configurable limits), making the whole thing run faster and using far less database queries.
Next up, the template engine. I’ve already written a template engine quite a while ago (several actually) – but I wanted to make some improvements and ended up rewriting most of it. If you don’t know, a template engine essentially separates presentation and program logic, which is very useful if you want to encrypt the actual PHP code without removing the ability to edit the HTML, or just generally to organize things – it makes it very easy to figure out where you need to be to change something, and even non-programmers can easily edit the layout of the page this way.
The previous incarnation of my engine supported variables and blocks. Variables are well, variables, and blocks can either be repeated any given number of times (the contents of variables can be varied for each instance – so this is what you would use to fill a table with data, for instance) or entirely hidden. This is sufficient in a lot of cases, but I wanted to add more: conditional statements, loops (other than repeated blocks), proper caching, and so on.
Previously, all parsing was done in the final processing function, which did everything by reading files and releasing a bunch of string manipulation functions on them. It worked, but the code was complicated as fuck and debugging (let alone adding something new) is a whole new kind of hell by itself. The new version uses a system not unlike what a compiler for, say, C, does: it splits the input data (the template file(s)) into a bunch of tokens (textual data, variables, blocks, etcetera), runs a lexical analysis check (grammar basically – make sure that the end if your if statement does not precede the actual if statement, for instance), and finally, goes over the list of tokens (using a specialized stack), combining it with the dynamic data (contents of variables, block instances, etcetera) to produce output.
That all sounds terribly complicated, and well, it is. One major advantage though is that now 90% of the processing effort (parsing and analyzing the template) can be stored for later use, and the engine does that either by files, or by sending the data to memcached (the latter being preferred, since it doesn’t involve hard drive activity, which is relatively slow). The result is a very powerful and fast system, in which it is actually possible to add a new feature, should the need for it arise.
The whole beast has a ton of features – variables (both local and global), blocks (plus ‘empty blocks’, something that gets shown if there are no instances of the block), conditional statements (if/elseif/else – admittedly expression handling is very basic at the moment, but that’s for future improvement), caching, variable modifiers, a bunch of debug features, tools like optionally stripping HTML comments from the final output – and the whole thing weighs in at ‘only’ 2650 lines of code (including tons of comments and whitespace) / 73.2 kilobytes (all in a single file). It’s definately not finished yet, but I’m quite satisfied already
Oh, and also, I now use NuSphere PhpED. Most of the time, IDE’s mostly manage to annoy the crap out of me, but this one actually works instead of being a constant pain in the ass. I’d definately recommend it.
Comments
Leave a Reply
