一、簡介: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()