python memcached在第一次获取时速度慢?

时间:2014-08-05 11:53:08

标签: python memcached

这是我的测试:

from time import time
import pylibmc

MEMCACHED_SERVERS = ['127.0.0.1:11211']

# We have no SASL support for memcached, so we can not use authentication
MEMCACHED_USERNAME = None
MEMCACHED_PASSWORD = None

MEMCACHED_POOL_SIZE = 100   # Used by ClientPool: in order to avoid blocking threads, at least the same size as the number of threads using this facility

mc = pylibmc.Client(MEMCACHED_SERVERS, binary=True,
                    username=MEMCACHED_USERNAME, password=MEMCACHED_PASSWORD,
                    behaviors={"tcp_nodelay" : True,
                                "ketama"     : True })

mc_pool = pylibmc.ClientPool(mc, MEMCACHED_POOL_SIZE)
MEMCACHED_BLOCK = True

def test():
    key = 'api-tree'
    s = time()
    for _ in xrange(20):
        with mc_pool.reserve(block=MEMCACHED_BLOCK) as mc:
            mydata = mc.get(key)
        c = time()
        print 'Ellapsed %d' % (c*1000 - s*1000)
        s = c

test()

这是输出:

Ellapsed 388
Ellapsed 3
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2
Ellapsed 2

这总是发生。改变MEMCACHED_POOL_SIZEMEMCACHED_BLOCK都不会产生任何影响。

为什么首次提取时memcached会变慢?我可以加快速度吗?

0 个答案:

没有答案