简体   繁体   English

在 memcached 中安全存储 Python 字符串和整数元组的最佳方法?

[英]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.

相关问题 存储映射到字符串的整数以使键可以是python范围的最佳方法是什么? - What is the best way to store integers mapped to strings so that the keys can be ranges in python? 将字符串元组编码为整数 - Encoding a tuple of strings into integers 如何在 CSV 文件 Python 中将整数存储为字符串 - How to store integers as strings in CSV file Python 从字符串的数组(或元组)在Python中创建动态sql“in list”子句的“最佳”方法是什么? - What's the “best” way to create a dynamic sql “in list” clause in Python from an array (or tuple) of strings? 从python中的数据文件中仅获取字符串,整数和/或浮点数的最佳方法? - Best way to get just strings, integers, and/or floats from a data file in python? 如何在python for循环中构建整数+字符串的元组数组变量? - How to build tuple array variable of integers + strings in python for loop? 将字符串列表转换为整数元组 - Convert list of strings into tuple of integers python:用于存储字符串的最佳数据结构? - python: best data structure to use to store strings? 存储字典的最佳方法,该字典的键是日期,值是python中的字符串列表 - Best way to store a dictionary of which keys are dates and values are a list of strings in python Python,元组索引必须是整数,而不是元组? - Python, tuple indices must be integers, not tuple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM