Home > Article > Backend Development > Memcached, memcached installation_PHP tutorial
Memcached is a high-performance distributed memory object caching system for dynamic web applications to reduce database load. It improves the speed of dynamic, database-driven websites by caching data and objects in memory to reduce the number of database reads.
Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can be written in any language and communicates with the daemon through the memcached protocol.
Storage method:
In order to improve performance, the data saved in memcached is stored in the built-in memory storage space of memcached. Since data only exists in memory, restarting memcached and restarting the operating system will cause all data to disappear. In addition, after the content capacity reaches the specified value, unused caches are automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, so it does not consider the persistence of data too much.
Tips:
Many languages have implemented clients to connect to memcached, mainly Perl and PHP.
1. Here we introduce the installation of windows environment.
1. Download the Windows stable version of memcache, unzip it and put it in a certain disk, such as c: memcached
2. Enter 'c:memcachedmemcached.exe -d install' under cmd to install
3. Then enter: 'c:memcachedmemcached.exe -d start' to start.
In the future, memcached will be automatically started as a service of Windows every time you boot up. The server side has now been installed.
4. Very simple. But it's not over yet, you just installed a memcached caching server, and it has not been associated with php. So it cannot be used in php programs.
Since my php version is 5.2.17, the downloaded php_memcache.dll must also be corresponding. If your php is 5.3, you can download it here
php_memcache-cvs-20090703-5.3-nts-VC6-x86.zip
The installation is exactly the same as adding an extension normally. Copy the dll file to the ext directory of your php directory, and then,
Add extension=php_memcache.dll to php.ini, restart the server, and you should be able to see the configuration information in phpinfo.
2. Installation under CentOS
Install yum -y install memcached
Set to start at boot chkconfig --level 2345 memcached on
Start and stop /etc/init.d/memcached start|stop
Additional: If the installation lacks other support, you can:
yum groupinstall "Development Tools"
Common operations
Memcache::add Add a value, if it already exists, return false
Memcache::addServer Add a server address for use
Memcache::close Close a Memcache object
Memcache::connect Create a Memcache object
Memcache::debug Control debugging functions
Memcache::decrement Performs subtraction operation on the value in a saved key
Memcache::delete Delete a key value
Memcache::flush Clear all cached data
Memcache::get Get a key value
Memcache::getExtendedStats Get the running system statistics of all processes in the process pool
Memcache::getServerStatus Gets the parameters for running the server
Memcache::getStats Returns some running statistics of the server
Memcache::getVersion Returns the version information of the running Memcache
Memcache::increment Performs addition operation on the value in a saved key
Memcache::pconnect Create a Memcache persistent connection object
Memcache::replace R overwrites an existing key
Memcache::set Add a value, if it already exists, overwrite it
Memcache::setCompressThreshold Compresses data larger than a certain size
Memcache::setServerParams Modify server parameters at runtime
<?<span>php </span><span>//</span><span>连接Memcache </span> <span>$mem</span> = <span>new</span><span> Memcache; </span><span>$mem</span>->connect("localhost", 11211<span>); </span><span>//</span><span>保存数据 </span> <span>$mem</span>->set('key1', 'This is first value', 0, 60<span>); </span><span>$val</span> = <span>$mem</span>->get('key1'<span>); </span><span>echo</span> "Get key1 value: " . <span>$val</span> ."<br>"<span>; </span><span>//</span><span>替换数据 </span> <span>$mem</span>->replace('key1', 'This is replace value', 0, 60<span>); </span><span>$val</span> = <span>$mem</span>->get('key1'<span>); </span><span>echo</span> "Get key1 value: " . <span>$val</span> . "<br>"<span>; </span><span>//</span><span>保存数组数据 </span> <span>$arr</span> = <span>array</span>('aaa', 'bbb', 'ccc', 'ddd'<span>); </span><span>$mem</span>->set('key2', <span>$arr</span>, 0, 60<span>); </span><span>$val2</span> = <span>$mem</span>->get('key2'<span>); </span><span>echo</span> "Get key2 value: "<span>; </span><span>print_r</span>(<span>$val2</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>//</span><span>删除数据 </span> <span>$mem</span>->delete('key1'<span>); </span><span>$val</span> = <span>$mem</span>->get('key1'<span>); </span><span>echo</span> "Get key1 value: " . <span>$val</span> . "<br>"<span>; </span><span>//</span><span>清除所有数据 </span> <span>$mem</span>-><span>flush</span><span>(); </span><span>$val2</span> = <span>$mem</span>->get('key2'<span>); </span><span>echo</span> "Get key2 value: "<span>; </span><span>print_r</span>(<span>$val2</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>//</span><span>关闭连接 </span> <span>$mem</span>-><span>close(); </span>?>
How memcached works:
First of all, memcached runs in one or more servers as a daemon, accepting client connection operations at any time. Clients can be written in various languages. Currently known client APIs include Perl/PHP/ Python/Ruby/Java/C#/C etc.
After PHP and other clients establish a connection with the memcached service, the next thing is to access objects. Each accessed object has a unique identifier key, and access operations are done through this key, the objects saved to memcached are actually placed in the memory, not saved in the cache file, which is why memcached can be so efficient and fast. Note that these objects are not persistent, and the data inside will be lost after the service is stopped.
memcachedb:
MemcacheDB is a distributed, key-value persistent storage system. It is not a caching component, but a reliable, fast and persistent storage engine based on object access. The protocol is consistent with memcache (incomplete), so many memcached clients can connect to it. MemcacheDB uses Berkeley DB as the persistent storage component, so it supports many Berkeley DB features.
We stand on the shoulders of giants. The front-end cache of MemcacheDB is Memcached
Front-end: memcached’s network layer
Backend: BerkeleyDB storage
What is the relationship and difference between memcached and smarty?
Memcache is a high-performance distributed memory object caching system that records the cache into memory.
For example, if you get a list display from the database, but you don’t want to read the database every time, you need to use cache, and memcache is one of them, which saves the records in memoryUse
For example, if you want to re-db to obtain data and display it, db -> memcache -> client
First determine whether memcache has data, if not, read DB, and then save the records obtained by db in memcache
The next time you need to read records, you can read them directly in memcache, which can share the burden of the database and is much faster.
Smarty is a template engine written in PHP. Its purpose is to separate PHP programmers from front-end personnel, so that programmers can change the logical content of the program without affecting the page design of the front-end personnel.
smarty runs in the view of MVC structure.
For example, if we want to display a variable in php, we need to write echo $a;
When using smarty, you need to write {$a} like this. After compilation, it will automatically display echo $a;, which is the same. The cache in smarty, after php is run , can be output to the browser. It requires calculation to run PHP to generate HTML output, and smarty will save the HTML generated by the PHP that was run before. If this PHP is called again, the previous HTML will be directly output. Caching effect.
memcache and smarty have nothing to do with each other. They have different functions and are not related.