1.MEMCACHED:KV structure, stored in memory, can reduce the database access pressure, can also be used as a session server
(1) Common commands
-u Specify user-m specify memory size
-D start|restart|stop-p Specify port, default 11211
-N Minimum allocated space-f growth factor, calculated as the minimum allocation space, default is 1.25 times times
-VV viewing the specific startup process
(2) Connecting memcached
Telnet x.x.x.x 11211
Quit quitting
View Stats Items
Save Set key flags timeout size
Extract Get Key
Append append key Flags timeout content_size content, for example:
Append magedu 0 4. com
Add and subtract INCR/DECR key num
Empty Flush_all
(3) PHP connection
Centos7 php-pecl-memcached
(4) PHP session server
php.ini file
Session.save_handler=memcache
Session.save_path= "Tcp://127.0.0.1:11211?persistent=1 & weight=1 & timeout=1 & Retry_interval=15"
2.varnish: can use memory or hard disk cache, can be used for front-end static page picture JS cache server
(1) Connecting varnish
Varnishadm-s/etc/varnish/secret-t 127.0.0.1:6082
Vcl.list list configuration files, avrilable available, active used
Vcl.show confname View specific configuration files
Vcl.use confname using configuration files
Vcl.load confname filename Loads a file into a configuration file
Backend.list
storage.list Cache Type
(2) Basic configuration
/etc/varnish/default.vcl
Backend Default {
. host= "x.x.x.x" is the IP of the listening host
. port= "80"
}
/etc/varnish/varnish.params
Varnish_listen_port=80
Varnish_storage= "File,/var/lib/varnish/varnis_storage.bin"
HDD storage, binary files, do not go to cat, files may be corrupted
Varnish_storage= "mallc,2048" Memory storage
(3) Common variables
Bereq: Varnish sending HTTP request to backend host
Bereq.http.HEADERS
Bereq.url
Bereq.proto Protocol version
Bereq.backend
BERESP:
Beresp.status
Beresp.backend.name
Beresp.http.HEADERS
Beresp.ttl Remove response time, remaining TTL lifetime
Req: HTTP request sent by backend to varnish host
Req.method 4.0 Request method
Req.request 3.0 Request Method
Obj:
Obj.hits Hit Count
Obj.ttl object ttl
Server
Server.ip
Server.hostname
(4) Example
Do not let the specified page check the cache
Sub Vcl_recv {
if (req.url ~ "^/test.html$") {
Return (pass)
}
}
Add hit tip to head
Sub Vcl_deliver {
if (obj.hits>0) {
Set resp.http.cache= "hit" + "" +server.ip
}else{
Set resp.http.cache= "Miss" + "" "+server.ip
}
}
Clear Page Cache 3.0
Sub Vcl_hit {
if (req.request = = "PURGE") {
Purge
Error 404 "not in cache";
}
}
Curl-x PURGE Http://magedu/test
Clear Page Cache 4.0
Sub Vcl_recv {
if (Req.method = = "PURGE") {
return (Purage);
}
}
Sub Vcl_purge {
Return (synth, "purged"))
}
curl-x PURGE http://magedu/test
(5) Static and dynamic separation
Backend Server1 {
...
}
Backend Server2 {
...
}
Sub Vcl_recv {
if (req.url ~ "(? i) \.php$") {
Set Req.backend_hint=server1;
}eles{
Set Req.backend_hint=server2;
}
}
(6) Load Balancing
Import directors;
Sub Vcl_init {
New Bar=directors.round_robin ();
Bar.add_backend (Server1);
Bar.add_backend (Server2);
}
Sub Vcl_recv {
Set Req.backend_hint=bar.backend ();
}
Linux self-study note--memcache and varnish cache server