Coding

Sử dụng session_set_save_handler để override session handle

 Việc scale (vertical) ứng dụng web sử dụng php  , có một vấn đề cần quan tâm hàng đầu là vấn đề sử lý session .

 
Khi scale php sẽ chạy trên các máy chủ khác nhau , vậy làm thế nào để các user khi login web app mà dùng chung được session trên nhiều server ?
 
Để làm được điều này chúng ta cần xử lý sao cho tất cả các máy chủ đều sử dụng chung một hệ thống quản lý session .
 
Hoặc đơn giản là sử dụng chung một session storage .
 
Mặc định trong php sẽ sử dụng file session handle các file chứa session sẽ nằm trong /tmp của mỗi server .
 
Điều này có thể gợi ý cho chúng ta việc share chung 1 vùng lưu trữ (ví dụ dùng NFS để share chung /tmp giữa các máy chủ).
 
Tuy nhiên cách này lại ảnh hưởng đến tốc độ truy xuất (vì phải qua mạng - sau đó là đến việc máy host session sẽ quá tải diskio).
 
Một hướng khác là sử dụng một db mysql chung để lưu session , thực tế thì cách này tạm được .
 
Để làm như vậy chúng ta cần override lại các hàm xử lý session của php
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
 
Sử dụng hàm session_set_save_handler để trỏ các call back function cần thiết , như vậy chúng ta cần viết lại 6 hàm open, close , read ,write ,destroy , gc .
 
tạm thời chúng ta viết tạm các hàm như sau :
<?php
function open($save_path, $session_name)
{
echo "open";
}
 
function close()
{
echo "close";
}
 
function read($id)
{
echo "read";
}
 
function write($id, $sess_data)
{
echo "write";
 
}
 
function destroy($id)
{
echo "destroy";
}
 
function gc($maxlifetime)
{
echo "gc";
}
 
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
// test thử phát
session_start();
 
?>
 
Chạy thử . kết quả : openread writeclose ...
 
Như vậy chúng ta thấy thứ tự của một quá trình đọc session .
 
Việc tiếp theo là implement mấy hàm trên vào mysql , memcached , file , hay bất kỳ một storage nào có thể dùng chung được là xong .
 
với file (cái này chỉ là làm lại những gì php đã làm :D )
<?php
$sess_save_path=".";
function open($save_path, $session_name)
{
global $sess_save_path;
 
$sess_save_path = $save_path;
return(true);
}
 
function close()
{
return(true);
}
 
function read($id)
{
global $sess_save_path;
 
$sess_file = "$sess_save_path/sess_$id";
return (string) @file_get_contents($sess_file);
}
 
function write($id, $sess_data)
{
global $sess_save_path;
 
$sess_file = "$sess_save_path/sess_$id";
if ($fp = @fopen($sess_file, "w")) {
$return = fwrite($fp, $sess_data);
fclose($fp);
return $return;
} else {
return(false);
}
 
}
 
function destroy($id)
{
global $sess_save_path;
 
$sess_file = "$sess_save_path/sess_$id";
return(@unlink($sess_file));
}
 
function gc($maxlifetime)
{
global $sess_save_path;
 
foreach (glob("$sess_save_path/sess_*") as $filename) {
if (filemtime($filename) + $maxlifetime < time()) {
@unlink($filename);
}
}
return true;
}
 
session_set_save_handler("open", "close", "read", "write", "destroy", "gc");
 
//test
session_start();
 
?>
 
Hay với mysql đại khái thế này :
function read($id)
{
$sql = "select * from `tb_session` where `session_id`=$id";
mysql_query($sql);
//....
}
 
function write($id, $sess_data)
{
$sql = "INSERT INTO `tb_session` (`session_id`, `updated_on`) VALUES ('{$this->session_id}', NOW())";
mysql_query($sql);
 
}
 
function destroy($id)
{
$sql = "Delete from `tb_session` where `session_id`=$id";
mysql_query($sql);
}
 
Hay với memcached :
function read($id)
{
return memcached::get("sessions/{$id}");
}
 
