Python-Flask-Memcached缓存系统
一、简介:Memcached 是一个高性能的分布式,基于内存的key-value存储的对象缓存系统(并不是一个数据库),用于动态Web应用以减轻数据库负载。
二、Memcached下载和安装
1、下载和安装Memcached服务端
1.1、下载地址
下载地址:http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
1.2、安装(备注:仅限windows系统)
1.2.1、解压下载的压缩包
1.2.2、命令行模式(管理员)进入到memcache目录下:
memcached.exe -d install 进行安装
memcached.exe -d start 启动
可以设置为启动类型为自动,即开机自动启动服务端。
#安装memcached C:\>cd "Program Files" C:\Program Files>cd memcached C:\Program Files\memcached>dir 驱动器 C 中的卷是 OSDisk 卷的序列号是 44B8-B575 C:\Program Files\memcached 的目录 2020/07/06 13:54 <DIR> . 2020/07/06 13:54 <DIR> .. 2009/12/17 00:47 560,458 libgcc_s_sjlj-1.dll 2009/12/17 00:47 507,640 memcached.exe 2009/12/17 00:47 154,699 pthreadGC2.dll 3 个文件 1,222,797 字节 2 个目录 42,870,157,312 可用字节 C:\Program Files\memcached>memcached.exe -d install #启动memcached服务 C:\Program Files\memcached>memcached.exe -d start #查看帮助 C:\Program Files\memcached>memcached -h memcached 1.4.4-14-g9c660c0 -p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -s <file> UNIX socket path to listen on (disables network support) -a <mask> access mask for UNIX socket, in octal (default: 0700) -l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses) -s <file> unix socket path to listen on (disables network support) -a <mask> access mask for unix socket, in octal (default 0700) -l <ip_addr> interface to listen on, default is INADDR_ANY -d start tell memcached to start -d restart tell running memcached to do a graceful restart -d stop|shutdown tell running memcached to shutdown -d install install memcached service -d uninstall uninstall memcached service -r maximize core file limit -u <username> assume identity of <username> (only when run as root) -m <num> max memory to use for items in megabytes (default: 64 MB) -M return error on memory exhausted (rather than removing items) -c <num> max simultaneous connections (default: 1024) -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user; under sh this is done with 'ulimit -S -l NUM_KB'). -v verbose (print errors/warnings while in event loop) -vv very verbose (also print client commands/reponses) -vvv extremely verbose (also print internal state transitions) -h print this help and exit -i print memcached and libevent license -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) -L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk. -D <char> Use <char> as the delimiter between key prefixes and IDs. This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server. -t <num> number of threads to use (default: 4) -R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20) -C Disable use of CAS -b Set the backlog queue limit (default: 1024) -B Binding protocol - one of ascii, binary, or auto (default) -I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m)
memcache服务没有启动,报如下错误:
MemCached: MemCache: inet:127.0.0.1:11211: connect: [WinError 10061] 由于目标计算机积极拒绝,无法连接。. Marking dead.
解决办法:
memcached.exe -d start 启动
三、memecached案例
1、set 和set_multi命令的使用
cat app.py
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.set('name', 'nulige') #设置name的值 mc.set_multi({'key1': 'nulige', 'key2': 'jojo'}) #设置key1 and key2 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
2、get与get_multi与delete命令的使用
ag:get和delete命令
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('username', 'nulige', time=120) # 一次只能设置一个值 username = mc.get('username') # 获取数据 print(username) mc.delete('username') #删除数据 @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
ag: get_multi命令
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('username', 'zhangshan', time=0) # 一次只能设置一个值 username = mc.get('username') # 获取数据 print(username) dic = {'name': 'to,', 'age': '19', 'job': 'IT'} mc.set_multi(dic) mc.replace('username', 'lisi') @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()add
3、add命令的使用
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('username', 'xiexie', time=120) # 一次只能设置一个值 username = mc.get('username') print(username) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
4、replace命令的使用
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('key', 'zhangshan', time=120) # 一次只能设置一个值 key = mc.get('key') # 获取数据 print(key) mc.replace('key', 'lisi', time=120) # 一次只能设置一个值 key1 = mc.get('key') print(key1) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
5、append 和 prepend命令使用
# append : 修改指定key的值,在该值后面追加内容。 # prepend : 修改指定key的值,在该值前面插入内容。 from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('key', 'zhangshan|', time=120) # 一次只能设置一个值 key = mc.get('key') # 获取数据 print(key) mc.append('key', 'wangxiao', time=120) # 在第一个键后追加内容 key1 = mc.get('key') print(key1) mc.prepend('key', 'wuyong|', time=120) # 在第一个键前面追加 key2 = mc.get('key') print(key2) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
6、delete和delete_multi命令的使用
# delete : 在Memcached中删除指定的一个键值对。 # delete_multi : 在Memcached中删除指定多个键值对。 from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('key', 'zhangshan|', time=120) # 一次只能设置一个值 key = mc.get('key') # 获取数据 print(key) mc.append('key', 'wangxiao', time=120) # 在第一个键后追加内容 key1 = mc.get('key') print(key1) mc.delete('key') print(key) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
7、decr和incr命令的使用
from flask import Flask import memcache app = Flask(__name__) mc = memcache.Client(['127.0.0.1:11211'], debug=True) # 能连接多个服务器 mc.add('num', 99) # 一次只能设置一个值 key = mc.get('num') # 获取数据 print(key) mc.incr('num') key1 = mc.get('num') print(key1) mc.decr('num', 10) key2 = mc.get('num') print(key2) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()