简体   繁体   中英

fastest way to store comment data python

Hi I have a small comment shoutbox type cgi process running on a server and currently when someone leaves a comment I simply format that comment into html ie

<p class="title">$title</p> <p class="comment">$comment</p>

and store in a flat file. Would it be faster and acceptably low in LOC to reimplement the storage in xml or json, in a simple spec of my own or stick with the simple html route?.

I don't want to use relational database for this.

If a flat file is fast enough, then go with that, since it's very simple and accessible. Storing as XML and JSON but still using a flat file probably is very comparable in performance.

You might want to consider (ignore this if you just left it out of your question) sanitizing/filtering the text, so that users can't break your HTML by eg entering "</p>" in the comment text.

XML is nice, clean way to store this type of data. In Python, you could use lxml to create/update the file:

from lxml import etree

P_XML = 'xml_file_path.xml'

def save_comment(title_text, comment_text):
  comment = etree.Element('comment')
  title = etree.SubElement(comment, 'title')
  title.text = title_text
  comment.text = comment_text
  f = open(P_XML, 'a')
  f.write(etree.tostring(comment, pretty_print=True))
  f.close()

save_comment("FIRST!", "FIRST COMMENT!!!")
save_comment("Awesome", "I love this site!")

That's a simple start, but you could do a lot more (ie set up an ID for each comment, read in the XML using lxml parser and add to it instead of just appending the file).

A flat-file is the fastest form of persistence. Period. There's no formatting, encoding, indexing, locking, or anything.

JSON (and YAML) impose some overheads. They will be slower. There's some formatting that must be done.

XML imposes more overheads than JSON/YAML. It will be slower still. There's a fair amount of formatting that must be done.

The more overhead, the slower it will be.

None of these have anything to do with sanitizing the comment input so that it will display as valid HTML. You should use cgi.escape to escape any HTML-like character sequences in the comment before saving the text to a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM