Caching in memory for Apache

Paul Boddie paul at boddie.org.uk
Fri Mar 24 12:12:07 EST 2006


Simon  Johnson wrote:
>
> I have decided to take the big plunge and drop the Microsoft platform
> and use Mod_Python and Apache in it's place.

Congratulations! ;-)

[...]

> One of the questions I have is: How do you cache stuff in memory? Say
> I've got a bunch of stuff I need to create from a set of database rows.
> Obviously, I do not want to consult the database each time I have a
> request for my page. Is there anyway in Mod_python for me to store
> values inside memory so I can look it up on the next GET request?

I suppose that you probably need to use sessions in your application in
order to achieve what you're aiming for. If you're not familiar with
the concept of a session in the context of Web applications, sessions
are just things which associate a particular user or client with
persistent information stored on the server: when sending responses
(ie. Web pages) to users/clients, you include a token which those
users/clients then submit back to you with each request; you use these
tokens to look up any information that you've stored for quick
retrieval; the method of transmitting tokens is most often a "cookie"
which is automatically sent back by browsers unless configured
otherwise.

I know that mod_python does support sessions, including in-memory
sessions, as do a number of different frameworks. It should be fairly
convenient to take database rows and store them in a session, and in
most cases the framework (eg. mod_python) will manage the low-level
details (such as the sending of cookies, and so on). Sometimes, there
are restrictions on what the persistent information in sessions can
actually look like: some implementations use things like pickle, and
you can run into "unpickleable" objects; some implementations recommend
that you only store things like strings or text.

Whether sessions are ultimately the "right" answer rests upon a number
of technical considerations, and some people have stated that they
believe them to be generally inappropriate. However, they're probably
the most immediately appropriate thing for you to use in this case.

Paul




More information about the Python-list mailing list