Archive for the ‘Smarty’ Category

Memcached

Wednesday, September 2nd, 2009

Wow!

I’m in charge of a site that uses a lot of resources, thousands of images, thousands of db rows and literally thousands of visitors each day. We are currently getting approximately 60 to 70 thousand visitors but the thing is that by the time we get to 60 thousand the server starts to slow down. The require for images, queries and number of petitions per second made us think of a redesign of the site.

First step was to load the balance between 2 servers for http request and for database queries. Each server will have its own database and web code. Over those servers we put a load balancer hardware.

Second step was clean the database, eliminate duplicated rows and reindex fields. Throw away those fields that were not used and improve queries.

Third step was cleaning up the code, delete lines of unused code and repurpose a couple of functions. We also set a template engine (Smarty).

Now it seems the site is working fine server speed improved but still the database queries were too slow… what was left to do? Memcache!

First install memcache on the server. In my case I’m running CentOS so I just had to do:

yum install memcached

Then install php’s memcache support:

yum install php-pecl-memcache

Restart httpd and then type the following command on your server’s console

memcached -d -u apache -m 512 -p 11211

What this does is that it starts the memcached daemon with apache user privileges usin 512 Mb of RAM memory and using port 11211.

Since you’ll be needing port 11211 don’t forget to check your iptables config so you may be able to use it.

That’s it! everything is ready, the only thing left is modify your php’s code and you’re on your way.

I haven’t tested it but to be honest I really think it will save us lot’s of resources from the server. I’ll post the result in a few days.

Passing variables between templates (Smarty)

Tuesday, August 25th, 2009

For a couple of months now I’ve been working with the WCM system from Nstein.

A client uses it to deploy its website. It looks cool and easy to use but as I just said it looks so.

It uses Smarty templates to generate webpages and content. For this particular client WCM has been ‘pimped’ and a lot of stuff happened. First of all the use of templates is totally wasted, instead of having a top and footer templates you copy and paste the code anyway that’s not why I’m writing this post, the thing is that I had a tpl which was included inside another tpl nested in another tpl. When trying to set the value of one Smarty variable in the deepest nested tpl I couldn’t get it to work! Damn! a coworker helped me and after a couple of hours he gave me the answer.

Declare the variable on the highest level tpl, then assign it and finally call it through Smarty’s get_template_vars like this:

$variable_to_use = “some value”; //Declaration

$smarty_object->assign(“variable_assigned”,$variable_to_use); //Making it available for Smarty

$variable_to_use = get_template_vars(“variable_assigned”); //Call from the deepest nesting level tpl file

It’s really easy but I wasn’t aware of it.

I got so angry till my coworker gave me this tip that I thought it should be an experience to post in my blog so here it is.