29 June 2010 View Comments

mysqlind_qc: Client Side Caching for MySQL extensions for PHP

mysqlind_qc: Client Side Caching for MySQL extensions for PHP

Here’s something in the making, a PHP extension which caches MySQL queries irrespective of the extension being used. It’s a plugin for mysqlnd, the native MySQL driver for PHP called  mysqlnd_qc. This currently requires PHP 5.3.3-dev which is still in development.

The extension allows caching buffered queries through mysql_query and mysqli_query. It allows cache-invalidation though TTL or a user defined callback. This extension is still in the protoype stage right now and will evolve as the project progresses.

The Query Cache is implemented as a PHP extension. It is written in C and operates “under the hood” of PHP. During the start up of the PHP interpreter it gets registered as a mysqlnd plugin to replaces selected mysqlnd methods.

At PHP run time it proxies queries send from mysqlnd/PHP to the MySQL server. If a query string starts with the SQL hint (/*qc=on*/) to enable caching of it and the query is not cached (Cache miss), the query cache plugin will record the raw wire protocol data send from MySQL to PHP to answer the query. The query cache records the wire protocol data in its cache medium and replays it, if still valid, on a cache hit.

Note that the query cache does not hold decoded result sets consisting of zvals (C struct representing a PHP variable). It stores the raw wire data of the MySQL client server protocol. In case of a cache hits, mysqlnd still needs to decode the cached raw wire data into PHP variables before passing the result to the user space. This approach has one major advantage: simplicity.

You can get more information and installation instructions over at the MySQLND Query Cache Project page at Mysql Forge.

Check out this slide which gives you more information about this extension:

Enhanced by Zemanta

27 May 2010 View Comments

Create RESTful Webservices in minutes with FRAPI

Create RESTful Webservices in minutes with FRAPI

Planning on starting a project with REST webservices? You’ve gotta check out this new framework called FRAPI. This API framework built in PHP eases development of REST webservices, by allowing you to add actions, responses and even create users and assign API keys to them for accessing the services you create. Here’s a video showing an overview of FRAPI and how simple it is to deploy and manage webservices.

This project is still quite new and their documentation is down to the bare minimum. They do have the required documentation for you you’ll need to get your hands dirty with FRAPI. First you’ll have to download the sources and set it up on your own. One you’ve downloaded the files and setup the directory permissions, you’ll also have to make the Apache Virtual host settings to get FRAPI to start working.

