program story

Python에서 사용되는 python-memcache (memcached)의 좋은 예?

inputbox 2020. 9. 5. 09:53
반응형

Python에서 사용되는 python-memcache (memcached)의 좋은 예? [닫은]


저는 Python과 web.py 프레임 워크를 사용하여 웹 앱을 작성 중이며 전체적으로 memcached를 사용해야합니다.

나는 python-memcached 모듈 에 대한 좋은 문서를 찾으려고 인터넷을 검색해 왔지만 내가 찾을 수있는 것은 MySQL 웹 사이트의이 예제 뿐이며 그 방법에 대한 문서는 훌륭하지 않습니다.


매우 간단합니다. 키와 만료 시간을 사용하여 값을 씁니다. 키를 사용하여 값을 얻습니다. 시스템에서 키를 만료 할 수 있습니다.

대부분의 클라이언트는 동일한 규칙을 따릅니다. memcached 홈페이지 에서 일반적인 지침과 모범 사례를 읽을 수 있습니다 .

정말로 파헤 치고 싶다면 출처를 살펴 보겠습니다. 다음은 헤더 주석입니다.

"""
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.
"""

I would advise you to use pylibmc instead.

It can act as a drop-in replacement of python-memcache, but a lot faster(as it's written in C). And you can find handy documentation for it here.

And to the question, as pylibmc just acts as a drop-in replacement, you can still refer to documentations of pylibmc for your python-memcache programming.


A good rule of thumb: use the built-in help system in Python. Example below...

jdoe@server:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import memcache
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'memcache']
>>> help(memcache)

------------------------------------------
NAME
    memcache - client module for memcached (memory cache daemon)

FILE
    /usr/lib/python2.7/dist-packages/memcache.py

MODULE DOCS
    http://docs.python.org/library/memcache

DESCRIPTION
    Overview
    ========

    See U{the MemCached homepage<http://www.danga.com/memcached>} for more about memcached.

    Usage summary
    =============
...
------------------------------------------

참고URL : https://stackoverflow.com/questions/868690/good-examples-of-python-memcache-memcached-being-used-in-python

반응형