PHP, memcached nginx memcache . - URL-
URL:
/widgets.json?a=1&b=2&c=3
PHP:
<?
$widgets_cache_key = $_SERVER['REQUEST_URI'];
$m = new Memcache;
$m->connect('127.0.0.1', 11211);
$data = $m->get($widgets_cache_key);
if(empty($data)){
$r = mysql_query("SELECT * FROM widgets WHERE ...;");
while($row = mysql_fetch_assoc($r)){
$data[] = $row;
}
$m->set($widgets_cache_key, $data);
}
var_dump(json_encode($data));
?>
. nginx front-end Apache ( Apache 8080 nginx 80), nginx:
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
keepalive_timeout 5;
tcp_nodelay on;
gzip on;
upstream apache {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name _;
location / {
if ($request_method = POST) {
proxy_pass http://apache;
break;
}
set $memcached_key $uri;
memcached_pass 127.0.0.1:11211;
default_type text/html;
proxy_intercept_errors on;
error_page 404 502 = /fallback;
}
location /fallback {
internal;
proxy_pass http://apache;
break;
}
}
}
set $memcached_key $uri;. memcached, REQUEST_URI , PHP . , nginx , , PHP Apache. .
memcache Apache. , nginx, .