使用Django在UDP上进行memcached侦听 [英] memcached listeing on UDP with Django

查看:70
本文介绍了使用Django在UDP上进行memcached侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我无法通过 UDP 监听 memcached 来正常工作( get 使用Django>设置 删除).


正如我在

给予:

ValueError:无法解析连接字符串:"udp:localhost:11211"

2.设置 CACHES 以使用 pylibmc Python绑定:

  CACHES = {'默认': {'BACKEND':'django.core.cache.backends.memcached.PyLibMCCache','LOCATION':'udp:127.0.0.1:11211',超时":无,}} 

服务器运行良好-可以进一步验证:

 >>>进口django>>>从django.core.cache导入缓存>>>cache.set('udp_key',12)>>>cache.get('udp_key')追溯(最近一次通话):在< module>中,文件<控制台">第1行.在获得的文件"/usr/lib/python2.7/site-packages/django/core/cache/backends/memcached.py"中,第84行val = self._cache.get(key)NotSupportedError:来自memcached_get(:1:udp_key)的错误28:不支持操作 


PS 不要在 TCP UDP 辩论


一个类似的问题- get()set()memcached侦听使用Python的UDP

解决方案

据我所了解的库 libmemcached pylibmc 使用的/a>不支持UDP的 get 操作./p>

我已经跟踪到 get 的缓存调用,直到 libmemcached ,并且发现

libmemcached 的文档确认了所有先前的事实.

使用 django.core.cache.backends.memcached.MemcachedCache 作为后端的选项也被丢弃,因为它仅使用TCP套接字( SOCK_STREAM )进行连接内存缓存.

更新: python-memcached-udp 现在是一个pip包.它的管理器可以根据需要添加更多功能.如果您有兴趣,我们绝对可以为使用UDP的Memcached创建新的Django缓存后端.

Question: I am not able to get memcached listening on UDP, to work (get set delete) with Django.


I have the memcached listening only on UDP 11211, as I have mentioned in the previous question. What I have tried so far:

1.Setting CACHES to use python-memcached Python binding. get and set didn't work with simple settings i.e. 'LOCATION': '127.0.0.1:11211', so tried specifying udp explicitly (using this mention as the rationale):

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'udp:127.0.0.1:11211',
        'TIMEOUT': None,
    }
}

gave:

ValueError: Unable to parse connection string: "udp:localhost:11211"

2.Setting CACHES to use pylibmc Python binding:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': 'udp:127.0.0.1:11211',
        'TIMEOUT': None,
    }
}

The server ran fine - to further verify:

>>> import django
>>> from django.core.cache import cache
>>> cache.set('udp_key', 12)
>>> cache.get('udp_key')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/django/core/cache/backends/memcached.py", line 84, in get
    val = self._cache.get(key)
NotSupportedError: error 28 from memcached_get(:1:udp_key): ACTION NOT SUPPORTED


P.S. Don't make it a memcached on TCP vs UDP debate


A similar question - get() set() memcached listening on UDP using Python

解决方案

As far as I have been able to explore the library libmemcached that pylibmc uses does not support get operations with UDP.

I have traced the cache call to get up to libmemcached and I have found the following code:

    ...
    if (memcached_is_udp(ptr))
    {
      return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
    }
    ...

that coincides with your error as pylibmc's get method is mapped to libmemcached's memcached_get method in the file with the code above (/libmemcached/get.cc).

I have install and configure the same environment in my own machine and I have got identical results.

Nevertheless, the set operation seems to work perfectly as I have observed running memcached in debugging mode.

I have also tried to provide different locations ((PROTOCOL + IP + PORT)s separated by ; in the LOCATION field) for the cache mixing TCP/UDP, but the library DOES NOT SUPPORT mixing protocols either and returns an error.

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
            'LOCATION': 'udp:127.0.0.1:11211;127.0.0.1:11211',
            'TIMEOUT': None,
        }
    }

All the previous facts are confirmed by the documentation of libmemcached.

The option of using django.core.cache.backends.memcached.MemcachedCache as a backend is also discarded as it only uses TCP sockets (SOCK_STREAM) for connecting to memcached.

UPDATE: python-memcached-udp is now a pip package. Its mantainer is open to add more features if needed. If you are interested we definitely could work on creating a new Django cache backend for Memcached with UDP.

这篇关于使用Django在UDP上进行memcached侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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