CHI::Driver::File - File-based cache using one file per entry in a multi-level directory structure



NAME

CHI::Driver::File -- File-based cache using one file per entry in a multi-level directory structure


SYNOPSIS

    use CHI;
    my $cache = CHI->new(driver => 'File', root_dir => '/path/to/cache/root', depth => 3);


DESCRIPTION

This cache driver stores data on the filesystem, so that it can be shared between processes on a single machine, or even on multiple machines if using NFS.

Each item is stored in its own file. During a set, a temporary file is created and then atomically renamed to the proper file. While not the most efficient, it eliminates the need for locking (with multiple overlapping sets, the last one "wins") and makes this cache usable in environments like NFS where locking might normally be undesirable.

The base filename is the key itself, with unsafe characters replaced with an escape sequence similar to URI escaping. The filename length is capped at 255 characters, which is the maximum for most Unix systems, so gets/sets for keys that escape to longer than 255 characters will fail.

The files are evenly distributed within a multi-level directory structure with a customizable depth, to minimize the time needed to search for a given entry.


CONSTRUCTOR OPTIONS

When using this driver, the following options can be passed to CHI->new() in addition to the CHI.


=over
root_dir

The location in the filesystem that will hold the root of the cache. Defaults to a directory called 'chi-driver-file' under the OS default temp directory (e.g. '/tmp' on UNIX).

For backward compatibility with Cache::FileCache, this can also be specified as cache_root.

dir_create_mode

Permissions mode to use when creating directories. Defaults to 0775.

file_create_mode

Permissions mode to change cache files to after creation, using chmod, e.g. 0666 or 0664. Default is to not use chmod and just create files with the current umask.

depth

The number of subdirectories deep to place cache files. Defaults to 2. This should be large enough that no leaf directory has more than a few hundred files. At present, each non-leaf directory contains up to 16 subdirectories, meaning a potential of 256 leaf directories.


=head1 METHODS
path_to_key ( $key )

Returns the full path to the cache file representing $key, whether or not that entry exists. Returns the empty list if a valid path cannot be computed, for example if the key is too long.

path_to_namespace

Returns the full path to the directory representing this cache's namespace, whether or not it has any entries.


SEE ALSO

CHI


AUTHOR

Jonathan Swartz


COPYRIGHT & LICENSE

Copyright (C) 2007 Jonathan Swartz.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

 CHI::Driver::File - File-based cache using one file per entry in a multi-level directory structure