[英]Best way to safely store Python tuple of strings and integers in memcached?
So I have a webapp where I want to store current rankings of posts based on an algo every minute.所以我有一个网络应用程序,我想在其中存储基于每分钟算法的帖子的当前排名。 To render the updated posts, I would like to avoid the db and get the data (eg post title, author, rank, etc.) from memcached (faster, right?).
为了呈现更新的帖子,我想避免使用数据库并从 memcached 中获取数据(例如帖子标题、作者、排名等)(更快,对吗?)。
One idea is (I'm using Django btw) for the view to convert a serialized string from memcached back to python (using pickle) and then rendering a template displaying the posts in the correct order.一个想法是(我正在使用 Django btw)视图将序列化字符串从 memcached 转换回 python(使用 pickle),然后呈现以正确顺序显示帖子的模板。 However, it may be unsafe because the user-submitted data (post title, etc.) may contain characters that may be parsed in an undesirable way.
但是,它可能是不安全的,因为用户提交的数据(帖子标题等)可能包含可能以不希望的方式解析的字符。
1) Is the best solution to just clean all data when these objects are saved or is there another solution? 1)在保存这些对象时清理所有数据的最佳解决方案是最好的解决方案,还是有其他解决方案?
2) If cleaning the data is an option, what characters should be excluded in the various fields of my Post model? 2)如果清理数据是一个选项,我的帖子model的各个字段中应该排除哪些字符?
3) Does pickle.dump require a file to write to, or can I just get a string as output? 3)pickle.dump 是否需要写入文件,或者我可以得到一个字符串为 output?
Thanks guys.多谢你们。
An example of what needs to be stored:需要存储的示例:
((post.id, post.title, post.upvotes, post.author.username), (,,,)... )
(say 25 posts repeating like this) (比如说 25 个这样重复的帖子)
Why not just let django.core.cache take care of that for you?为什么不让 django.core.cache 为您解决这个问题? I'm doing this from a django project that has memcached hooked up as the cache backend:
我从一个 django 项目中执行此操作,该项目已将 memcached 连接为缓存后端:
$ python manage.py shell
In [1]: from django.core.cache import cache
In [2]: cache.set('woot', (3, 'blah', 590))
In [3]: cache.get('woot')
Out[3]: (3, 'blah', 590)
It works for any python object that can be pickled.它适用于任何可以腌制的 python object。
Also, why not just cache the output, with HTML and everything?另外,为什么不直接缓存 output、HTML 和所有内容呢? Django provides good per-view caching and template fragment caching.
Django 提供了良好的按视图缓存和模板片段缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.