Pereiti prie pagrindinio turinio

Kaip įdiegti Memcached

Memcached – tai bendros paskirties paskirstyta kešavimo sistema, skirta duomenims laikinai saugoti RAM atmintyje. Dažniausiai ji naudojama dinamiškoms, nuo duomenų bazių priklausomoms svetainėms paspartinti, nes leidžia gerokai sumažinti užklausų į išorinius šaltinius – tarkime, duomenų bazę ar API – skaičių.

Papildinį ar modulį memcached palaikymui turi beveik kiekviena populiaresnė CMS, o nemažai programavimo kalbų – PHP, Perl, Ruby, Python ir kitos – siūlo savas memcached bibliotekas.

Kadangi viskas laikoma operatyviojoje atmintyje, o ne diske, sistema veikia itin greitai – nuolat rašyti duomenis į diską tiesiog nereikia.

Diegimas

Memcached galite įdiegti šia komanda:

apt-get install memcached -y

Konfigūravimas

Norėdami redaguoti numatytąjį /etc/memcached.conf konfigūracijos failą, naudokite šią komandą:

nano /etc/memcached.conf

Faile matysite numatytuosius memcached nustatymus, pavyzdžiui, numatytąjį prievadą 11211, atminties limitą 64 MB ir kitus parametrus.

Dažniausiai keičiami parametrai:

p – prievadas, kuriame veikia memcached;
mmemcached skiriamos atminties limitas;
c – didžiausias leidžiamų vienalaikių prisijungimų skaičius/

Pavyzdžiui, jei norime, kad memcached naudotų 2 GB RAM ir leistų iki 1024 vienalaikių prisijungimų, konfigūracijos failas gali atrodyti taip:

# memcached default config file
# 2003 - Jay Bonci
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 2048

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1

# Limit the number of simultaneous incoming connections. The daemon default is 1024
-c 1024

# Lock down all paged memory. Consult with the README and homepage before you do this
# -k

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r

Užbaigimas

Sukonfigūravę memcached pagal savo poreikius, galite paleisti paslaugą:

service memcached start

Norėdami patikrinti, ar memcached veikia, naudokite šią komandą:

service memcached status

Arba galite patikrinti procesą ir naudojamą prievadą šiomis komandomis:

pgrep memcached netstat -tulpn | grep :11211
Ar gavote atsakymą į savo klausimą?