Archive for April 2008


Save your Site, Cache that Data!

April 25th, 2008 — 1:38pm

One of the things that I’ve noticed in running PHPDeveloper.org is that the highest traffic (most of the traffic for the site, actually) is going to the RSS feed giving the latest news I’ve posted. When I first relaunched the site with Solar, things were fine – but only for about ten minutes. As soon as everyone’s aggregators came back on and started pulling the feed, the load on my server shot straight up. Thankfully I was able to get it back down to a more manageable level with a static version before the box took a nosedive. I had to do something about it and I figured that caching the feed’s information was definitely a start.

I’d never really used the Solar_Cache stuff before, but thankfully – it’s super easy. I figured that the biggest bottleneck in making the feed was pulling the data from the database each time. I opened up the controller for my feed (Feed.php – I know, very creative) and added a Solar_Cache object.

You can set this stuff up in your configuration file too, but I dropped it into my controller as a quick solution.

In my _setup() call:

$config=array(
    'adapter'=> 'Solar_Cache_Adapter_File',
    'path'=> '/tmp',
    'life'=> 200
);
$this->cache = Solar::factory('Solar_Cache', $config);

This creates a cache object in $this->cache that I can use for whatever I want. It’s file caching and the results will get put in /tmp. That “200″ for the life is in seconds, so it’s at about three minutes right now. There’s lots more options for caching besides files already built into the framework too like APC, eAccelerator, variables and XCache.

With our object made, we apply it down in our default actionIndex() wrapped around our database fetch:

$ret=$this->cache->fetch('feed_data');
if(!$ret){
   $news=$this->news->fetchNews();
   $this->cache->save('feed_data',$news);
   $ret=$news;
}
$this->news_items=$ret;

Pretty simple, really – the cache object checks to see if the data already exists and, if it does, just passes it on through to our view. If it doesn’t (either that it’s the first time it’s being made or it has expired) it will pull the new news and push it out to the cache. The view then takes this array of values and makes a basic RSS feed out of it for all the world to see.

You wouldn’t believe how much something simple like caching your feed can help on even a moderately popular site. Check out the class list for details on the other caching options.

4 comments » | PHP, Solar

Twitter Updates for 2008-04-23

April 23rd, 2008 — 11:59pm
  • @ramsey: did you ever get that "too many requests" issue solved with spaz? #
  • work you damn jquery selector! i command thee! #
  • @funkatron on spaz? every time I fire it up I get that "too many API requests" message after a few seconds #
  • @funkatron @ElizabethN trying the passowrd thing first… #
  • hmm, interesting – an onclick on an anchor and an onclick on a div firing at the same time #
  • @jeichorn nice! #
  • @ElizabethN heh…me too…wonder what it was #
  • okay, this cough is officially getting old #
  • grr….slow people– #
  • how many dbas does it take to check permissions on a sequence…(apparently 3) #
  • @elazar heheh #
  • i <3 last.fm #

Comment » | Personal

Twitter Updates for 2008-04-22

April 22nd, 2008 — 11:59pm
  • Happy Earth Day all (or for me and my wife, our anniversary – woo!) #
  • we now return to "damn nature, you’re scary!" #

Comment » | Personal

A Look Back from 10k

April 18th, 2008 — 2:40pm

It’s funny – I only realized a few days ago that the 10k post was coming up. You get so used to just wring the posts day after day that you don’t even really notice the numbers. Each thousand along the way has definitely been a milestone, but reaching ten thousand posts to PHPDeveloper.org really feels like an accomplishment.

I started the site while I was back in college. A friend of mine at the time (who I now work with once again, woo!) introduced a young Perl programmer to the wild world of web programming. When I headed out to college I had a basic idea of how the web worked and what it was. I knew there was value in it and not just in the business sense. There was this feeling that I could put my fingers to the keys and make things – things other people could look at and enjoy. When I learned about PHP, my interest grew and I read everything I could and looked at every site even remotely related to PHP. The more I really got into it, the more I wanted to share the things I was finding with the whole web (or at least the part that would want to read it). So, in one late Texas summer, the phpdeveloper.org domain name was bought and I set up shop on a little 486 there on the school’s network….it pays to be friends with the network admins.

