Improves the performance of Web applications and jumps from memcached
When we think of storing data in Web applications, we usually think of a traditional database MySQL. This is a great long-term storage and data analysis, but there are many short-term needs for a better choice: memcached. This is a good option for storing information between page requests and performance improvement. This article describes how to start using memcached PHP. Introduction to memcached is a simple interface that allows you to store things on the storage server. This can run on the same machine as the Web server's scalability, but comes from distributed across multiple server instances. All you need is Memcached running in the background. PHP provides a simple interface and a PECL Library. Debian-based Linux system, which is easy: $ sudo apt-get install memcached $ sudo service memcached start $ sudo pecl install memcached $ sudo service httpd restart now we need to mention that there are two PHP libraries using memcached technically. The old library is called "set" and lacks some functions. The new "cache" library uses libmemcached and is generally preferred. The first step in PHP is to connect to the server. Connection can be sustained across the requirements, which is a good performance. And then add it to the server list. In this case, we will use the default port of a local running instance: function get_memcached () {// Set a persistent connection ID $ mc = new Memcached ('webapp '); // Set a short timeout (in milliseconds) so if the server goes down // it doesn't take down our site with it $ mc-> setOption (Memcached: OPT_CONNECT_TIMEOUT, 1000); if (! $ Mc-> getServerList () {if (! $ Mc-> addServer ('localhost', 11211) {error_log ('could not add memcached server. '); return false ;}} return $ mc;} now you can read and write PHP variables and key-based memcached. you can define simple functions. They will serialize and deserialize automatically. You may not like writing database connections or result set resources, but you can array and store these result sets. Data storage let us say that we want to store the list of recent accessed urls for each login user. We can use sessions, but devices that do not work and it will clear sessions as soon as possible and will disappear. We can use a database, but this data is most likely that our system has no critical slowness. It is easy to use memcached: $ user_id = 123; // The current user's ID cache now lets us cache database results to truly improve your Web applications. Database query is usually the biggest bottleneck for processing on the server, so as to avoid repeated query results cached in the memory, which can provide a huge performance gain. The simplest way is to query stores with only primary keys. It is usually better to delete the cached value. when the database record is updated, the user will not see any outdated value. Function store_product ($ id, $ name, $ price) {// Create or update the product in the database $ db = get_db (); $ qry = $ db-> prepare ('replace INTO product (id, name, price) VALUES (: id,: name,: price )'); $ qry-> bindParam (': ID', $ id); $ qry-> bindParam (': name', $ name); $ qry-> bindParam (': pric', $ price); $ qry-> execute (); restrictions and precautions for any technical choice: a critical maximum length is 250 bytes. Brief your key. The default maximum value is 1 MB. This is not the correct place to store large values. Warehousing is not a writing method that locks reading or logging in the database. You must be aware that any Web request can update any value at any time. Make sure that you have enough RAM memcached servers to merge. Next, you can perform memcached: the cached value can time out. When this is useful data, it should be cached for a period of time rather than manually deleted. A simple method of increasing and decreasing is helpful for keeping requests between counters quickly. Memcached is correctly configured. you can share data between applications written in many programming languages. Give memcached a try. This is a simple and effective solution to maximize the performance of your Web applications, as appropriate.