Archive for the ‘Database’ Category

0061 | PHP simple object cache

Tuesday, June 2nd, 2009 Posted in Database, PHP Coding, Programming, Web Server | 3 Comments »

ระบบ cache มาอีกแล้ว เอ๊อกๆ
คราวนี้เป็น object cache ครับ ไม่ใช่ cache ทั้งหน้าแบบคราวก่อน

<?php
$_cachetime = 3600;
$_cacheroot = '/path/to/cache/';
function cacheget($key) {
	global $_cachetime, $_cacheroot;
	$_cachename = sprintf('%x', crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	if (file_exists($_cachefile) && (filemtime($_cachefile) > time() - $_cachetime || 
		(file_exists($_cachefile.'.lock') && time() - filemtime($_cachefile.'.lock') < 120))) {
		return unserialize(file_get_contents($_cachefile));
	}
	return false;
}
function cachelock($key) {
	global $_cacheroot;
	$_cachename = sprintf("%x", crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	umask(0);
	if (!file_exists($_cachefolder)) {
		mkdir($_cachefolder,0777);
	}
	touch($_cachefile.'.lock');
}
function cacheset($key, $value) {
	global $_cacheroot;
	$_cachename = sprintf('%x', crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	umask(0);
	if (!file_exists($_cachefolder)) {
		mkdir($_cachefolder,0777);
	}
	if ($_h = fopen($_cachefile, 'w')) {
		fwrite($_h, serialize($value));
		fclose($_h);
		unlink($_cachefile.'.lock');
	}
}
?>

วิธีติดตั้ง:

include ไฟล์ไปบนสุดเลย แก้ตัวแปร $_cacheroot กับ $_cachetime ด้วยนะครับ
chmod 777 folder ตามที่ตั้งใน $_cacheroot ไว้ด้วย

วิธีเรียกใช้:

สมมติว่า cache คำสั่ง sql นะครับ
ถ้าปกติใช้อย่างนี้

<?php
$query = 'SELECT * FROM table';
$sql = mysql_query($query);
while ($row = mysql_fetch_assoc($sql)) {
        var_dump($row);
}
?>

ก็แก้เป็นประมาณนี้

<?php
$query = 'SELECT * FROM table';
if (!$rows = cacheget($query)) {
        cachelock($query);
        $sql = mysql_query($query);
        $rows = array();
        while ($row = mysql_fetch_assoc($sql))
                $rows[] = $row;
        cacheset($query, $rows);
}
foreach($rows as $row) {
        var_dump($row);
}
?>

Tags: , ,

0031 | MySQL: my.cnf

Tuesday, September 2nd, 2008 Posted in Database | 2 Comments »

เอามาเก็บไว้ฮะ สำหรับแรม 4gb ฐานข้อมูลขนาด 1 gb

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1
set-variable=local-infile=0
skip-character-set-client-handshake
skip-name-resolve
skip-bdb
max_connections = 600
key_buffer = 512M
myisam_sort_buffer_size = 32M
join_buffer_size = 1M
read_buffer_size = 2M
sort_buffer_size = 4M
table_cache = 1536
thread_cache = 8
thread_concurrency = 4
thread_cache_size = 256
wait_timeout = 600
connect_timeout = 10
max_tmp_tables = 256
tmp_table_size = 64M
max_allowed_packet = 16M
max_connect_errors = 10
query_cache_limit = 1M
query_cache_size = 32M
query_cache_type = 1
query_prealloc_size = 16384
query_alloc_block_size = 16384
log_slow_queries
log_queries_not_using_indexes

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
open_files_limit = 8192

Tags: , ,