『壹』 python 中哪裡會用memcache模塊實現什麼功能
memcached很強大,它可以支持分布式的共享內存緩存,大型站點都用它。對小站點來說,有足夠內存的話,使用它也可以得到超贊的效果。
功能類似鍵值資料庫
『貳』 python-memcached是不是線程安全的
答案是肯定的,前提你在使用Python 2.4+和python-memcached 1.36+
為什麼我們需要線程安全的memcached client,因為我們的實際應用一般是多線程的模型,例如cherrypy、twisted,如果python-memcached不是線程安全的話,引起的問題不僅僅是並發修改共享變數這么簡單,是外部socket鏈接的數據流的混亂
python-memcached怎麼實現線程安全的呢?查看源代碼看到
try:
# Only exists in Python 2.4+
from threading import local
except ImportError:
# TODO: add the pure-python local implementation
class local(object):
pass
class Client(local): 很取巧的讓Client類繼承threading.local,也就是Client裡面的每一個屬性都是跟當前線程綁定的。實現雖然不太優雅,但是很實在
但是別以為這樣就可以隨便在線程裡面用python-memcached了,因為這種thread local的做法,你的應用必須要使用thread pool的模式,而不能不停創建銷毀thread,因為每一個新線程的創建,對於就會使用一個全新的Client,也就是一個全新的socket鏈接,如果不停打開創建銷毀thread的話,就會導致不停的創建銷毀socket鏈接,導致性能大量下降。幸好,無論是cherrypy還是twisted,都是使用了thread pool的模式
『叄』 你使用過 Memcache 緩存嗎如果使用過,能夠簡單的描述一下它的工作原理嗎
首先 memcached 是以守護程序方式運行於一個或多個伺服器中,隨時接受客戶端的連接操作,客戶端可以由各種語言編寫,目前已知的客戶端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。PHP 等客戶端在與 memcached 服務建立連接之後,接下來的事情就是存取對象了,每個被存取的對象都有一個唯一的標識符 key,存取操作均通過這個 key 進行,保存到 memcached 中的對象實際上是放置內存中的,並不是保存在 cache 文件中的,這也是為什麼PHP內存緩存技術memcached 能夠如此高效快速的原因。注意,這些對象並不是持久的,服務停止之後,里邊的數據就會丟失。
『肆』 怎樣離線安裝python-memcached
reviewboard作為一個開源的代碼復查工具,已經受到越來越多的開發人員喜愛。然而,也是由於其開源的特性,使得其的安裝也過於linux化。在linux下,只需要下載安裝指定的包即可,包之間的依賴會自動地進行處理。然而在windows下,就需要安裝人員...
『伍』 pylibmc如何在windows下進行安裝啊
pylibmc安裝過程就像個遞歸調用,要安裝pylibmc需要先安裝libmemcached,安裝libmemcached又需要先安裝memcached,安裝memcached又要先安裝libevent。以下是安裝過程
1。安裝libevent
./configure && make && make install
默認安裝的需要做個軟連:/usr/lib/libevent-1.3.so.1 -> /usr/local/lib/libevent-1.3.so.1。1.3是版本號
2。安裝memcached
./configure && make && make install
3。安裝libmemcached
./configure 需要指定參數,參照http://www.hdwong.com/article/libmemcached-compile-error.html
./configure --disable-64bit CFLAGS="-O3 -march=i686"
make && make install
默認安裝的需要做個軟連:ln -s /usr/local/lib/libmemcached.so.5 /usr/lib/libmemcached.so.5
4。安裝pylibmc
./configure && make && make install
『陸』 python 上怎麼安裝memcache
zabbix:/root/sbin/xx#python
Python2.7.3(default,Apr92017,14:47:16)
[GCC4.4.720120313(RedHat4.4.7-17)]onlinux2
Type"help","right","credits"or"license"formoreinformation.
>>>importmemcache
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<mole>
ImportError:Nomolenamedmemcache
>>>exit();
zabbix:/root/sbin/xx#pipinstallmemcache
Collectingmemcache
/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318:SNIMissingWarning:AnHTTPSrequesthasbeenmade,buttheSNI
(SubjectNameIndication).,whichcancausevalidation
failures..Formoreinformation,see n/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122:InsecurePlatformWarning:AtrueSSLContextobjectisnot
available.Lconnectionstofail..
Formoreinformation,see s.io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
(fromversions:)
zabbix:/root/sbin/xx#pipinstallpython-memcached
Collectingpython-memcached
/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318:SNIMissingWarning:AnHTTPSrequesthasbeenmade,buttheSNI
(SubjectNameIndication).,whichcancausevalidation
failures..Formoreinformation,see docs.io/en/latest/security.html#snimissingwarning.
SNIMissingWarning
/usr/local/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122:InsecurePlatformWarning:AtrueSSLContextobjectisnot
available.Lconnectionstofail..
Formoreinformation,see io/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloadingpython-memcached-1.58.tar.gz
Collectingsix>=1.4.0(frompython-memcached)
Downloadingsix-1.10.0-py2.py3-none-any.whl
Installingcollectedpackages:six,python-memcached
Runningsetup.pyinstallforpython-memcached...done
Successfullyinstalledpython-memcached-1.58six-1.10.0
zabbix:/root/sbin/xx#
zabbix:/root/sbin/xx#python
Python2.7.3(default,Apr92017,14:47:16)
[GCC4.4.720120313(RedHat4.4.7-17)]onlinux2
Type"help","right","credits"or"license"formoreinformation.
>>>importmemcache
>>>
『柒』 python redis和cache的區別
簡單區別:
1. Redis中,並不是所有的數據都一直存儲在內存中的,這是和Memcached相比一個最大的區別。
2. Redis不僅僅支持簡單的k/v類型的數據,同時還提供list,set,hash等數據結構的存儲。
3. Redis支持數據的備份,即master-slave模式的數據備份。
4. Redis支持數據的持久化,可以將內存中的數據保持在磁碟中,重啟的時候可以再次載入進行使用。
Redis在很多方面具備資料庫的特徵,或者說就是一個資料庫系統,而Memcached只是簡單的K/V緩存
下面是來自redis作者的說法(stackoverflow上面)。
You should not care too much about performances. Redis is faster per core with small values, but memcached is able to use multiple cores with a single executable and TCP port without help from the client. Also memcached is faster with big values in the order of 100k. Redis recently improved a lot about big values (unstable branch) but still memcached is faster in this use case. The point here is: nor one or the other will likely going to be your bottleneck for the query-per-second they can deliver.
You should care about memory usage. For simple key-value pairs memcached is more memory efficient