192.168.62.124python DevOps (11)----python operation Cache memcache, Redis

Source: Internet
Author: User

Content directory:

Cache

    • Memcache
    • Redis
Memcache

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

memcached installation Configuration

#Install dependent packagesYum Install libevent-devel#Installing the SoftwareYum-y install memcached#Start the service/usr/bin/memcached-d-u root-l 192.168.62.124-m 1024-p 11211#Command Explanation" "start memcache Common parameters-p <num> Set TCP port number (default is not set to: 11211)-u <num> UDP Listening port (default: 11211, 0 o'clock off)-L <IP_ADDR > Binding address (Default: all allow, regardless of the internal or external network or local replacement IP, there is a security risk, if set to 127.0.0.1 can only be native access)-D run Daemon mode-u <username> bind using the specified for  Run process <username>-m <num> allow maximum memory usage, Unit m (default: + MB)-P <file> write PID to File <file>, which allows fast process termination behind Need to be used with-D" "

Memcache command

memcached Command Store command: Set/add/replace/append/prepend/cas get command: get/gets other commands: Delete/stats.

Python Operation Memcache

Python operation memcached Download the installation using the Python-memcached module: https://pypi.python.org/pypi/python-memcached

Operation

Import Memcache mc = Memcache. Client ([' 192.168.62.124:11211 '], debug=true) #链接memcache mc.set ("foo", "bar") #插入数据ret = Mc.get (' foo ') #获取数据 print (ret) #debug = True indicates a real-world error message when a run error occurs, and the parameter is removed when it is online.

Memcache Natural Support Cluster

The python-memcached module natively supports cluster operations by maintaining a list of hosts in memory, and the weight values of the hosts in the cluster are proportional to the number of occurrences of the host in the list.

     Host    weight    1.1.1.1   1    1.1.1.2   2    1.1.1.3   1 then in-memory host list is:    host_list = ["1.1.1.1", "1.1.1.2" , "1.1.1.2", "1.1.1.3",]

If the user is to create a key-value pair in memory (for example: K1 = "V1"), then perform the steps:

    • Convert K1 into a number based on the algorithm
    • Calculate number and host list length to remainder, get a value n (0 <= N < list length)
    • Gets the host in the host list according to the value obtained in 2nd step, for example: Host_list[n]
    • Connect the host acquired in step 3rd, place k1 = "V1" In the server's memory

The code is implemented as follows:

MC = Memcache. Client ([' 1.1.1.1:12000 ', 1), (' 1.1.1.2:12000 ', 2), (' 1.1.1.3:12000 ', 1)], debug=true) mc.set (' K1 ', ' v1 ')

Redis

192.168.62.124python DevOps (11)----python operation Cache memcache, Redis

Related Article

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.