开发者社区> 问答> 正文

Python中使用python-memcache(memcached)的好例子?

我正在使用Python和web.py框架编写Web应用程序,并且需要始终使用memcached。

我一直在互联网上搜索,试图在python-memcached模块上找到一些好的文档,但是我所能找到的只是MySQL网站上的此示例,而其方法上的文档也不是很好。

展开
收起
祖安文状元 2020-02-23 15:35:40 796 0
1 条回答
写回答
取消 提交回答
  • 这很简单。您使用键和到期时间来写入值。您可以使用键获取值。您可以使系统中的密钥到期。

    大多数客户遵循相同的规则。您可以阅读memcached主页上的一般说明和最佳实践。

    如果你真的想深入研究它,我会看看它的来源。这是标题评论:

    """ client module for memcached (memory cache daemon)

    Overview

    See U{the MemCached homepagehttp://www.danga.com/memcached} for more about memcached.

    Usage summary

    This should give you a feel for how this module operates::

    import memcache
    mc = memcache.Client(['127.0.0.1:11211'], debug=0)
    
    mc.set("some_key", "Some value")
    value = mc.get("some_key")
    
    mc.set("another_key", 3)
    mc.delete("another_key")
    
    mc.set("key", "1")   # note that the key used for incr/decr must be a string.
    mc.incr("key")
    mc.decr("key")
    

    The standard way to use memcache with a database is like this::

    key = derive_key(obj)
    obj = mc.get(key)
    if not obj:
        obj = backend_api.get(...)
        mc.set(key, obj)
    
    # we now have obj, and future passes through this code
    # will use the object from the cache.
    

    Detailed Documentation

    More detailed documentation is available in the L{Client} class. """

    2020-04-15 23:39:06
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载