FRAPI has the following requirements for it to run:

  • PHP 5.2.2 or later
  • APC: Required for caching of actions, errors, etc.
  • HTTP: (http://pecl.php.net/pecl_http
  • PEAR: (http://pear.php.net)
  • PEAR::HTTP_Request2: This could be replaced by pecl_http however it is used in the bundled ArmChair package which is used to access CouchDB (http://pear.php.net/HTTP_Request2)

Some of the advantages I see after playing around with FRAPI is that it auto-generates the code skeleton for you to work on so you just have to go fill in the blanks to create your service. You also don’t have to worry about transfoming the your data into different formats, FRAPI supports JSON, XML, PHP, and plain text natively. This enables you to make your webservices cross-platform and even power mobile apps. You also don’t have to tweak around with code which handles http requests and responses while creating your webservices.

For more information on this project, head over to their site  getfrapi.com or their project page on github.

Reblog this post [with Zemanta]

18 May 2010 View Comments

Check your server load before you process

Check your server load before you process

Most of us just write applications to be deployed on a webserver without thinking about what to do when the server becomes overloaded. What happens in most cases is that the application would go trying to run itself on every request, and on a heavily loaded system, it just goes on the aggravate the problem, making increasing the load on the server, till finally the server becomes unreachable.

What if you could actually check the server load in your PHP application? Would you think about checking the server load before doing some heavy computational task or database accesses? There’s function in PHP which will allow you to check the load averages on a server.

The sys_getloadavg() in PHP gives you the load averages for your server. You can use this to check the load on your server before processing a request.

$serverload = sys_getloadavg();
print_r($serverload);

The code above gives the output:

Array
(
    [0] => 1.07
    [1] => 0.89
    [2] => 1
)

The output from the code shows the system load averages where [0] is the load averages for the past 1 minute, [1] is for the past 5 minutes and [2] is for the past 15 minutes.

On an ideal server, the load averages on the server shouldn’t go above 3. A load average of more than 15 would mean that the server is already running much lower than normal, and you may not want to add more load to this.

You can use this output to decide whether to serve a process intensive page depending on the server load. Here’s a pseudo-code on how you’d do something like this:

$serverload = sys_getloadavg();
if ($serverload[0]<10)
  {
    // process loads of data now
    some_big_process();
  } else
     {   // Send a 503 header stating that the server is overloaded.
         header('HTTP/1.1 503 Too busy, try again later');
         die('The server is busy at present and cannot process your request.');
      }

You can also run the application in such a way that you can wait till the load on the server decreases to run your process. This is useful if you are running a cron for processing data:

set_time_limit(0);
//set time limit to 0, so PHP's max execution time doesn't interfere with the processing script
$serverload = sys_getloadavg();
//Check load and see if it's low enough to start processing
while ($serverload[0]>5)
{  //Wait for 1 minute to check load again
    sleep(60);
}
// out of the loop - so let's do some processing now!
some_big_process();

To ensure that the script doesn’t run endlessly on a permanently overloaded server, time check in the while loop to auto-end the script it the server load remains high for a certain amount of time.

More reading:
Load & Load Averages on Wikipedia
PHP function – sys_getloadavg

4 January 2010 View Comments

Practical PHP Testing – Free Ebook

Practical PHP Testing – Free Ebook

If you’re new to testing in PHP, and were wondering how to create and run automated tests, here’s an ebook which will help you get the basics right.

Practical PHP Testing is an ebook which is a compilation of  articles from Giorgio Sironi’s blog on Practical PHP testing.

This book takes you though the basics of PHPUnit – how to install it and start writing simple tests using PHP Unit. Here are some of what this ebook covers:

  • bonus chapter on TDD theory;
  • a case study on testing a php function;
  • working code samples, some of whom were originally kept on pastebin.com;
  • sets of TDD exercises at the end of each chapter;
  • glossary that substitutes external links to wiki and other posts, to not interrupt your reading with terms lookup.

More information and download link is available here.

Reblog this post [with Zemanta]

14 July 2009 View Comments

PHP 5.3 & Internationalization

Unicode_sample

Unicode_sampleStas Malyshev has written a good article over at Zend Developer Zone about the new intl – Internalization extension in PHP 5.3. Internationalization has been a major problem with web developers when developing sites which have to support multiple languages. The new extension eases the developer’s work drastically. Stas mentions in the article that the following modules have been implemented in the Intl extension:

  • Locale — deals with breaking locale data into components, assembling a locale string from components and displaying the names of countries, languages etc in a specified locale.
  • Collator — a means of comparing and sorting strings according to local rules.
  • Number formatter — allows you to format numbers in a variety of ways, and to parse textual representations of numbers.
  • Date formatter — allows you to format dates and to parse textual representations of dates.
  • Message formatter — allows you to compose messages from parameterized strings while formatting the data inside according to local rules and allowing choices dependent on the actual parameter value.
  • Normalizer — a means of bringing a Unicode string to a standard, unambiguous representation.
  • Grapheme module – handles parsing a string into a set of graphemes.
  • IDN – handles internationalized domain names format

Read Stas’ article to learn how to use the new features this extension provides to make your website Internationalization ready.

If you’re using PHP5.2, you’re in luck since there is a PECL package which should run on PHP 5.2.4 and newer over at: http://pecl.php.net/package/intl

Link: http://devzone.zend.com/article/4799-Internationalization-in-PHP-5.3

Image via Wikipedia

Reblog this post [with Zemanta]

Tags: ,
28 May 2009 View Comments

PHP Compiler Internal Slides by Sebastian Bergmann

phpcompilerinternals

Here’s an interesting slide from Sebastian Bergmann which explains the magic that happens when you execute your PHP code, and how it gets converted into bytecode. He also goes ahead to show you how to extend the PHP compiler in the presentation.

Reblog this post [with Zemanta]

12 May 2009 View Comments

PHP Quick Profiler – Profile your Code

PHP Quick Pofiler

PHP Quick Pofiler

Here’s a good looking and functional profiler you can plug into your projects. PQP (PHP Quick Profiler) allows you to profile your code and get information about how much time your script took to execute, the amount of memory used, the files which have been included, errors in your code, queries and any other debug information you would like to show up.

With minor changes to your code, it’s easy to get information on the console, memory and file utilizations. Getting information about your queries and load times, you’ll have to follow the instructions over at:  http://particletree.com/features/php-quick-profiler/.You can also send your exceptions, variable dumps and custom messages sent to the console.

See a demo of PHP Quick Profiler at - http://particletree.com/examples/pqp/

Get more information and download PHP Quick Profiler at: http://particletree.com/features/php-quick-profiler/

Reblog this post [with Zemanta]

8 January 2009 View Comments

PHP functions in Javascript using PHP.JS

logosquare

PHP.JS Logo

PHP programmers usually have to handle HTML & Javascript front-end coding as well and I’m sure a lot of us have been frustrated with the lack of those easy to use PHP functions in Javascript like those array, encode/decode and string functions. Here’s a Javascript project which provides you just that.

PHP.JS is an open source project started by which aims at providing the standard PHP functions for Javascript development. This eases up programming in Javascript for PHP developers as they can use functions they are familiar with. Currently the project has ported around 230+ PHP functions.

To use this library all you need to do is to download the php.js file and include it in your page:

<script src="/pathto/php.js"></script>

Then use use the function as you would in PHP. For example if you want to use the urlencode function in Javascript, just use it like this in Javascript

urlencode('http://blogs.vinuthomas.com/');

This should return ‘http%3A%2F%2Fblogs.vinuthomas.com%2F’. As easy as that !

Edit: As Kevin mentioned in the comments below, don’t use the entire php.js file in your projects, just use the function you need.

History of PHP.JS from (phpjs.org):

A developer called Kevin van Zonneveld was once working on a project with a lot of client(JS) / server(PHP) interaction, and he found himself coding PHP functions (like base64_decode & urldecode) in JavaScript to smoothen communication between the two languages.

He stored the stored the functions in a file called php.js which was included in the project. But even when the project was done, it remained fun trying to port PHP functions to JavaScript and so the library grew.

Kevin decided to share the little library online, which triggered the enthusiasm of a lot of PHP developers longing for PHP functionality in JavaScript. The project was open sourced in 2008, and many people contributed their own functions in the comments sections of Kevin’s blog.

It was decided that the library deserved a bigger home, and a face of its own, and so the PHP.JS core team (which at that time also consisted of Michael White, Felix Geisendörfer, Philip Peterson) developed the phpjs.org website

Links:
PHP.JS Home Page
List of Functions available in PHP.JS
Download PHP.JS

Reblog this post [with Zemanta]

8 October 2008 View Comments

Roll out your own Social Network using Elgg

elgg

If you’re thinking of rolling out a Social Network project, try Elgg. You can launch your own Social Network without writing a line of code. :)

Elgg is a open source social network platform built on PHP. The best about Elgg is the ability to extend the base platform’s functionality by downloading or writing your own plugins. Some of the plugins currently available include – a blog engine, social bookmarks, message boards, OpenID signup and login, and more. For a complete list of plugins that Elgg supports, head over to: http://docs.elgg.org/wiki/Features

Elgg also won an award at Infoworld’s 2008 Best of Open Source  Awards.

Link: Elgg Official Site, Dowload Elgg, Documentation

Reblog this post [with Zemanta]

13 August 2008 View Comments

Zend Framework to get AMF support

Zend Framework to get AMF support

Andi Gutmans has announced that Adobe will be contributing towards AMF (Action Message Format) support on Zend Framework. This will allow with apps made in Flex or Adobe Air to communicate with PHP. Earlier, libraries like AMF-PHP allowed this interaction between Flash and PHP.

Adobe’s proposal for this support on the Zend Framework can be viewed over at: http://gourl.in/1p

Andi mentions over at his blog that AMF support is targetted for the Zend Framework 1.7 version.

More over at Andi’s Blog.

Reblog this post