hoder.org

July 30, 2008

PHP Cache Control for dynamic pages

Filed under: Uncategorized — admin @ 4:10 pm

http://ontosys.com/php/cache.html

PHP Cache Control

This note describes a scheme for allowing PHP pages to be cached by a browser.

Example files

cache_check.inc — Logic to support caching of dynamic pages

<?php
$if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);

$mtime = filemtime($SCRIPT_FILENAME);
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';

if ($if_modified_since == $gmdate_mod) {
    header("HTTP/1.0 304 Not Modified");
    exit;
}
header("Last-Modified: $gmdate_mod");
?>

The value of the HTTP If-Modified-Since header (if any) is available in $HTTP_IF_MODIFIED_SINCE. We check the date value in that header against the modification date of the executed PHP script file itself. If they are the same, we send a 304 response and quit.

Otherwise, we send a Last-Modified header with the file’s modification date.

cachable.php3 — An example cacheable dynamically-generated file

<?php // -*- sgml-parent-document: ("dummy.html" "html" "body" ()) -*-
include 'cache_check.inc';

if (isset($touch))  touch($SCRIPT_FILENAME);
$gmdate_now = gmdate('D, d M Y H:i:s') . ' GMT';
$now = time();

print "
<table>
<tr><td>if_modified_since</td><td>$if_modified_since</td></tr>
<tr><td>gmdate_mod</td><td>$gmdate_mod</td></tr>
<tr><td>gmdate_now</td><td>$gmdate_now</td></tr>
</table>
<p>
<a href=\"$SCRIPT_NAME\">Link to self</a>.<br>
<a href=\"$SCRIPT_NAME?touch=y&time=$now\">Update source file</a>.<br>
<a href=\"$SCRIPT_NAME?time=$now\">Link to self with varying URL</a>.
";
?>

This dynamic page simply generates some output for testing purposes. Note that the gmdate_now value will not appear to change if the browser uses the file from its cache or if the server sends back a 304.

July 26, 2008

preg_replace

Filed under: Uncategorized — admin @ 11:07 pm

RewriteCond %{REQUEST_FILENAME} /(.*)-f([0-9]*).html

<?php
$patterns 
= array (‘/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/’
,
                   
‘/^\s*{(\w+)}\s*=/’
);
$replace = array (‘\3/\4/\1\2′, ‘$\1 =’
);
echo 
preg_replace($patterns, $replace, ‘{startDate} = 1999-5-27′
);
?>

July 16, 2008

Google checkout

Filed under: Uncategorized — admin @ 12:58 pm

Google checkout & paypal. What’s the difference ?

July 7, 2008

增加内存表的使用空间

Filed under: freebsd, mysql — admin @ 12:05 pm

如何增加内存表的使用空间

this is how to change it with my.cnf
/usr/local/mysql/bin/mysqld_safe –user=mysql    -O max_heap_table_size=320M  &

默认的是16M,å¯ä»¥æ ¹æ®è‡ªå·±çš„需è¦ï¼Œå¢žåŠ åˆ°éœ€è¦çš„大å°

July 4, 2008

Top 10 SQL PerformanceTips

Filed under: mysql — admin @ 1:12 pm

How to optimize your sql performance?

Check here

Top 1000 SQL Performance Tips

Interactive session from MySQL Camp I:

Specific Query Performance Tips (see also database design tips for tips on indexes):

  1. Use EXPLAIN to profile the query execution plan
  2. Use Slow Query Log (always have it on!)
  3. Don’t use DISTINCT when you have or could use GROUP BY
  4. Insert performance
    1. Batch INSERT and REPLACE
    2. Use LOAD DATA instead of INSERT
  5. LIMIT m,n may not be as fast as it sounds
  6. Don’t use ORDER BY RAND() if you have > ~2K records
  7. Use SQL_NO_CACHE when you are SELECTing frequently updated data or large sets of data
  8. Avoid wildcards at the start of LIKE queries
  9. Avoid correlated subqueries and in select and where clause (try to avoid in)
  10. No calculated comparisons — isolate indexed columns
  11. ORDER BY and LIMIT work best with equalities and covered indexes
  12. Separate text/blobs from metadata, don’t put text/blobs in results if you don’t need them
  13. Derived tables (subqueries in the FROM clause) can be useful for retrieving BLOBs without sorting them. (Self-join can speed up a query if 1st part finds the IDs and uses then to fetch the rest)
  14. ALTER TABLE…ORDER BY can take data sorted chronologically and re-order it by a different field — this can make queries on that field run faster (maybe this goes in indexing?)
  15. Know when to split a complex query and join smaller ones
  16. Delete small amounts at a time if you can
  17. Make similar queries consistent so cache is used
  18. Have good SQL query standards
  19. Don’t use deprecated features
  20. Turning OR on multiple index fields (<5.0) into UNION may speed things up (with LIMIT), after 5.0 the index_merge should pick stuff up.
  21. Don’t use COUNT * on Innodb tables for every search, do it a few times and/or summary tables, or if you need it for the total # of rows, use SQL_CALC_FOUND_ROWS and SELECT FOUND_ROWS()
  22. Use INSERT … ON DUPLICATE KEY update (INSERT IGNORE) to avoid having to SELECT
  23. use groupwise maximum instead of subqueries

