I'm writing a blog in Python and have come to the point where I have to decide on the URL scheme to use. It's tempting to just list the entries start to end, like:
http://myblog.com/1
http://myblog.com/2
...
http://myblog.com/1568
And on the server side I would just have the blog entries in a python list. My fear though is that it will become slow to traverse to the requested entry as the list grows. I don't know how big (as in memory) the entries will be so I can't store them in a lower level array (if there even is in python) or fixed size table of any sort.
The other optio开发者_C百科n I'm thinking of is to prefix the entry URLs with a year:
http://myblog.com/2010/1
http://myblog.com/2010/2
http://myblog.com/2011/1
I guess this speeds things up as I could store the entries in a tree structure:
entries = {
'2010': [entry1, entry2, ...],
'2012': [entry1, entry2, ...]
}
How would you do it? I leave the discussion open as I'm interested in how people think here.
Thanks!
The performance difference between the two will be negligible. The overhead of the list or dictionary will be much smaller than the size of the text in the entries.
Keeping everything in memory is not a good solution though. You should persist the entries to disk (database, flat file, etc.), then (if necessary) use a cache to keep the most frequently accessed entries in memory.
What about mapping URLs to blog entries using a dictionary? This is simple, and dictionary has O(1) lookup so it would be fast even if your blog had many entries.
精彩评论