memcached listeing on UDP with Django

白昼怎懂夜的黑 提交于 2019-12-22 20:07:10

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/36374750/memcached-listeing-on-udp-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!