Category Archives: Chỉ dẫn lập trình

PHP Memcache and APC extensions

What is Memcache ?

Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

What is APC ?

APC Cache (Eaccelerator and other similar caches) is Fast but it is not distributed so you’re wasting cache and reducing possible hit rate by caching things locally if you have many web servers. MemcacheD is relatively slow but distributed and so you do not waste memory by caching same item in a few places, it is also faster to warmup as you need only one access to bring item into the cache, not access for each of web servers.

The good thing however is you do not have to select one or another, you can use both at the same time. APC will be great for caching small but frequently accessed things which are not taking too much memory. For example if you store list of states in the database you can cache it this way. For NNSEEK we can use it to cache list of languages, top groups and much of semi-static modules shown on group directory pages. Memcached is good for caching things which take large amount of space combined and which you only need to fetch few per page. For example search results may be good candidate (assuming we want to cache them and want to cache them in memory).

– Linux: memcache.so, apc.so (apt-get install or pecl)
– Windows: php_memcache.dll, php_apc.dll (google to get)

MVC – Model View Controller & PHP

The browser makes a request to web server by request URL.

The web server (Apache, WEBrick, etc.) receives the request. It uses routes to find out which controller to use: the default route pattern is “/controller/action/id” as defined in configuration file. The web server then uses the dispatcher to create a new controller, call the action and pass the parameters.

Controllers do the work of parsing user requests, data submissions, cookies, sessions and the “browser stuff”.

Models are PHP classes. They talk to the database, store and validate data, perform the business logic and otherwise do the heavy lifting.

Views are what the user sees: HTML, CSS, XML, Javascript, JSON. They’re the sales rep putting up flyers and collecting surveys, at the manager’s direction. Views are merely puppets reading what the controller gives them. They don’t know what happens in the back room.

The controller returns the response body (HTML, XML, etc.) & metadata (caching headers, redirects) to the server. The server combines the raw data into a proper HTTP response and sends it to the user.

Mảng trong shell script

In ra tháng hiện tại bằng tiếng Việt không dấu.
# Khai báo mảng (chỉ số từ 0)
a=( mot hai ba bon nam sau bay tam chin muoi “muoi mot” “muoi hai” )
# Lấy số phần tử của mảng
n=${#a[@]}
# Lấy giá trị phần tử $i của mảng
v=${a[$i]}
# Thay gia tri phan tu thu 1 cua mang
a[1]=”mot moi”
m=`date “+%B”`
for (( i=0; i < $n; i++ )) do     if [ m –eq $i ]; then         echo "Thang hien tai la thang ${a[$i]}"         # Thoát khỏi vòng for         break     fi done

PHP CLI vs PHP CGI

In many ways, the CLI SAPI works in the same way as the CGI SAPI and other SAPIs. However, it is important to keep track of where it differs and why, because it will directly affect how you write your scripts.
These are the most important differncies between CLI and CGI:

* Unlike the CGI SAPI, CLI writes no headers to the output by default
* There are some php.ini directives which are overridden by the CLI SAPI because they do not make sense in shell environments:

  • html_errors: CLI default is FALSE
  • implicit_flush: CLI default is TRUE
  • max_execution_time: CLI default is 0 (unlimited)
  • register_argc_argv: CLI default is TRUE

* You can have command line arguments with your script! Variable “$argc” provides you with a number of arguments passed to the application. And array “$argv” gives you an array of the actual arguments
* There are 3 new constant defined for the shell enviroment: STDIN, STDOUT, STDERR. All of them are file handlers for correspondent shell devicies. For example STDIN is the handler for fopen(‘php://stdin’, ‘r’) . So, you can read a line from STDIN like this: $strLine = trim(fgets(STDIN));. STDIN is already defined for you by PHP CLI!
* PHP CLI does not change the current directory to the directory of the executed script. The current directory for the script would be the directory where your type PHP CLI command.
* There are number of USEFUL options avaiable for PHP CLI. Which will allow you to get some valuable information about you php setup, your php script or run it in defferent modes.
* In PHP 5 there were some changes in CLI and CGI filenames. In PHP 5, the CGI version was renamed to php-cgi.exe (previously php.exe) and the CLI version now sits in the main directory (previously cli/php.exe).
* In PHP 5 it was also introduced a new mode: php-win.exe. This is equal to the CLI version, except that php-win doesn’t output anything and thus provides no console (no “dos box” appears on the screen). This behavior is similar to PHP GTK.

More information: http://www.php-cli.com/