在Python中使用python-memcache(memcached)的好例子吗? [英] Good examples of python-memcache (memcached) being used in Python?

查看:45
本文介绍了在Python中使用python-memcache(memcached)的好例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和web.py框架编写Web应用程序,并且需要在整个过程中使用memcached.

I'm writing a web app using Python and the web.py framework, and I need to use memcached throughout.

我一直在互联网上搜索,试图在 python- memcached 模块,但我只能找到

I've been searching the internet trying to find some good documentation on the python-memcached module, but all I could find was this example on the MySQL website, and the documentation on its methods isn't great.

推荐答案

这很简单.您可以使用键和有效时间来写值.您可以使用键获取值.您可以使系统中的密钥失效.

It's fairly simple. You write values using keys and expiry times. You get values using keys. You can expire keys from the system.

大多数客户端遵循相同的规则.您可以在 memcached主页上阅读通用说明和最佳做法.

Most clients follow the same rules. You can read the generic instructions and best practices on the memcached homepage.

如果您真的想深入研究它,请查看源代码.这是标题注释:

If you really want to dig into it, I'd look at the source. Here's the header comment:

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

Overview
========

See U{the MemCached homepage<http://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.
"""

这篇关于在Python中使用python-memcache(memcached)的好例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