in reply to Caching files question
You could use something like this:
my %cache; sub myOpen { my( $mode, $path ) = @_; my $fh; if( exists $cache{ $path } ) { open $fh, $mode, \$cache{ $path } or return; } else { open $fh, $mode, $path or return; { local $/; sysread( $fh, $cache{ $path }, -s( $path ) ) or return; close $fh; } open $fh, $mode, \$cache{ $path } or return; } return $fh; }
Substitute calls to myOpen() for your existing calls to open. If the file hasn't been opened before, it is opened in the usual way, but its contents are slurped into the cache hash, keyed by the pathname, and the file is closed.
That hash value (scalar) is then opened as a ram file, and the ramfile filehandle returned to the caller. If the path already exists in the cache, just this last step is required.
I'm assuming these files are read-only. You can write to ramfiles, but then you're into the task of ensuring that they get written back to the filesystem in a timely manner.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Caching files question
by graff (Chancellor) on Aug 17, 2008 at 14:23 UTC | |
by vit (Friar) on Aug 17, 2008 at 15:37 UTC | |
by graff (Chancellor) on Aug 17, 2008 at 18:57 UTC | |
by BrowserUk (Patriarch) on Aug 18, 2008 at 00:02 UTC |