Memcached, memcached installation_PHP tutorial
Memcached, memcached installation
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Memcached is a commonly used caching technology that can greatly improve the performance of web applications. In PHP, the commonly used Session processing method is to store the Session file on the server's hard disk. However, this method is not optimal because the server's hard disk will become one of the performance bottlenecks. The use of Memcached caching technology can optimize Session processing in PHP and improve the performance of Web applications. Session in PHP

Caching library in PHP8.0: Memcached With the rapid development of the Internet, modern applications require efficient and reliable caching technology to improve performance and handle large amounts of data. Due to PHP's popularity and open source nature, the PHP caching library has become an essential tool in the web development community. Memcached is a widely used open source high-speed memory caching system that can handle millions of simultaneous connected cache requests and can be used in many different types of applications, such as social networks, online

With the development of the Internet, PHP applications have become more and more common in the field of Internet applications. However, high concurrent access by PHP applications can lead to high CPU usage on the server, thus affecting the performance of the application. In order to optimize the performance of PHP applications, Memcached caching technology has become a good choice. This article will introduce how to use Memcached caching technology to optimize the CPU usage of PHP applications. Introduction to Memcached caching technology Memcached is a

With the rapid development of the Internet, large-scale MySQL database backup and recovery has become one of the essential skills for major enterprises and websites. With the widespread application of Memcached, how to back up and restore Memcached has also become an important issue. As one of the main languages for web development, PHP has unique advantages and skills in handling backup and recovery of MySQL and Memcached. This article will introduce in detail the implementation method of PHP processing MySQL and Memcached backup and recovery.

With the rapid development of modern Internet applications, user experience is crucial to the success of an application. How to ensure high performance and high availability of applications has become one of the important issues that developers need to solve. As one of the widely used programming languages, PHP's performance monitoring and optimization are also very important. Memcached is a high-performance, distributed memory object caching system that can help applications improve performance and scalability. This article will introduce how to use PHP and Memcached to implement performance monitoring.

With the continuous increase of network applications and the continuous expansion of data volume, data reading and writing efficiency has become one of the important factors affecting application performance. The application of caching technology can solve this problem well. In PHP applications, Memcached is the most commonly used cache server. Memcached is a high-performance distributed memory object caching system that can store commonly used data in memory and improve the efficiency of data retrieval. This article will introduce how to use PHP and Memcached for cache management, and how to optimize

With the continuous development of Internet technology, audio and video resources have become a very important form of content on the Internet, and PHP, as one of the most widely used languages in network development, is also constantly used in the field of video and audio playback. However, with the increasing number of users of audio and video websites, many websites have discovered a problem: under high concurrency conditions, PHP's processing speed of audio and video slows down significantly, resulting in problems such as inability to play in time or stuck playback. To solve this problem, Memcached caching technology should

With the rapid growth of modern applications, caching has become a vital part of many developers. Caching can greatly improve application performance and reduce server load. In CakePHP, one way to implement caching is to use Memcached. Memcached is a memory-based distributed caching system. It stores data in memory and can read and write data quickly. In a multi-server environment, Memcached can store data in a distributed manner and share it over the network. not only can