Scaling Performance Tips:

  1. Use benchmarking
  2. isolate workloads don’t let administrative work interfere with customer performance. (ie backups)
  3. Debugging sucks, testing rocks!
  4. As your data grows, indexing may change (cardinality and selectivity change). Structuring may want to change. Make your schema as modular as your code. Make your code able to scale. Plan and embrace change, and get developers to do the same.

Network Performance Tips:

  1. Minimize traffic by fetching only what you need.
    1. Paging/chunked data retrieval to limit
    2. Don’t use SELECT *
    3. Be wary of lots of small quick queries if a longer query can be more efficient
  2. Use multi_query if appropriate to reduce round-trips

OS Performance Tips:

  1. Use proper data partitions
    1. For Cluster. Start thinking about Cluster *before* you need them
  2. Keep the database host as clean as possible. Do you really need a windowing system on that server?
  3. Utilize the strengths of the OS
  4. pare down cron scripts
  5. create a test environment
  6. source control schema and config files
  7. for LVM innodb backups, restore to a different instance of MySQL so Innodb can roll forward
  8. partition appropriately
  9. partition your database when you have real data — do not assume you know your dataset until you have real data

MySQL Server Overall Tips:

  1. innodb_flush_commit=0 can help slave lag
  2. Optimize for data types, use consistent data types. Use PROCEDURE ANALYSE() to help determine the smallest data type for your needs.
  3. use optimistic locking, not pessimistic locking. try to use shared lock, not exclusive lock. share mode vs. FOR UPDATE
  4. if you can, compress text/blobs
  5. compress static data
  6. don’t back up static data as often
  7. enable and increase the query and buffer caches if appropriate
  8. config params — http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/ is a good reference
  9. Config variables & tips:
    1. use one of the supplied config files
    2. key_buffer, unix cache (leave some RAM free), per-connection variables, innodb memory variables
    3. be aware of global vs. per-connection variables
    4. check SHOW STATUS and SHOW VARIABLES (GLOBAL|SESSION in 5.0 and up)
    5. be aware of swapping esp. with Linux, “swappiness” (bypass OS filecache for innodb data files, innodb_flush_method=O_DIRECT if possible (this is also OS specific))
    6. defragment tables, rebuild indexes, do table maintenance
    7. If you use innodb_flush_txn_commit=1, use a battery-backed hardware cache write controller
    8. more RAM is good so faster disk speed
    9. use 64-bit architectures
  10. –skip-name-resolve
  11. increase myisam_sort_buffer_size to optimize large inserts (this is a per-connection variable)
  12. look up memory tuning parameter for on-insert caching
  13. increase temp table size in a data warehousing environment (default is 32Mb) so it doesn’t write to disk (also constrained by max_heap_table_size, default 16Mb)
  14. Run in SQL_MODE=STRICT to help identify warnings
  15. /tmp dir on battery-backed write cache
  16. consider battery-backed RAM for innodb logfiles
  17. use –safe-updates for client
  18. Redundant data is redundant

Storage Engine Performance Tips:

  1. InnoDB ALWAYS keeps the primary key as part of each index, so do not make the primary key very large
  2. Utilize different storage engines on master/slave ie, if you need fulltext indexing on a table.
  3. BLACKHOLE engine and replication is much faster than FEDERATED tables for things like logs.
  4. Know your storage engines and what performs best for your needs, know that different ones exist.
    1. ie, use MERGE tables ARCHIVE tables for logs
    2. Archive old data — don’t be a pack-rat! 2 common engines for this are ARCHIVE tables and MERGE tables
  5. use row-level instead of table-level locking for OLTP workloads
  6. try out a few schemas and storage engines in your test environment before picking one.

Database Design Performance Tips:

  1. Design sane query schemas. don’t be afraid of table joins, often they are faster than denormalization
  2. Don’t use boolean flags
  3. Use Indexes
  4. Don’t Index Everything
  5. Do not duplicate indexes
  6. Do not use large columns in indexes if the ratio of SELECTs:INSERTs is low.
  7. be careful of redundant columns in an index or across indexes
  8. Use a clever key and ORDER BY instead of MAX
  9. Normalize first, and denormalize where appropriate.
  10. Databases are not spreadsheets, even though Access really really looks like one. Then again, Access isn’t a real database
  11. use INET_ATON and INET_NTOA for IP addresses, not char or varchar
  12. make it a habit to REVERSE() email addresses, so you can easily search domains (this will help avoid wildcards at the start of LIKE queries if you want to find everyone whose e-mail is in a certain domain)
  13. A NULL data type can take more room to store than NOT NULL
  14. Choose appropriate character sets & collations — UTF16 will store each character in 2 bytes, whether it needs it or not, latin1 is faster than UTF8.
  15. Use Triggers wisely
  16. use min_rows and max_rows to specify approximate data size so space can be pre-allocated and reference points can be calculated.
  17. Use HASH indexing for indexing across columns with similar data prefixes
  18. Use myisam_pack_keys for int data
  19. be able to change your schema without ruining functionality of your code
  20. segregate tables/databases that benefit from different configuration variables

Other:

  1. Hire a MySQL ™ Certified DBA
  2. Know that there are many consulting companies out there that can help, as well as MySQL’s Professional Services.
  3. Read and post to MySQL Planet at http://www.planetmysql.org
  4. Attend the yearly MySQL Conference and Expo or other conferences with MySQL tracks (link to the conference here)
  5. Support your local User Group (link to forge page w/user groups here)

 Authored by

Jay Pipes, Sheeri Kritzer, Bill Karwin, Ronald (”Jeremy Basher”) Bradford, Farhan “Frank Mash” Mashraqi, Taso Du Val, Ron Hu, Klinton Lee, Rick James, Alan Kasindorf, Eric Bergen, Kaj Arno, Joel Seligstein, Amy Lee

July 2, 2008

mstring installed as a port under FreeBSD

Filed under: PHP, freebsd — admin @ 11:25 pm

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\nowidctlpar\fi-4800\li4800\b\f0\fs20  Main >> Installing port in /usr/ports/converters/php5-mbstring\par
\par
Installing port in /usr/ports/converters/php5-mbstring\par
===>  Vulnerability check disabled, database not found\par
=> php-5.2.6.tar.bz2 doesn’t seem to exist in /usr/ports/distfiles/.\par
=> Attempting to fetch from http://br.php.net/distributions/.\par
php-5.2.6.tar.bz2                                     9346 kB  317 kBps\par
===>  Extracting for php5-mbstring-5.2.6\par
=> MD5 Checksum OK for php-5.2.6.tar.bz2.\par
=> SHA256 Checksum OK for php-5.2.6.tar.bz2.\par
===>  Patching for php5-mbstring-5.2.6\par
===>  Applying FreeBSD patches for php5-mbstring-5.2.6\par
===>   php5-mbstring-5.2.6 depends on file: /usr/local/bin/phpize - found\par
===>   php5-mbstring-5.2.6 depends on file: /usr/local/bin/autoconf-2.61 - found\par
===>  PHPizing for php5-mbstring-5.2.6\par
Configuring for:\par
PHP Api Version:         20041225\par
Zend Module Api No:      20060613\par
Zend Extension Api No:   220060519\par
===>  Configuring for php5-mbstring-5.2.6\par
configure: WARNING: you should use –build, –host, –target\par
checking for grep that handles long lines and -e… /usr/bin/grep\par
checking for egrep… /usr/bin/grep -E\par
checking for a sed that does not truncate output… /bin/sed\par
checking for amd64-portbld-freebsd6.2-gcc… cc\par
checking for C compiler default output file name… a.out\par
checking whether the C compiler works… yes\par
checking whether we are cross compiling… no\par
checking for suffix of executables… \par
checking for suffix of object files… o\par
checking whether we are using the GNU C compiler… yes\par
checking whether cc accepts -g… yes\par
checking for cc option to accept ISO C89… none needed\par
checking whether cc understands -c and -o together… yes\par
checking for system library directory… lib\par
checking if compiler supports -R… yes\par
checking build system type… amd64-portbld-freebsd6.2\par
checking host system type… amd64-portbld-freebsd6.2\par
checking target system type… amd64-portbld-freebsd6.2\par
checking for PHP prefix… /usr/local\par
checking for PHP includes… -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib\par
checking for PHP extension directory… /usr/local/lib/php/extensions/no-debug-non-zts-20060613\par
checking for PHP installed headers prefix… /usr/local/include/php\par
checking for re2c… re2c\par
checking for re2c version… 0.13.3 (ok)\par
checking for gawk… no\par
checking for nawk… nawk\par
checking if nawk is broken… no\par
checking whether to enable multibyte string support… yes, shared\par
checking whether to enable multibyte regex support… yes\par
checking whether to check multibyte regex backtrack… yes\par
checking for external libmbfl… no\par
checking how to run the C preprocessor… cc -E\par
checking for ANSI C header files… yes\par
checking for sys/types.h… yes\par
checking for sys/stat.h… yes\par
checking for stdlib.h… yes\par
checking for string.h… yes\par
checking for memory.h… yes\par
checking for strings.h… yes\par
checking for inttypes.h… yes\par
checking for stdint.h… yes\par
checking for unistd.h… yes\par
checking for variable length prototypes and stdarg.h… yes\par
checking for stdlib.h… (cached) yes\par
checking for string.h… (cached) yes\par
checking for strings.h… (cached) yes\par
checking for unistd.h… (cached) yes\par
checking sys/time.h usability… yes\par
checking sys/time.h presence… yes\par
checking for sys/time.h… yes\par
checking sys/times.h usability… yes\par
checking sys/times.h presence… yes\par
checking for sys/times.h… yes\par
checking stdarg.h usability… yes\par
checking stdarg.h presence… yes\par
checking for stdarg.h… yes\par
checking for int… yes\par
checking size of int… 4\par
checking for short… yes\par
checking size of short… 2\par

