memcached listeing on UDP with Django
Question: **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](http://stackoverflow.com/questions/36055900/get-set-
memcached-listening-on-udp-using-python). 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](http://sendapatch.se/projects/pylibmc/reference.html#pylibmc.Client)
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](http://stackoverflow.com/questions/36055900/get-set-memcached-
listening-on-udp-using-python)


Answer: As far as I have been able to explore the library
[libmemcached](http://libmemcached.org/) that
[pylibmc](http://sendapatch.se/projects/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](http://bazaar.launchpad.net/~tangent-
trunk/libmemcached/1.2/view/head:/libmemcached/get.cc):

    
    
        ...
        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](http://docs.libmemcached.org/memcached_behavior.html?highlight=udp#MEMCACHED_BEHAVIOR_USE_UDP).

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](https://pypi.python.org/pypi/python-
memcached-udp/) is now a pip package. Its mantainer [is open to add more
features if needed](https://github.com/idanmo/python-memcached-udp/issues/3).
If you are interested we definitely could work on creating a new Django cache
backend for Memcached with UDP.

