‘壹’ 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