Back then I would only post once a day – it’d gather some of the things that I thought were useful and write a summary, mashing them all together. I worked on the site (then in PHP3) and used it as a learning experience to grow in the language. Terms like “CMS” and “abstraction” started to come into the picture and soon PHP4 burst onto the scene in a big way. The site got its first major rewrite then, adapting to this strange and new object oriented setup the language now offered. I created my own little set of libraries to use for the site and whatever other projects that bled over on the sites. The number of hits that the site was getting was growing and the little 486 had to be retired for a dual Celeron 266 machine in my apartment. It kept the room hot but it served up the pages well.

Fast forward to just a few years ago and you’ll find PHPDeveloper.org sitting on a dedicated server, graduating up to the “big boy” world. The site’s been through two more major rewrites (one with the Zend Framework and the other recently with Solar) and have moved hosts once again to where it lives now (Slicehost).

I’ve had tons of help from others in the PHP community out there over the years – people like Davey, Eric and Ben that have posted for me what I wasn’t even remotely close to an internet connection. There have also been lots of supporting players over the years and, more recently, people from the community offering suggestions and sending in news submissions and leads to follow.

PHPDeveloper.org has always been about sharing the best and latest from all around the web to the PHP developers out there and I have no plan of stopping any time soon. As long as the PHP community thrives (and lets face it, it’s not going anywhere) the site will be right there along with it with plenty of news, views and community thoughts as they happen.

Thanks to all for the support to make it to 10k – here’s to hoping for 10k more!

3 comments » | Community, PHP, Web

PHPDeveloper.org Sees the Light – The Move to Solar

April 13th, 2008 — 6:53am

So I’ve been asked several times about the move I made for PHPDeveloper.org from the Zend Framework over to Solar with the main question being “why?”

Well, I hate to break it to any of the framework zealots out there but the only real reason I had for doing it was the old “try something new” idea that floats around every so often. Yes, I know the site was working just fine under the old Zend Framework code (and yes, I do mean old – I think it was a few versions before 1.0 even) but I get the itch to do something new every once and a while. Some people redesign – I’ve done that too – and others rewrite things from scratch.

I took the opportunity to really look at the core of the site, stripping out all of the extra little “goodies” that I’d hacked in to the ZF version to get various things working. I came back to the primary focus of the site – to provide the latest news information quickly and easily – and made sure that all new code pointed toward that goal. The overall concept is a simple one really: it’s essentailly a blog whos topic just happens to be the goings-on of the PHP community as a whole. All it really needed to do was show the news items, serve up a feed and provide an administrative way for me to add new posts to the system.

I developed the Zend Framework version many moons ago and I almost don’t remember how it was all set up. It took me a bit to get back into it to see how things were structured, but in the end, most of that was tossed and replaced wth some sleek Solar code.

One of the biggest things that I was happy with in the new rewrite was the way that the news information is fetched from a the database. There’s no longer function calls like “getMainNews()” or “getNewsDetail()”. Instead they were replaced by a fetch function that takes in parameters on the object (sort of like the Command design pattern) and applies them to the current query.

For example, I make a call to the “setProperties” function of the NewsAPI object to tell it that I want a type of “where” with a value of “ID=1234″. The fetch function then looks through the properties and applies the operation to the query. The result is a function that can be called the same way every time with the same sort of output. The only difference is in how much/what kind of output there is.

I was concerned about some of the performance issues I was seeing on my server when I made the switch. Some of it was my own fault – forgetting to cache the feed instead of geenrating it, not adding the spam/IP filtering to ward off some of the spammers – but there was still a slow down when the load started to get high. I knew Solar could handle it (it had done it wonderfully on the dev server) so it had to be something else. The dedicated machine I’d been using was nice, but was showing some of its age. I decided to buy a slice from Slicehost and set up shop over there with only PHPDev running on it. Turns out that it wasn’t the new Solar version that was the issue, it was the server. In fact, I’d almost be willing to not cache the feed anymore – the performance is that good.

My last little part of the transition is writing the backend command-line scripts that I use to do some automatic things and the site will be back and complete and 100% Solar-ized.

I know there’s some things I didn’t cover here, so if you have any questions, leave a comment or drop me a line: enygma at phpdeveloper dot org. I’m more than happy to talk Solar with you. And if you’re interested and want to chat with other Solar folks (including some of the main developers behind it), come over to the freenode network – irc.freenode.net – and pop in to #solarphp and say “hi”.

SolarPHP.com

Comment » | PHP, Solar, Web

Back to top