Python uses memcached

Source: Internet
Author: User

Reprinted from Http://oursimplehouse.blog.sohu.com/63588732.html

Install memcached:
1. Download memcached-1.2.2.tar.gz
2. Tar xvzf memcached-1.2.2.tar.gz
3./configure;make;make Install

To install the Python API components:
1. Download python-memcached-1.39.tar.gz
2. Tar xvzf python-memcached-1.39.tar.gz
3. Python setup.py Install

Start memcached
Memcached-d-M 64-l 10.1.41.113-p 11211
This memcached is started as a daemon mode (-D), and then the cache space is 64M (-m), listening (-L) Server 10.1.41.113 port 11,212th (-P)
Add-u to specify the user parameter under root
Memcached-u bj1822-d-M 64-l 10.1.41.113-p 11211

Memcached-h
Memcached 1.2.2
-P <num> TCP port number to listen on (default:11211)
-U <num> UDP port number to listen on (default:0, off)
-S <file> UNIX socket path to listen on (disables network support)
-L <ip_addr> interface to listen in, default is Indrr_any
-D Run as a daemon
-R Maximize Core file limit
-U <username> assume identity of <username> (only if run as root)
-M <num> max memory to use for items in megabytes, default is ~ MB
-M return error on memory exhausted (rather than removing items)
-C <num> Max simultaneous connections, default is 1024
-K Lock down all paged memory
-V Verbose (print errors/warnings while in event loop)
-VV very verbose (also print client commands/reponses)
-H Print this help and exit
-I print memcached and libevent license
-B Run a managed instanced (mnemonic:buckets)
-P <file> Save PID in <file>, only used with-d option
-F <factor> chunk size growth factor, default 1.25
-N <bytes> minimum space allocated for key+value+flags, default 48

To write a Python program:
Import Memcache, Time
MC = Memcache. Client ([' 10.1.41.113:11211 '], debug=0)
Connect to the 10.1.41.113 11211 port, which is the MEMCACHD-initiated port.

Mc.set ("Some_key", "some value")
Set key and value, the third parameter defaults to 0, which means the data never times out.
If this is set:
Mc.set ("Some_key", "some value", 1)
Indicates timeout after one second
Two seconds to print value.
Time.sleep (2)
Value = Mc.get ("Some_key")
Print value
The result is none.

Delete
Mc.set ("Another_key", 3)                                                                               mc.delete ("Another_key")                                                                                                                 
Self-increment and self-reduction
Mc.set ("Key", "1")                                                                                     mc.incr ("key")                                                                                           MC.DECR ("key")           

About LRU
LRU is a buffer over the storage limit when the deletion of the trailing tail is the longest time no one to access the element, the parameter is-M. However, setting the-m and expiration time will present the risk of deleting the elements that are not invalidated. So someone on the internet changed the code to increase the judgment on Expiration Time:

Returns the code for the time-out:
if (Exptime > Realtime_maxdelta)
Return (rel_time_t) (exptime-stats.started);
else {
Return (rel_time_t) (Exptime + current_time);
}
There are two memcached of the Expiration Time format, which is the number of seconds to expire from January 1, 1970 when the number of seconds is greater than 60*60*24*30, which is 30 days, otherwise it is a valid number of seconds.

Delete the cache's code:
for (search = Tails[id]; tries>0 && search; tries--, Search=search->prev) {
if (search->refcount==0) {
Item_unlink (search);
Break
}
}
Additional conditions:
Search->exptime && search->exptime <= current_time
This will ensure that the deleted elements are all out of date.

Python uses memcached

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.