tags:

views:

132

answers:

7

My Django application retrieves an RSS feed every day. I would like to persist the time the feed was last updated somewhere in the app. I'm only retrieving one feed, it will never grow to be multiple feeds. How can I persist the last updated time?

My ideas so far

  1. Create a model and add a datetime field to it. This seems like overkill as it adds another table to the database, in which there will only ever be one row. Other than that, it's the most obvious and straight-forward solution.

  2. Create a settings object which just stores key/value mappings. The last updated date would just be row in this database. This is essentially a generic version of the previous solution.

  3. Use dbsettings/django-values, which allows you to store settings in the database. The last updated date would just be a 'setting'.

Any other ideas that I'm missing?

A: 

If you need it only for caching purposes, why not store it in the memcached?

On the other hand, if you use this data for other purposes (e.g. display it on the page, or to make some calculation, etc.), then I would store it in a new model - in Django, all persistence is built on top of the database, via models, and I would not try to use other "clever" solutions.

Roberto Liffredo
A: 

One solution I've used in the past is to use Django's cache feature. You set a value to True with an expiration time of one day (in your case.) If the value is not set, you fetch the feed, otherwise you don't do anything.

You can see my solution here: Importing your Flickr photos with Django

Blixt
A: 

One thing I used to do when I was deving with PHP, was to store the xml somewhere, but with a new tag inserted to hold the timestamp of the latest retrieval. It wasn't great, but it was quick and simple.

geowa4
A: 

In spite of the fact databases regularly store many rows in any given table, having a table with only one row is not especially costly, so long as you don't have (m)any indexes, which would waste space. In fact most databases create many single row tables to implement some features, like monotonic sequences used for generating primary keys. I encourage you to create a regular model for this.

TokenMacGuy
This is what I ended up doing. After more thought, I did create a model.
scompt.com
A: 
  1. RAM is volatile, thus not persistent: memcached is not what you asked for.
  2. XML it is not the right technology to store a single value.
  3. RDMS is not the right technology to store a single value.
  4. Django cache framework will answer your question if CACHE_BACKEND is set to anything else than file://...

The filesystem is the right technology to "persist a single value".

In settings.py:

 RSS_FETCH_DATETIME_PATH=os.path.join(
     os.path.abspath(os.path.dirname(__file__)),
     'rss_fetch_datetime'
 )

In your rss fetch script:

 from django.conf import settings
 handler = open(RSS_FETCH_DATETIME_PATH, 'w+')
 handler.write(int(time.time()))
 handler.close()

Wherever you need to read it:

 from django.conf import settings
 handler = open(RSS_FETCH_DATETIME_PATH, 'r+')
 timestamp = int(handler.read())
 handler.close()

But cron is the right tool if you want to "run a command every day", for example at 5AM:

 0 5 * * * /path/to/manage.py runscript /path/to/retreive/script

Of course, you can still write the last update timestamp in a file at the end of the retreive script, and use it somewhere else, if that makes sense to you.

Concluding by quoting Ken Thompson:

One of my most productive days was throwing away 1000 lines of code.

jpic
A: 

Keeping it simple would lead to the idea of just storing it in the file system ... why can't you do that? You could, for example, have a siteconfig module in one of your apps which held these sorts of data. This could load up data from a specific file, which could be text, JSON, ConfigParser, pickle or any suitable format. Just import siteconfig somewhere, and it can load the data and make it available to the other modules in your site. You could easily extend this to hold a dict-like object with a number of settings (e.g., if you ever have multiple feeds, but don't want to have a model just for 2-3 rows, you could easily hold the last-retrieved time for each feed in a dict keyed by feed URL).

Vinay Sajip
A: 

Create a session key, which persists forever and update the feed timestamp every time you access it.

tjazz