写入 Django 缓存非常慢
Very slow to write to Django cache
我曾经在全局变量中缓存数据库查询以加速我的应用程序。由于这是强烈不建议的(并且它确实产生了问题),我想改用任何类型的 Django 缓存。我尝试了 LocMemCache 和 DatabaseCache,但都需要...大约 15 秒 来设置我的变量(比生成数据所需的时间长两倍,大小为 7MB)。
这是预期的吗?我做错了什么吗?
(Memcached 限制为 1MB,我不能分割我的数据,它由任意大的二进制掩码组成)。
编辑:FileBasedCache 也需要 30 秒才能设置。
Settings.py:
CACHES = {
'default': {...},
'stats': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
# or 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'stats',
},
}
Service.py:
from django.core.cache import caches
def stats_service():
stats_cache = caches['stats']
if stats_cache.get('key') is None:
stats_cache.set('key', data) # 15s with DatabaseCache, 30s with LocMemCache
return stats_cache.get('key')
全局变量(超快)版本:
_cache = {}
def stats_service():
if _cache.get('key') is None:
_cache['key'] = data
return _cache['key']
一个选择可能是使用 diskcache.DjangoCache. DiskCache extends the Django cache API to support writing and reading binary streams as-is (avoid pickling). It works particularly well for large values (like those greater than 1MB). DiskCache is an Apache2 licensed disk and file backed cache library,用纯 Python 编写并与 Django 兼容。
在您的情况下,您可以在缓存中使用 ndarray tostring and numpy fromstring methods to quickly convert to/from a Python string. Then wrap the string with io.StringIO 到 store/retrieve。例如:
from django.core.cache import cache
value = cache.get('cache-key', read=True)
if value:
data = numpy.fromstring(value.read())
value.close()
else:
data = ... # Generate 7MB array.
cachge.set('cache-key', io.StringIO(data.tostring()), read=True)
DiskCache 扩展了 Django 缓存 API 通过允许在磁盘上存储为二进制 blob 的类似文件的值。 Django cache benchmarks 页面对备选缓存后端进行了讨论和比较。
这段代码实际上工作正常:https://djangosnippets.org/snippets/2396/
据我了解,the only problem 使用全局变量进行缓存是线程安全的,而这个 no-pickle 版本是线程安全的。
我曾经在全局变量中缓存数据库查询以加速我的应用程序。由于这是强烈不建议的(并且它确实产生了问题),我想改用任何类型的 Django 缓存。我尝试了 LocMemCache 和 DatabaseCache,但都需要...大约 15 秒 来设置我的变量(比生成数据所需的时间长两倍,大小为 7MB)。
这是预期的吗?我做错了什么吗?
(Memcached 限制为 1MB,我不能分割我的数据,它由任意大的二进制掩码组成)。
编辑:FileBasedCache 也需要 30 秒才能设置。
Settings.py:
CACHES = {
'default': {...},
'stats': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
# or 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'stats',
},
}
Service.py:
from django.core.cache import caches
def stats_service():
stats_cache = caches['stats']
if stats_cache.get('key') is None:
stats_cache.set('key', data) # 15s with DatabaseCache, 30s with LocMemCache
return stats_cache.get('key')
全局变量(超快)版本:
_cache = {}
def stats_service():
if _cache.get('key') is None:
_cache['key'] = data
return _cache['key']
一个选择可能是使用 diskcache.DjangoCache. DiskCache extends the Django cache API to support writing and reading binary streams as-is (avoid pickling). It works particularly well for large values (like those greater than 1MB). DiskCache is an Apache2 licensed disk and file backed cache library,用纯 Python 编写并与 Django 兼容。
在您的情况下,您可以在缓存中使用 ndarray tostring and numpy fromstring methods to quickly convert to/from a Python string. Then wrap the string with io.StringIO 到 store/retrieve。例如:
from django.core.cache import cache
value = cache.get('cache-key', read=True)
if value:
data = numpy.fromstring(value.read())
value.close()
else:
data = ... # Generate 7MB array.
cachge.set('cache-key', io.StringIO(data.tostring()), read=True)
DiskCache 扩展了 Django 缓存 API 通过允许在磁盘上存储为二进制 blob 的类似文件的值。 Django cache benchmarks 页面对备选缓存后端进行了讨论和比较。
这段代码实际上工作正常:https://djangosnippets.org/snippets/2396/
据我了解,the only problem 使用全局变量进行缓存是线程安全的,而这个 no-pickle 版本是线程安全的。