in reply to How to cache a socket in cgi programming?

Short answer is "you can't really", at least for vanilla CGI. Every request that's dispatched by your CGI program is a separate process which services the request and then exits. Having said that, you do have options but they require more than just setting up a CGI does.

One option is to move to something like FastCGI or mod_perl, which keep a persistent perl interpreter around which can keep database connections around (see also Apache::DBI for the later).

Another is to use some form of connection caching in your database if it supports it (e.g. pgpool for Postgres).

The problem with these two approaches is that they may not be available if you're using some sort of vanilla lowest-common-denominator web hosting (although FastCGI does seem to be getting more support since Ruby on Rails can use it, so there may be hope).

  • Comment on Re: How to cache a socket in cgi programming?

Replies are listed 'Best First'.
Re^2: How to cache a socket in cgi programming?
by ablmf (Acolyte) on May 23, 2006 at 12:15 UTC
    I already know that each mod_perl process would keep a connection to the database. But the problem is, my program is not connectting to a database. So, Apache::DBI can not help me. Keeping a local copy of data is unaccpetalbe because the data might change quickly. Maybe I should give more details of my task. My job is writing the configuration module for a embeded system that allows administrator to manage it remotely by a client program. The client and server talk to each other by a private protocol. Now my boss realized that CS is not as popular as BS. So he asked me to give him a BS resolution but I should not change codes on the server side. So my plan is like this: | Browser |<---http---->| Web Server |<--private protocol-->| Data |

      Sorry, I should have been more clear and said "connections like database connections (see Apache::DBI for an example how)". Even if you're not specifically caching a DBI handle the principle is the same and you could gain insight into implementation by looking at that module.