CentOS 7
Sponsored Link

Memcached : Use it on PHP2016/07/18

 
This is the example to use Memcached on PHP.
[1] Install PHP Memcached Client Module.
# install from EPEL

[root@dlp ~]#
yum --enablerepo=epel -y install php-pecl-memcached
[2] Basic usage on PHP.
[cent@dlp ~]$
vi use_memcache.php
<?php
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$memcache->setOption(Memcached::OPT_COMPRESSION, false);

// set and get a Key
$memcache->set('key01', 'value01');
print 'key01.value : ' . $memcache->get('key01') . "\n";

// append and get a Key
$memcache->append('key01', ',value02');
print 'key01.value : ' . $memcache->get('key01') . "\n";

$memcache->set('key02', 1);
print 'key02.value : ' . $memcache->get('key02') . "\n";

// increment
$memcache->increment('key02', 100);
print 'key02.value : ' . $memcache->get('key02') . "\n";

// decrement
$memcache->decrement('key02', 51);
print 'key02.value : ' . $memcache->get('key02') . "\n";

$memcache->set('key03', 'value03');
print 'key03.value : ' . $memcache->get('key03') . "\n";

// CAS (on the example below, the Value of key03 will not update to value05)
$memcache->get('key03', null, $cas);
$memcache->replace('key03', 'value04');
if ($memcache->getResultCode() == Memcached::RES_NOTFOUND) {
    $memcache->add('key03', 'value03');
} else {
    $memcache->cas($cas, 'key03', 'value05');
}
print 'key03.value : ' . $memcache->get('key03') . "\n";
?>

# run

[cent@dlp ~]$
php use_memcache.php

key01.value : value01
key01.value : value01,value02
key02.value : 1
key02.value : 101
key02.value : 50
key03.value : value03
key03.value : value04
Matched Content