php memcached 和 django memcached 存储不同吗?

Are php memcached and django memcached storage different?

我正在尝试在我的新 Django 项目中实现缓存,这里的问题是,缓存是通过 PHP 服务器设置的,我需要从 Django 代码中读取它。我可以在Django中设置缓存,在Django中读取,我也可以在PHP中设置缓存,在PHP中读取。但是,我无法跨平台进行。即我无法在 Django 中读取 PHP 中设置的缓存,反之亦然。虽然,如果我执行 telnet localhost 11211 并获取两个密钥,我只能获取 PHP 中设置的密钥。 我已经完成 pip install python-memcached 安装以将 Memcached 与 Python 一起使用。 所以,我的问题是如何为 Django 和 PHP 使用通用缓存服务器?

这是我的 PHP 片段

$memObj = new Memcached();
$memObj->addServer('localhost', 11211);
$memObj->set('php_key', 'hello php');
var_dump($memObj->get('django_key')); #prints False
echo $memObj->get('php_key'); #prints 'hello php'

以下是我的 Python/Django 片段

settings.py

CACHES = {
   'default': {
      'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
      'LOCATION': 'localhost:11211',
   }
}

在浏览量中,

from django.core.cache import cache

cache.set('django_key', 'Hello world')
php_cache = cache.get('php_key')
print(php_cache) # Outputs None
django_cache = cache.get('django_key')
print(django_cache) # Outputs 'Hello world'

在ubuntu终端

telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
get php_key
VALUE php_key
hello php
END
get django_key
END

那是因为 django 传递给 memcached 的密钥并不完全是您在调用 cache.set

时使用的密钥

the cache key provided by a user is not used verbatim – it is combined with the cache prefix and key version to provide a final cache key. By default, the three parts are joined using colons to produce a final string

https://docs.djangoproject.com/en/1.10/topics/cache/#cache-key-transformation

调整设置,或创建您自己的 KEY_FUNCTION 以确保 PHP 键与 django 键匹配。