This is a discussion on Why we need Memcache? within the PHP Programming forums, part of the Web Development category; All other commands don't involve unstructured data. In all of them, the client sends one command line, and expects (...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| All other commands don't involve unstructured data. In all of them, the client sends one command line, and expects (depending on the command) either one line of response, or several lines of response ending with "END" on the last line. A command line always starts with the name of the command, followed by parameters (if any) delimited by whitespace. Command names are lower-case and are case-sensitive.
__________________ With, J. Jeyaseelan Everything Possible |
| Sponsored Links |
| |||
| Expiration times Some commands involve a client sending some kind of expiration time (relative to an item or to an operation requested by the client) to the server. In all such cases, the actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Each command sent by a client may be answered with an error string from the server. These error strings come in three types:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| "CLIENT_ERROR <error>\r\n" means some sort of client error in the input line, i.e. the input doesn't conform to the protocol in some way. <error> is a human-readable error string.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| "SERVER_ERROR <error>\r\n" means some sort of server error prevents the server from carrying out the command. <error> is a human-readable error string. In cases of severe server errors, which make it impossible to continue serving the client (this shouldn't normally happen), the server will close the connection after sending the error line. This is the only case in which the server closes a connection to a client.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi, here below the miracle usage of memcache. creating large, interactive, database-backed Web sites spanning multiple servers. Approximately 70 machines currently run LiveJournal.com, a blogging and social networking system with 2.5 million accounts. In addition to the typical blogging and friend/interest/profile declaration features, LiveJournal also sports forums, polls, a per-user news aggregator, audio posts by phone and other features useful for bringing people together.
__________________ Regards, Senraj.A |
| |||
| Hi, "Caching" is a term you've probably heard mentioned before in various places . The idea behind caching is to store a copy of some piece of data so you can re-use it again later without jumping through whatever hoops you had to go through the first time to get it. There are different ways you can cache data (queries, objects, etc) and different medium in which you can store the cache (files, database, memory). Any way you do it, the main goal of caching is to increase the performance of your site or application. In many cases caching is used to lessen the amount of interaction with the database, which increases performance and decreases the load on your server.
__________________ Regards, Senraj.A |
| |||
| Hi, I would like to talk about my personal favorite method of caching: memcached. I'll show you how memcached works, and how to use it to help your site/application run faster and scale better. Already you know "memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
__________________ Regards, Senraj.A |
| |||
| Hi, memcached is an application that you can use to take advantage of spare free memory on any number of machines to cache pretty much anything you want (with a few exceptions) and retrieve it very quickly. Some of memcached's great features are that in runs on a number of platforms (Linux, BSD, Windows), is VERY fast, and has a number of client APIs already written so you'll more than likely find libraries for any type of project you're working on. We'll focus on the PHP API in this article. Before I get too far, I want to mention a couple of alternatives that may fit your particular situation.
__________________ Regards, Senraj.A |
| |||
| Hi, Local Database Query Cache: Your database may have it's own native query caching, which you don't have to do much to use. The only drawback is that if a table is updated, its entire cache is thrown out.
__________________ Regards, Senraj.A |
| |||
| Hi, The PHP APC extension: The APC extension is an opcode cache for your PHP scripts, but also provides a similar function to that of memcache. The biggest problem with APC is that you can only access the local APC cache. There are other distributed caching systems, such a MCache, but I have no personal experience with any of these, so I cannot opine on any advantages or disadvantages of using another tool.
__________________ Regards, Senraj.A |
| |||
| Hi, After install memcache, sample implementation. Now that all the pieces are in place, let's integrate memcached into our application. First thing we need to do is to connect to our memcached server: PLAIN TEXT PHP: PHP Code:
__________________ Regards, Senraj.A |
| |||
| Hi, This is assuming that memcached is running on the local machine and it's using the default settings. You would usually do this connection when you open a database connection at the beginning of your application. If you want to connect to more than one memcached server, simply call $memcache->connect() again and pass in the name and port number of the additional server(s). Now that we've got a connection, let's look at this section of code:
__________________ Regards, Senraj.A |
| |||
| the client sends a command line which looks like this: <command name> <key> <flags> <exptime> <bytes> [noreply]\r\n cas <key> <flags> <exptime> <bytes> <cas unqiue> [noreply]\r\n
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| <command name> is "set", "add", "replace", "append" or "prepend" "set" means "store this data". "add" means "store this data, but only if the server *doesn't* already hold data for this key". "replace" means "store this data, but only if the server *does* already hold data for this key". "append" means "add this data to an existing key after existing data". "prepend" means "add this data to an existing key before existing data". The append and prepend commands do not accept flags or exptime. They update existing data portions, and ignore new flag and exptime settings. "cas" is a check and set operation which means "store this data but only if no one else has updated since I last fetched it."
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| <flags> is an arbitrary 16-bit unsigned integer (written out in decimal) that the server stores along with the data and sends back when the item is retrieved. Clients may use this as a bit field to store data-specific information; this field is opaque to the server. Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead of 16, but you might want to restrict yourself to 16 bits for compatibility with older versions.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| <exptime> is expiration time. If it's 0, the item never expires (although it may be deleted from the cache to make place for other items). If it's non-zero (either Unix time or offset in seconds from current time), it is guaranteed that clients will not be able to retrieve this item after the expiration time arrives (measured by server time).
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| <bytes> is the number of bytes in the data block to follow, *not* including the delimiting \r\n. <bytes> may be zero (in which case it's followed by an empty data block).
__________________ With, J. Jeyaseelan Everything Possible |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to delete memcache? | Kamalakannan | PHP Programming | 11 | 10-25-2007 11:30 PM |
| memcache commands and behavior | prasath | Database Support | 4 | 09-27-2007 12:04 AM |
| Phpaccelerator & Memcache | sivaramakrishnan | PHP Programming | 0 | 08-31-2007 07:59 AM |
| Memcache with Mysql | write2ashokkumar | Database Support | 0 | 08-02-2007 02:41 AM |
| memcache in PHP | DuraiBabu | PHP Programming | 6 | 07-18-2007 12:43 AM |