Basic usage of memcache

Source: Internet
Author: User

Memcached features:
Simple Protocol
Libevent-based event processing
Built-in memory storage
Distributed memcached does not communicate with each other

1. Simple protocol:
Using simple text-based protocols, you can use Telnet to save data and obtain data on memcached.
2. Based on libevent:
Encapsulates the event processing functions of Linux and other operating systems into a unified interface, enabling high performance in Linux, BSD, and other operating systems.
3. built-in memory storage:
To improve performance, data stored in memcached is stored in the built-in storage space of memcached. If the content capacity reaches the specified value, it is based on LRU (least recently used) the algorithm automatically deletes unused caches.
4. Distributed memcached non-communication
The memcached server does not have distributed functions. Each memcached does not communicate with each other to share information. The distribution depends entirely on the implementation of the client.


Install memcached in Linux:
Sudo Yum install libevent-Deval
Wget http: // ip/XXXXX
Tar zxf memcached-version.tar.gz
CD memcached-version
./Configure
Make
Sudo make install

Start memcached:
/Usr/local/bin/memcached-P 11211-M 64 m-VV
Option description:
Option description
-P uses the TCP port. The default value is 11211.
-Maximum memory size of M. The default value is 64 m.
-VV is started in very vrebose mode. debugging information and errors are output to the console.
-D is started as a daemon in the background
-H help

<?php$memcache = new Memcache;$memcache->connect('localhost', 11211) or die ("Could not connect");$version = $memcache->getVersion();echo "Server's version: ".$version."<br/>\n";$tmp_object = new stdClass;$tmp_object->str_attr = 'test';$tmp_object->int_attr = 123;$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";$get_result = $memcache->get('key');echo "Data from the cache:<br/>\n";var_dump($get_result);?> 


Memcache can save data by using the following methods:
Add
Replace
Set
Usage:
$ Add = $ memcache-> Add ('key', 'value', 'duration ');
$ Replace = $ memcache-> Replace ('key', 'value', 'duration ');
$ Set = $ memcache-> set ('key', 'value', 'duration ');
How to get data from memcache:
Get
Usage:
$ Getresult = $ memcache-> get ('key ');
Var_dump ($ getresult );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.