django-autocomplete-light:如何缓存选项?
我有一个自己的城市模型(不是django-cities-light),在MySQL表中有超过200万条记录。每次我在自动完成字段中开始输入时,htop表中的CPU负载会超过200%,这看起来是因为脚本在每次自动完成时都请求数据库。
我想把这个表放到memcache中,以避免这种情况,现在我有了以下内容:
autocomplete_light_registry.py
import autocomplete_light
from django.core.cache import cache, InvalidCacheBackendError
from cities.models import City
def prepare_choices(model):
key = "%s_autocomplete" % model.__name__.lower()
try:
qs = cache.get(key)
if cache.get(key): # return if not expired
return qs
except InvalidCacheBackendError:
pass
qs = model.objects.all() # populate cache
cache.set(key, qs, 60*60*24) # if expired or not set
return qs
class CityAutocomplete(autocomplete_light.AutocompleteModelBase):
search_fields = ['city_name']
choices = prepare_choices(City)
autocomplete_light.register(City, CityAutocomplete)
但是它仍然在请求mysql。
有什么建议吗?
更新
我尝试在django shell中为城市表设置缓存,但这个过程出现了段错误的消息。
>>> from django.core.cache import cache
>>> qs = City.objects.all()
>>> qs.count()
2246813
>>> key = 'city_autocomplete'
>>> cache.set(key, qs, 60*60*24)
Segmentation fault
不过我能够把较小的表放入缓存中,我希望能解决这个问题,所以仍然需要答案。
1 个回答
3
cache.set(key, qs, 60*60*24) 出现了段错误
这是因为查询的数据太大了。
你需要在过滤后再进行缓存。
这是我这样做的。虽然不是完美的,但在处理500个元素时效果不错。
def get_autocomplete(request):
if request.is_ajax():
q = request.GET.get('term', '')
results_list = MY_model.objects.filter(title__contains=q).all()
results = []
for result in results_list:
results.append(result)
data = json.dumps(results)
else:
data = 'Nothing to see here!'
mimetype = 'application/json'
return http.HttpResponse(data, mimetype)
这是我在网上找到的。
你最好只保留前10个元素,因为其他的会超出屏幕。