function write($id, $data)
{
return memcached::set("sessions/{$id}", $data, 3600);
}
 
function destroy($id)
{
return memcached::delete("sessions/{$id}");
}
 
Dùng memcache hay mysql cũng khá nhanh , mysql thì chạy bàng memory storage engine , bật query cache tướng đối lớn cho nhanh , có thể replication ra nhiều server để đẩy performed lên cao nữa nếu tải lớn .

80 Bình luận

  1. Golden Goose nói:

    https://www.nikefreesale.com/ nike free run womens, https://www.goldengoosedeluxebrandsneakers.us/ golden goose shoes, https://www.philippemodeloutlet.com/philippe-model-mens-c-1/ Philippe Model Mens, https://www.nikefreesale.com/ nike free run shoes outlet, https://www.airmax270site.com/nike-air-max-270-c-1/ air max 270, https://www.goldengoosesneakersale.us/ golden goose, https://www.goldengooser.com/ Golden Goose, https://www.airmax270site.com/ cheap air max 270, https://www.goldengoosedeluxebrandsneakers.us/ golden goose deluxe brand sneakers, https://www.newgoldengoosesneakers.com/golden-goose-hi-star-c-3/ Golden Goose Hi Star, https://www.airmax95shoes.com/nike-air-max-95-womens-c-2/ Nike Air Max 95 Womens, https://www.ggdbsneakersale.com/ggdb-hi-star-c-3/ GGDB Hi Star, https://www.saleggdbshoes.com/ golden goose sale, https://www.goldengoosesneakersale.us/ golden goose sneakers sale, https://www.airmax270site.com/ air max 270, https://www.ggdbsneakersale.com/ GGDB, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-superstar-c-7/ golden goose superstar, https://www.nikefreesale.com/ nike free run shoes, https://www.ggdbsneakersale.com/ggdb-superstar-c-7/ GGDB Super Star, https://www.saleggdbshoes.com/ggdb-mid-star-c-4/ GGDB Mid Star, https://www.ggdbsneakersale.com/ GGDB Sneakers Sale, https://www.yeezybooststock.com/yeezy-boost-700-c-5/ yeezy boost 700, https://www.airmax95shoes.com/ cheap air max 95, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-hi-star-c-3/ golden goose hi star, https://www.converse-outlet.com/converse-seasonal-color-c-6/ Converse Seasonal Color, https://www.goldengoosesneakerser.com/ golden goose sale, https://www.goldengoosesneaker.us/golden-goose-mid-star-c-4/ golden goose mid star, https://www.nikefreesale.com/nike-free-60-c-24/ nike free 6.0, https://www.mrgoldengoose.com/golden-goose-slide-c-13/ golden goose slide, https://www.airvapormaxsale.com/ Air VaporMax Outlet, https://www.goldengoosesneaker.us/golden-goose-hi-star-c-3/ golden goose hi star, https://www.goldengooseshoesit.com/golden-goose-starter-c-6/ Golden Goose Starter, https://www.ggdbsneakersale.com/ggdb-mid-star-c-4/ GGDB Mid Star, https://www.saleggdbshoes.com/ggdb-slide-c-5/ GGDB Slide, https://www.nikefreesale.com/nike-free-40-c-22/ nike free 4.0, https://www.philippemodelsale.com/philippe-moldel-man-c-1/ Philippe Model Man, https://www.converse-outlet.com/ Converse Outlet, https://www.goldengoosesneaker.us/golden-goose-v-star-c-8/ golden goose v star, https://www.yeezybooststock.com/ adidas yeezy boost, https://www.goldengooser.com/v-star-c-3/ golden goose v star, https://www.yeezybooststock.com/powerphase-calabasas-c-6/ powerphase calabasas, https://www.goldengoosesneakerser.com/golden-goose-slide-c-5/ golden goose slide, https://www.philippemodeloutlet.com/ philippe model outlet, https://www.philippemodel.us.com/philippe-model-women-sneakers-c-2/ Philippe Model Women Sneakers, https://www.goldengoosesneakerser.com/golden-goose-hi-star-c-3/ golden goose hi star, https://www.goldengooseshoesit.com/ Golden Goose Sale, https://www.nikefreesale.com/ nike free run shoes sale, https://www.ggdbsneakersale.com/ggdb-francy-c-2/ GGDB Francy, https://www.goldengoosesneaker.us/golden-goose-francy-c-2/ golden goose francy, https://www.nikelunarsale.com/ nike lunar shoes, https://www.saleggdbshoes.com/ggdb-hi-star-c-3/ GGDB Hi Star, https://www.goldengoosedeluxebrandsneakers.us/ golden goose deluxe brand shoes, https://www.mrgoldengoose.com/golden-goose-hi-star-c-16/ golden goose hi star, https://www.goldengoosesneakerser.com/golden-goose-mid-star-c-4/ golden goose mid star, https://www.airvapormaxsale.com/ Air VaporMax, https://www.philippemodeloutlet.com/ philippe model outlet online, https://www.airmax95shoes.com/ air max 95, https://www.saleggdbshoes.com/ ggdb shoes, https://www.yeezybooststock.com/ yeezy boost 350 v2, https://www.philippemodelsale.com/ Philippe Model Shoes, https://www.goldengoosesneaker.us/ golden goose outlet, https://www.nikefreesale.com/nike-free-flyknit-c-26/ nike free flyknit, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-francy-c-2/ golden goose francy, https://www.nikefreesale.com/nike-fs-lite-c-27/ nike free fs lite, https://www.nikelunarsale.com/lunarglide-8-c-12/ nike lunarglide 8, https://www.airmax95shoes.com/ nike air max 95, https://www.goldengoosesneakerser.com/golden-goose-superstar-c-7/ golden goose superstar, https://www.newgoldengoosesneakers.com/golden-goose-superstar-c-7/ Golden Goose Superstar, https://www.mrgoldengoose.com/golden-goose-francy-c-10/ golden goose francy, https://www.goldengoosesneakerser.com/golden-goose-starter-c-6/ golden goose starter, https://www.goldengoosesneakersale.us/ golden goose outlet, https://www.saleggdbshoes.com/ggdb-superstar-c-7/ GGDB Superstar, https://www.newgoldengoosesneakers.com/golden-goose-starter-c-6/ Golden Goose Starter, https://www.mrgoldengoose.com/ golden goose, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-starter-c-6/ golden goose starter, https://www.airmax95shoes.com/nike-air-max-95-mens-c-1/ Nike Air Max 95 Mens, https://www.goldengoosesneaker.us/golden-goose-super-star-c-7/ golden goose super star, https://www.newgoldengoosesneakers.com/ Golden Goose, https://www.goldengoosesneaker.us/ golden goose, https://www.goldengoosedeluxebrandsneakers.us/ golden goose sneakers, https://www.nikefreesale.com/nike-free-advantage-c-25/ nike free advantage, https://www.ggdbsneakersale.com/ Golden Goose Sneakers, https://www.goldengooser.com/superstar-c-4/ golden goose superstar, https://www.mrgoldengoose.com/golden-goose-mid-star-c-14/ golden goose mid star, https://www.mrgoldengoose.com/ ggdb sneakers, https://www.goldengooseshoesit.com/golden-goose-v-star-c-8/ Golden Goose V Star, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-v-star-c-8/ golden goose v star, https://www.newgoldengoosesneakers.com/golden-goose-francy-c-2/ Golden Goose Francy, https://www.airvapormaxsale.com/air-vapormax-cs-c-9/ Air VaporMax CS, https://www.goldengooser.com/ball-star-c-1/ golden goose ball star, https://www.yeezybooststock.com/ yeezy boost, https://www.philippemodelsale.com/philippe-moldel-woman-c-2/ Philippe Model Woman, https://www.airvapormaxsale.com/air-vapormax-plus-c-3/ Air VaporMax Plus, https://www.ggdbsneakersale.com/ggdb-slide-c-5/ GGDB Slide, https://www.nikelunarsale.com/ nike lunar on sale, https://www.goldengooser.com/ Golden Goose Sneakers, https://www.nikefreesale.com/ nike free run mens, https://www.goldengoosesneakerser.com/ golden goose, https://www.airvapormaxsale.com/ Nike Air VaporMax Shoes, https://www.nikefreesale.com/nike-free-tr-c-30/ nike free tr, https://www.goldengooser.com/francy-c-2/ golden goose francy, https://www.newgoldengoosesneakers.com/ Golden Goose Sneakers, https://www.mrgoldengoose.com/golden-goose-v-star-c-11/ golden goose v star, https://www.nikefreesale.com/nike-free-rn-c-29/ nike free rn, https://www.airmax270site.com/ air max 270 sale, https://www.saleggdbshoes.com/ggdb-starter-c-6/ GGDB Starter, https://www.goldengoosesneaker.us/golden-goose-ball-star-c-1/ golden goose ball star, https://www.goldengooseshoesit.com/golden-goose-slide-c-5/ Golden Goose Slide, https://www.yeezybooststock.com/yeezy-boost-350-v2-c-2/ yeezy boost 350 v2, https://www.goldengooseshoesit.com/ Golden Goose Outlet, https://www.philippemodelsale.com/ Philippe Model Sale, https://www.yeezybooststock.com/ yeezy boost 350, https://www.airvapormaxsale.com/air-vapormax-moc-c-5/ Air VaporMax Moc, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-ball-star-c-1/ golden goose ball star, https://www.goldengooser.com/ Golden Goose Sale, https://www.converse-outlet.com/ Converse Shoes, https://www.philippemodeloutlet.com/philippe-model-womens-c-2/ Philippe Model Womens, https://www.converse-outlet.com/converse-1970s-c-1/ Converse 1970s, https://www.nikelunarsale.com/ nike lunar mens, https://www.philippemodelsale.com/ Philippe Model Sneakers, https://www.philippemodel.us.com/ Philippe Model, https://www.ggdbsneakersale.com/ggdb-ball-star-c-1/ GGDB Ball Star, https://www.airmax270site.com/ nike air max 270, https://www.converse-outlet.com/converse-low-top-c-4/ Converse Low Top, https://www.airmax95shoes.com/ air max 95 outlet, https://www.goldengooseshoesit.com/ Golden Goose Shoes, https://www.converse-outlet.com/converse-monochrome-c-5/ Converse Monochrome, https://www.airmax95shoes.com/ air max 95 sale, https://www.mrgoldengoose.com/golden-goose-starter-c-15/ golden goose starter, https://www.goldengooseshoesit.com/golden-goose-mid-star-c-4/ Golden Goose Mid Star, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-slide-c-5/ golden goose slide, https://www.philippemodel.us.com/ Philippe Model Sneakers Sale, https://www.philippemodel.us.com/ Philippe Model Sneakers, https://www.nikefreesale.com/nike-free-50-c-23/ nike free 5.0, https://www.goldengoosedeluxebrandsneakers.us/golden-goose-mid-star-c-4/ golden goose mid star, https://www.goldengoosedeluxebrandsneakers.us/ golden goose, https://www.goldengooseshoesit.com/golden-goose-hi-star-c-3/ Golden Goose Hi Star, https://www.mrgoldengoose.com/ golden goose outlet, https://www.goldengoosesneakerser.com/golden-goose-ball-star-c-1/ golden goose ball star, https://www.converse-outlet.com/ Converse Store, https://www.airmax270site.com/ air max 270 outlet, https://www.yeezybooststock.com/yeezy-boost-500-c-4/ yeezy boost 500, https://www.airvapormaxsale.com/air-vapormax-97-c-7/ Air VaporMax 97, https://www.goldengoosesneaker.us/golden-goose-starter-c-6/ golden goose starter, https://www.goldengooseshoesit.com/golden-goose-francy-c-2/ Golden Goose Francy, https://www.goldengooseshoesit.com/golden-goose-superstar-c-7/ Golden Goose Superstar, https://www.goldengoosesneakerser.com/golden-goose-v-star-c-8/ golden goose v star, https://www.mrgoldengoose.com/golden-goose-superstar-c-12/ golden goose superstar, https://www.newgoldengoosesneakers.com/golden-goose-mid-star-c-4/ Golden Goose Mid Star, https://www.yeezybooststock.com/yeezy-boost-infant-c-3/ yeezy boost infant, https://www.saleggdbshoes.com/ ggdb shoes sale, https://www.goldengoosesneakerser.com/golden-goose-francy-c-2/ golden goose francy, https://www.goldengooseshoesit.com/golden-goose-ball-star-c-1/ Golden Goose Ball Star, https://www.airvapormaxsale.com/air-vapormax-running-c-4/ Air VaporMax Running, https://www.converse-outlet.com/converse-leather-c-3/ Converse Leather Shoes, https://www.goldengoosesneakerser.com/ golden goose outlet, https://www.mrgoldengoose.com/ golden goose sneakers, https://www.mrgoldengoose.com/ golden goose sale, https://www.mrgoldengoose.com/golden-goose-ball-star-c-9/ golden goose ball star, https://www.yeezybooststock.com/yeezy-boost-350-c-1/ yeezy boost 350, https://www.newgoldengoosesneakers.com/golden-goose-ball-star-c-1/ Golden Goose Ball Star, https://www.ggdbsneakersale.com/ggdb-starter-c-6/ GGDB Starter, https://www.goldengooser.com/hi-star-c-8/ golden goose hi star, https://www.goldengoosesneakerser.com/ golden goose sneakers, https://www.ggdbsneakersale.com/ GGDB Sneakers, https://www.airvapormaxsale.com/ Air VaporMax Sale, https://www.saleggdbshoes.com/ggdb-francy-c-2/ GGDB Francy, https://www.nikelunarsale.com/ nike lunar shoes outlet, https://www.goldengoosesneakersale.us/ golden goose sneakers, https://www.goldengoosesneakersale.us/golden-goose-sneakers-c-1/ golden goose sneakers, https://www.goldengooser.com/starter-c-7/ golden goose starter, https://www.airvapormaxsale.com/air-vapormax-95-c-6/ Air VaporMax 95, https://www.airvapormaxsale.com/air-vapormax-20-c-1/ Air VaporMax 2.0, https://www.airvapormaxsale.com/air-vapormax-kpu-tpu-c-8/ Air VaporMax KPU TPU, https://www.airvapormaxsale.com/air-vapormax-flyknit-c-2/ Air VaporMax Flyknit, https://www.goldengooser.com/ Golden Goose Outlet, https://www.airvapormaxsale.com/ Cheap Air VaporMax Shoes, https://www.goldengooseshoesit.com/ Golden Goose, https://www.philippemodeloutlet.com/ philippe model on sale, https://www.philippemodelsale.com/ Philippe Model, https://www.newgoldengoosesneakers.com/ Golden Goose Sneakers Sale, https://www.newgoldengoosesneakers.com/golden-goose-slide-c-5/ Golden Goose Slide, https://www.philippemodeloutlet.com/ philippe model sneakers outlet, https://www.goldengooser.com/slide-c-5/ golden goose slide, https://www.newgoldengoosesneakers.com/ Golden Goose Sneakers Outlet, https://www.airmax95shoes.com/ air max 95 shoes, https://www.saleggdbshoes.com/ggdb-ball-star-c-1/ GGDB Ball Star, https://www.philippemodel.us.com/ Philippe Model Sneakers Outlet, https://www.saleggdbshoes.com/ golden goose, https://www.goldengoosesneaker.us/golden-goose-slide-c-5/ golden goose slide, https://www.converse-outlet.com/converse-high-top-c-2/ Converse High Top, https://www.philippemodel.us.com/philippe-model-men-sneakers-c-1/ Philippe Model Men Sneakers, https://www.goldengooser.com/mid-star-c-6/ golden goose mid star, https://www.goldengoosesneaker.us/ golden goose sneakers, https://www.nikelunarsale.com/ nike lunar womens

Trở về
  • HTML5 Canvas
  • HTML5 Canvas

     The <canvas> element is used to draw graphics, on the fly, on a web page. Draw a red rectangle, a gradient rectangle,...