Mike,

Thank you very much for explaining it to me. I had no idea that the
cache middleware wouldn't read the file and cache it along with the
response. I figured caching the file itself as well would be
redundant, which could be costly depending on the size of these files
I am storing in memory. But now that I know the file isn't getting
cached, it makes sense. If it is a bug, that's no big deal because I
will do what you said and read the file into memory so it does get
cached. But I can definitely understand why it wouldn't be a bug,
since loading the files into memory without the programmer doing it
purposefully could be bad too, and very costly in memory (if I am
understanding the issue correctly).

I will definitely go about caching the files the non-lazy way from now
on :)

Thanks,
Brandon

On Mar 27, 5:45 pm, Mike Axiak <[EMAIL PROTECTED]> wrote:
> Brandon,
>
> It appears that the Cache Middleware does not read the content from
> the file before caching the response [1]. I'm not sure if this is a
> bug or not...though I'd probably lean towards it being a bug. (Do we
> not cache middleware to *always* evaluate a file object before caching
> the response?)
> Anyway, until that behavior gets changed, there's no reason you can't
> read (via .read()) your files to just send your content.
>
> If you expect Django to cache all your data, then you have to read the
> entire file into memory at *some* point. So I see two options:
>    1. Read all the file contents into memory by calling
> file_object.read() before putting it into the HttpResponse object.
>    2. Disabling cache middleware for that view. To do this, you can
> use the cache_control decorator [2]. E.g.:
>       @cache_control(max_age=0)
>       dev view(request): ...
>
> As it is your kind of giving Django conflicting requests since you're
> saying "Don't load the file into memory, load it lazily" while also
> saying "load the entire response into the cache".
>
> Regards,
> Mike
>
> 1:http://code.djangoproject.com/browser/django/trunk/django/middleware/...
> 2:http://www.djangoproject.com/documentation/cache/#controlling-cache-u...
>
> On Mar 27, 6:13 pm, bfrederi <[EMAIL PROTECTED]> wrote:
>
> > I appreciate the help Rajesh,
>
> > I haven't added any special settings for the cachemiddleware, I just
> > placed 'django.middleware.cache.CacheMiddleware' in my middleware
> > classes and that's it. As far as the low-level caching that I have
> > written into my code using the "from django.core.cache import cache",
> > all of that is working fine. It's just the middleware that is the
> > issue. As soon as I set the cache middleware in my middleware classes
> > setting, that's when I start having problems.
>
> > Here is an example view, where I try to open a file and it gives me a
> > "ValueError: I/O operation on closed file":
> > view (the line it actually executes is line 24):http://dpaste.com/41671/
> > function it calls (open_system_file):http://dpaste.com/41672/
>
> > Every part of that view works fine except for when I go to open that
> > file on line 24. And it works fine in my tests.py, I get the exact
> > data that I am supposed to from the file (the specific test is on line
> > 22):http://dpaste.com/41678/(open_system_fileon line 23 is defined
> > in system_methods on line 3)
>
> > And as far as stack traces go, I have my mod python error I just gave
> > you. Here is the apache2 error log (I blocked out my ip out of
> > paranoia):http://dpaste.com/41682/Idon't know what else to give you
> > that would help as far as problems within the stack. I'm not doing
> > anything db related, so if there is something else you need me to
> > post, please mention it by name specifically, in case there is
> > something I'm not thinking of.
>
> > Thanks for the help,
> > Brandon
>
> > On Mar 27, 4:01 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
>
> > > Hi Brandon,
>
> > > > I would paste in some code examples, but this project is several
> > > > hundred lines long, and there are several incidences where I open
> > > > files that is conflicting with the cache middleware.
>
> > > Not knowing how you have your cache setup (anonymous, all views, low-
> > > level), it's hard to tell what's causing this problem. You haven't
> > > included full stack traces either. So, I would suggest including at
> > > least this:
>
> > > - A snippet of a view that fails...include at least one file opening
> > > command.
> > > - Full stacktraces of the two errors you mentioned along with the
> > > snippets of your application code that the stack traces point to.
>
> > > -Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to