Ubuntu 16.04 installing memcached

Time:2021-7-28

Install memcached server first:

1. Install the Memcache server:

sudo apt-get install memcached

2. Start the service after installation:

memcached -d -m 128 -p 11111 -u root

Startup parameters of memcached service:

-P listening port

-L the IP address of the connection. It is local by default

-D start starts the memcached service

-D restart restart memcached service

-D stop | shutdown shut down the running memcached service

-D install install memcached service

-D uninstall uninstall memcached service

-U runs as (valid only when running as root)

-M maximum memory usage in MB. Default 64MB

-M an error is returned when memory is exhausted, rather than deleting an item

-C the maximum number of simultaneous connections. The default is 1024

-F block size growth factor, the default is 1.25-n minimum allocated space, and key + value + flags is 48 by default

-H display help

3. You can check whether the service is started:

ps -ef | grep memcache

4. Install the memcached client:

sudo apt-get install php-memcached

5. Restart Apache:

service apache2 restart
Common operations:

//Instantiate memcached
$m = new Memcached();

//Add server
$m->addServer('127.0.0.1', 11211);

//Add multiple servers
$servers = array(
    array('127.0.0.1', 11211),
    array('127.0.0.2', 11211)
);
$m->addServers($servers);

//View server status
$m->getStats();

//View server version number
$m->getVersion();

//Add data (multiple add operations on the same key will not replace the previous value value)
$m->add('key1', 'value1', 1200);

//Replace key value
$m->replace(key1, 'value2', 1200);

//Set(), create a new value if the data does not exist, and replace the previous value if the data exists
$m->set(('key1', 'value3', 1200);

//Add multiple pieces of data
$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);
$m->setMulti($data, 1200);

//Get data
$m->get('mkey');

//Get multiple pieces of data
$m->getMulti(array('key1', 'key2'));

//Delete data
$m->delete('key1');

//Delete multiple pieces of data
$m->deleteMulti(array('key1', 'key2'));

//Delete all caches
$m->flush();

//Increment int data (the second parameter is an incremented value)
$m->increment('num', 1);

//Increment int data (the second parameter is a decreasing value)
$m->decrement('num', 1);

//Code returned from last operation
$m->getResultCode();

//Status returned from last operation
$m->getResultMessage();