exim

Filed under: email, freebsd — admin @ 12:12 am

Main >> DNS Functions >> Edit MX Entry
/var/spool/exim/msglog/6

Current MX Entries
Domain   MX Entry Always Accept
hobid.com  0 hobid.com Delete
20 alt1.aspmx.l.google.com Delete
No Set To Yes
Main >> Service Configuration >> Exim Configuration Editor

Exim Configuration Editor
Configuration file passes test!  New configuration file was installed.
Enabled system filter options: attachments|fail_spam_score_over_200|spam_rewrite
Enabled ACL options in block ACL_RATELIMIT_BLOCK: 0tracksenders
Enabled ACL options in block ACL_RATELIMIT_SPAM_BLOCK: ratelimit_spam_score_over_200
Enabled ACL options in block ACL_RBL_BLOCK:
Enabled ACL options in block ACL_PRE_RECP_VERIFY_BLOCK: dictionary_attack
Enabled ACL options in block ACL_NOTQUIT_BLOCK: ratelimit
Enabled ACL options in block ACL_TRUSTEDLIST_BLOCK:
Enabled ACL options in block ACL_CONNECT_BLOCK: ratelimit|spammerlist
Enabled ACL options in block ACL_SPAM_BLOCK: deny_spam_score_over_200
Detected spam handling in acls, disabling spamassassin in routers & transports!.
SpamAssassin method remains unchanged
Configured options list is:
Provided options list is: hostlist senderverifybypass_hosts|hostlist skipsmtpcheck_hosts|hostlist spammeripblocks|hostlist backupmx_hosts|hostlist trustedmailhosts|domainlist user_domains|smtp_receive_timeout|ignore_bounce_errors_after|timeout_frozen_after|auto_thaw|callout_domain_negative_expire|callout_negative_expire|acl_smtp_connect|acl_smtp_notquit|spamd_address
Exim Insert Regex is: virtual_userdelivery|virtual_aliases|lookuphost|virtual_user|address_pipe|localuser
Exim Replace Regex is: virtual_sa_user|sa_localuser|virtual_sa_userdelivery|local_sa_delivery|central_filter|central_user_filter|democheck|fail_remote_domains|has_alias_but_no_mailbox_discarded_to_prevent_loop|literal|local_delivery|local_delivery_spam|localuser|localuser_spam|lookuphost|remote_smtp|userforward|virtual_aliases|virtual_aliases_nostar|virtual_user|virtual_user_spam|virtual_userdelivery|virtual_userdelivery_spam
Exim Match Insert Regex is: quota_directory|maildir_format
Exim version 4.69 #0 (FreeBSD 6.2) built 04-Jun-2008 21:38:13
Copyright (c) University of Cambridge 2006
Probably Berkeley DB version 1.8x (native mode)
Support for: crypteq iconv() IPv6 use_setclassresources PAM Perl Expand_dlfunc OpenSSL Content_Scanning Old_Demime
Lookups: lsearch wildlsearch nwildlsearch iplsearch cdb dbm dbmnz dnsdb dsearch nis nis0 passwd
Authenticators: cram_md5 dovecot plaintext spa
Routers: accept dnslookup ipliteral manualroute queryprogram redirect
Transports: appendfile/maildir/mailstore/mbx autoreply lmtp pipe smtp
Fixed never_users: 0
Size of off_t: 8
Exim Perl Load List is: spam_acl_support|checkuserquota|boxtrapper|safefile|fast_checkvalias|checkspam|checkspam2|fast_isdemo|fast_accountfunc|checkpass_cphulkd
/etc/exim.pl.local installed!
razor2 is not installed, disabling it in SpamAssassin to save memory
pyzor is not installed, disabling it in SpamAssassin to save memory
SPF is disabled in exim or unavailable, enabling SPF for SpamAssassin

