In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Memcache Module of Python Standard Library Series
This module is an API interface for Python to operate memcached.
Memcached official website: http://memcached.org/
Module official website: https://pypi.python.org/pypi/python-memcached/
What is Memcached?
Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.
The above content is extracted from the introduction of the official website. Visit the official website for specific information.
Install Memcached
Package installation
Ubuntu
Apt-get install memcached
CentOS
Yum install memcached
Source code installation
Memcached source package installation depends on libevent
# Ubuntuapt-get install libevent-dev# CentOSyum install libevent-devel
Compile and install memcached
Wget https://memcached.org/latesttar-zxf memcached-1.x.x.tar.gzcd memcached-1.x.x./configure-- prefix=/usr/local/memcachedmake & & make test & & sudo make install
For more information, please see. / configure-- full help option. Some optional additional libraries are required for SASL support.
Start
When I install it here, I install it as a package.
[root@anshengme] # memcached-d-m 10-u root-l 0.0.0.0-p 11211-c 256-P / tmp/memcached.pid
Parameter description
Parameter description-d starts a daemon process-m is the amount of memory allocated to Memcache, in units MB-u is the user running Memcache-l is the IP address of the server listening-p is the port on which Memcache listens, preferably above 1024-c option is the maximum number of concurrent connections running, default is 1024, set according to the load of your server-P is set to save the pid file of Memcache
Set Boot self-boot
[root@anshengme ~] # chmod + x / etc/rc.d/rc.local [root@anshengme ~] # echo 'memcached-d-m 10-u root-l 0.0.0.0-p 11211-c 256-P / tmp/memcached.pid' > > / etc/rc.local
Close memcached
[root@anshengme ~] # pkill `cat / tmp/ memcached.pid`
test
First check whether port 11211 is started.
[root@anshengme ~] # netstat-tlnp | grep "11211" tcp 0 0 0.0.0.0 tlnp 11211 0.0.0.0 tcp * LISTEN 4245/memcached
Use telnet to see if port 11211 can be started, and test OK if possible, otherwise you will need to shoot the wrong picture. I hope there is no problem.
[root@anshengme ~] # telnet 127.0.0.1 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is'^'.
If the following appears, it means that the startup is successful!
Memcache usage
Install Memcache
Download the module package, the latest version
[root@anshengme ~] # wget https://pypi.python.org/packages/f7/62/14b2448cfb04427366f24104c9da97cf8ea380d7258a3233f066a951a8d8/python-memcached-1.58.tar.gz#md5=23b258105013d14d899828d334e6b044
Extract and install
[root@anshengme ~] # tar xf python-memcached-1.58.tar.gz [root@anshengme ~] # cd python-memcached-1.58 [root@anshengme python-memcached-1.58] # python setup.py install
Enter the Python interpreter to import the module. If the import is successful, the module is installed successfully.
[root@anshengme python-memcached-1.58] # python > > import memcache > >
First experience
# Import memcache module > import memcache# connects to a Memcached server > conn = memcache.Client (['192.168.56.100 Memcached 11211']) # set a value and override it if it exists > conn.set (' K1', 'v1') True# get the value > conn.get (' K1') 'v1'
More ways to use
Set the timeout
> conn.set ('knot,' vicious, 1) True > conn.get ('k')
Set the value and report an error if it exists
> conn.add ('Kwon Magazine hello') # False setting failed False > conn.get (' k') # the original value has not changed'v'
Modify the value. Return False if it does not exist.
> conn.replace ('Knowles journal helloworld') # successfully set True > conn.get ('k') # returns the modified value 'helloworld' > conn.replace (' kkkk','hello') # modify a value False that does not exist
Set multiple values, create if the value does not exist, modify if it exists
> conn.get ('key1') > conn.set_multi ({' key1':'value1','key2':'value2'}) [] > conn.get ('key1')' value1'
Delete a value
> conn.get ('key1')' value1' > conn.delete ('key1') 1 > conn.get (' key1')
Delete multiple values
> conn.set_multi ({'key3':'value3','key4':'value4'}) [] > conn.delete_multi ([' key3', 'key4']) 1
Get one value and multiple values
> conn.set_multi ({'key5':'value5','key6':'value6'}) [] > conn.get (' key5') 'value5' > conn.get_multi ([' key5','key6']) {'key5':'value5','key6':'value6'}
Modifies the value of the specified key, appending content to that value
> conn.append ('key5','after') True > conn.get (' key5') 'value5after'
Modifies the value of the specified key, inserting content before the value
> conn.prepend ('key5','before') True > conn.get (' key5') 'beforevalue5after'
Add or decrease a value in Memcached by adding or decreasing N (N defaults to 1)
> conn.set ('number','9') True# increases > conn.incr (' number') 1 increases > conn.incr ('number', 10) 2 decreases > conn.decr (' number') decreases > conn.decr ('number', 10) 9
For example, set a value like this:
Conn.set ('nasty Personality 10)
Now that user An and user B get both values, if either user modifies the value, the other user will report an error when operating on the value.
If you want to solve the above problems, you can use gets and cas. The test code is as follows:
#-- s1.py# _ * _ coding:utf-8 _ * _ import memcacheconn1 = memcache.Client (['192.168.56.100 memcache.Client 11211], debug=True, cache_cas=True) conn1.set (' n', 9) # gets gets the value and gets the counter result = conn1.gets ('n') print (result) # blocking input ('> >') # set value conn1.cas ('n' 99) #-- memcache.Client _ * _ coding:utf-8 _ * _ import memcacheconn2 = memcache.Client (['192.168.56.100 memcache.Client 11211'], debug=True, cache_cas=True) # gets gets the value and gets the counter result = conn2.gets (' n') print (result) # blocking input ('> >') # setting value conn2.cas
The execution effect is as follows:
Multi-node operation
First, there are four memcached instances on the server, each of which is a separate memcached service.
[root@anshengme ~] # netstat-tlnp | grep "memcached" tcp 0 0 0.0.0 0 tlnp 11211 0.0.0 memcached * LISTEN 1125/memcached tcp 0 0 0.0.0 0 15 * LISTEN 1330/memcached tcp 0 0 0.0.0 .0coding:utf-8 11213 0.0.0.0coding:utf-8 * LISTEN 1337/memcached tcp 00 0.0.0.0coding:utf-8 11214 0.0.0.0 import memcache# connects to multiple memcached servers conn = memcache.Client (# IP: Port) Weight [('192.168.56.100), (' 192.168.56.100) (11212), ('192.168.56.100) (11213), (' 192.168.56.10011214), ('192.168.56.10011214)]) conn.set (' kicking,'v')
Multi-node data storage process
Convert a string to a number first
The result and the number of nodes are divided.
The result must be between the number of nodes, and the remainder is a few, and the data is stored on that node.
As shown in the figure
# convert a string to a numeric module import binascii# is extracted from part of the memcache source code def cmemcache_hash (key): return (binascii.crc32 (key) & 0xffffffff) > 16) & 0x7fff) or 1) # result is the returned number result = cmemcache_hash (bytes ('kink, encoding='utf-8')) print (result) Memcached-based Session instance
Main program script
# _ * _ coding:utf-8 _ * _ import tornado.ioloopimport tornado.webimport MemcacheToSessionclass BaseHandler (tornado.web.RequestHandler): def initialize (self): self.session = MemcacheToSession.Session (self) # pass class MainHandler (BaseHandler): def get (self): Info = self.session.GetAll () self.render ("template/index.html", Data=Info) def post (self, * args * * kwargs): # get the passed value Key = self.get_argument ('key') Val = self.get_argument (' val') action = self.get_argument ('action') if action =' set': # setting value self.session [Key] = Val elif action = = 'del': del Self.session [Key] # get all the information Info = self.session.GetAll () # return to the front-end rendering self.render ("template/index.html" Data=Info) settings = {"tempalte_path": "template", "cookie_secret": "508CE6152CB93994628D3E99934B83CC",} application = tornado.web.Application ([(rushing, MainHandler),], * * settings) if _ _ name__ = = "_ _ main__": application.listen (9999) tornado.ioloop.IOLoop.instance (). Start ()
Template file
Set/del: Key: Val: {{Data}}
Set up the mini module of Session
# _ * _ coding: utf-8 _ * _ import memcacheimport hashlibimport uuidimport json# connection memcachedconn = memcache.Client (['192.168.56.100 import memcacheimport hashlibimport uuidimport json# 11211']) class Session: CookieID =' uc' ExpiresTime = 60 * 20 def _ _ init__ (self Handler): "" is used to create the dictionary of user session in memcached: param handler: request header "" self.handler = handler # get the random string SessionID = self.handler.get_secure_cookie (Session.CookieID) from the client None) # client exists and if SessionID and conn.get (SessionID) also exists on the server: self.SessionID = SessionID else: # get the random string self.SessionID = self.SessionKey () # write the random string to memcached for 20 minutes conn.set (self.SessionID, json.dumps ({})) Session.ExpiresTime) # add 20 minutes for each access timeout conn.set (self.SessionID, conn.get (self.SessionID), Session.ExpiresTime) # set Cookie self.handler.set_secure_cookie ('uc') Self.SessionID) def SessionKey (self): ": return: generate random string" UUID = str (uuid.uuid1 ()) .replace ('-','') MD5 = hashlib.md5 () MD5.update (bytes (UUID, encoding='utf-8')) SessionKey = MD5.hexdigest () return SessionKey def _ setitem__ (self) Key, value): "" set key: param value in session: param key: session information: corresponding Value "# get session_id SessionDict = json.loads (conn.get (self.SessionID)) # set dictionary key SessionDict [key] = value # reassign conn.set (self.SessionID, json.dumps (SessionDict)) Session.ExpiresTime) def _ getitem__ (self, item): ": param item: Session corresponding Key: return: acquired Session information"# get SessionID and convert it to dictionary SessionDict = json.loads (conn.get (self.SessionID)) # get the corresponding data ResultData = SessionDict.get (item) None) return ResultData def _ delitem__ (self, key): ": param key: the Key to be deleted" # get the SessionID and convert it to the dictionary SessionDict = json.loads (conn.get (self.SessionID)) # delete the dictionary key del SessionDict [key] # reassign conn.set (self.SessionID Json.dumps (SessionDict), Session.ExpiresTime) def GetAll (self): # get all the information in Session Used only to test SessionData = conn.get (self.SessionID) return SessionData
The demonstration is as follows:
# Python Standard Library # Memcache
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.