Memcached Simple introduction and use in Django

Source: Internet
Author: User

What is memcached?
Memcached is a high-performance distributed memory object caching system, and many companies around the world use this caching project to build heavy-duty Web sites to share the stress of the database. Memcached is to maintain a unified huge hash table in memory, memcached can store a variety of data, including images, video, files, and database retrieval results, of course, because the data in memcached is stored in memory, should no one will be the video , pictures and other data are saved in memcached. In fact, the principle of memcached is simply to call the data into memory and then read from memory, thus greatly improving the reading speed.

What scenarios are memcached suitable for?
Storage Verification Code (graphics Verification Code, SMS Verification code), login session and so on all is not vital data.

Installation and startup options for the memcached service:
Installation: sudo apt install memcached (Linux)

Start: Service memcached start or/usr/bin/memcached start

Startup options:

-u: Specifies the user who started the memcached.
-D: This parameter is for memcached to run in the background.
-M: Specifies how much memory to run memcached, in M, which defaults to 64M.
-P: Specifies the port occupied, the default port is 11211.
-L: Default is 127.0.0.1, specify which IP address the other machine can connect to my memcached server. If you are using the service memcached start, you can only connect through the local computer. If you want to connect other machines, you must set the-l 0.0.0.0.
If you want to use the above parameters to specify some configuration information, you cannot use service memcached start, and you should run it in a/usr/bin/memcached way. such as/usr/bin/memcached-u memcache-m 1024-p 11222 start.

Basic use of memcached:
Connection:
We generally use Telnet to connect to the Memcached service, and then operate the Telnet connection memcached:
    Telnet IP Address port port number

Example: Telnet 192.168.42.133 11211

Basic operation:
To add or change data:
Command key flag (whether compressed) expire length
Command type and how to use it:
Add: Adding data, if the key already exists in the memcached, it will not be stored in, not_stored
Replace: Only key already exists in memcached to use
Set: Sets the data, if the key exists, the function is replace, if the key does not exist, then the function is add

 Query data:
Get key

Delete data:
Delete key: Deletes a specific data
Flush_all: Delete all key-value pairs in memcached

  Operations for numeric type data:

   INCR key Increase_num: Adds increase_num to the specified key

DECR key decrease_num: Reduces Decrease_num to the specified key

To view the status of Memcached:
Stats: Will list some of the memcached's most common data

To view all the keys in the memcached:
Stats items: View the current item_id
Stats cachedump item_id num: View the names of Num keys, and if NUM is 0, query all

Using Memcached in Django:
First, you need to configure the cache in settings.py:

CACHES = {
' default ': {
' backend ': ' Django.core.cache.backends.memcached.MemcachedCache ',
location ': ' 127.0.0.1:11211 ',
}

If you want to use the memcached service provided by multiple machines, you can specify multiple connections at the location, data will not be shared across multiple machines, or it will be stored separately in one of the machines according to a certain algorithm . The sample code is as follows:

CACHES = {
' Default ': {
' Backend ': ' Django.core.cache.backends.memcached.MemcachedCache ',
' Location ': [
' 172.19.26.240:11211 ',
' 172.19.26.242:11211 ',
]
}
}


Once you have configured the memcached cache, you can later use the following code to manipulate memcached in your code:

From Django.core.cache Import Cache

def index (Request):
Cache.set (' abc ', ' Zhiliao ', 60)
Print (Cache.get (' abc '))
Response = HttpResponse (' index ')
return response

It is important to note that Django does not store the data in the memcached when it is stored in the key, but does some processing of the key. For example, a prefix will be added, and a version number will be added, so if we want to manually manipulate the value that we put into our Django operation, we must know the actual key that was deposited. If we want to customize the processing of key, then we can do it in settings. Add the Key_function parameter to the caches :

CACHES = {
' Default ': {
' Backend ': ' Django.core.cache.backends.memcached.MemcachedCache ',
' Location ': ' 127.0.0.1:11211 ',
' Key_function ': Lambda key,prefix_key,version: "django:%s"%key
}
}

Memcached Simple introduction and use in Django

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.