Attempting to restart exim
Waiting for exim to restart…. . . . . . . . . . . finished.

exim statusmailnull 26474  0.0  0.1 15896  3484  ??  Ss    3:15PM   0:00.00 /usr/local/sbin/exim -bd -q30m (exim-4.69-0)
mailnull 26477  0.0  0.1 15896  3448  ??  Ss    3:15PM   0:00.00 /usr/local/sbin/exim -tls-on-connect -bd -oX 465 (exim-4.69-0)

exim started ok
Your configuration changes have been saved!

Main >> Hostname A Entry Missing!

Hostname A Entry Missing!
The server was unable to lookup an an A entry for its hostname (newinst.layeredtech.com). This is generally because the entry was never added. However this could also be the result of your nameserver(s) being down. If you would like to attempt to automatically add the entry, .

Main >> Software >> Update Server Software

Update Server Software
cPanel Package Upgrades in Progress…
Ftp Setup Script Version 6.1
This is the pure-ftpd installer
Searching ports for pure-ftpd ……………………………….found pure-ftpd in /usr/ports/ftp/pure-ftpd….Done
pure-ftpd (1.0.21-2) is already installed.
MySQL Setup Script Version 7.0
This is the MySQL installer for OS FreeBSD
Searching ports for mysql50-client ………………………..found mysql50-client in /usr/ports/databases/mysql50-client….Done
mysql50-client (5.0.51a) is already installed.
Searching ports for mysql50-server ………………………..found mysql50-server in /usr/ports/databases/mysql50-server….Done
mysql50-server (5.0.51a) is already installed.
Install Complete
bandmin Setup Script Version 1.0
courier-imap Setup Script Version 1.0
This is the courier-imap installer for OS FreeBSD
Source: packages-6.2-release
looking up ftp5.de.freebsd.org
connecting to ftp5.de.freebsd.org:21
fetch: ftp://ftp5.de.freebsd.org/pub/FreeBSD/ports/amd64/packages-6.2-release/INDEX: Operation timed out
Source: packages-6-stable
looking up ftp5.de.freebsd.org
connecting to ftp5.de.freebsd.org:21
fetch: ftp://ftp5.de.freebsd.org/pub/FreeBSD/ports/amd64/packages-6-stable/INDEX: Operation timed out
Source: packages-6.2-release
looking up ftp.ua.freebsd.org
connecting to ftp.ua.freebsd.org:21
binding data socket
initiating transfer
remote size / mtime: 8252233 / 1164048316
/root/.cpbsdpkgs/6-8-2008.INDEX                       8058 kB  380 kBps
gdbm (1.8.3_2) is already installed.
Searching ports for courier-authlib ……………………………………………………….found courier-authlib in /usr/ports/security/courier-authlib….Done
courier-authlib (0.60.2) is already installed.
Searching ports for courier-imap ………………………………………..found courier-imap in /usr/ports/mail/courier-imap….Done
courier-imap (4.3.1,2) is already installed.
No restart required
Install Complete
Exim (maildir) Setup Script Version 20.0
exim (4.69) is already installed.
exim (4.69) is already installed.
Searching ports for portupgrade …………………………………………………..found portupgrade in /usr/ports/ports-mgmt/portupgrade….Done
portupgrade (2.4.3-2,2) is already installed.
openssh is installed
exim is installed
rdate is installed
bash is installed
ncftp is installed
wget is installed
jpeg is installed
python is installed
imap-uw is installed
png is installed

Powered by hoder.org