PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/CHI-0.55/lib/CHI/Driver/File.pm

#
Perl | 367 lines | 170 code | 51 blank | 146 comment | 25 complexity | 19cf430b8e16c64d1828c5178ab39ff7 MD5 | raw file
Possible License(s): AGPL-1.0
  1. package CHI::Driver::File;
  2. BEGIN {
  3. $CHI::Driver::File::VERSION = '0.55';
  4. }
  5. use Carp;
  6. use Cwd qw(realpath cwd);
  7. use CHI::Types;
  8. use CHI::Util
  9. qw(fast_catdir fast_catfile unique_id read_dir read_file write_file);
  10. use Digest::JHash qw(jhash);
  11. use File::Basename qw(basename dirname);
  12. use File::Find qw(find);
  13. use File::Path qw(mkpath rmtree);
  14. use File::Spec::Functions qw(catdir catfile splitdir tmpdir);
  15. use Log::Any qw($log);
  16. use Moose;
  17. use strict;
  18. use warnings;
  19. extends 'CHI::Driver';
  20. has '+max_key_length' => ( default => 248 );
  21. has 'depth' => ( is => 'ro', isa => 'Int', default => 2 );
  22. has 'dir_create_mode' => ( is => 'ro', isa => 'Int', default => oct(775) );
  23. has 'file_create_mode' => ( is => 'ro', isa => 'Int', default => oct(666) );
  24. has 'file_extension' => ( is => 'ro', isa => 'Str', default => '.dat' );
  25. has 'path_to_namespace' => ( is => 'ro', lazy => 1, builder => '_build_path_to_namespace' );
  26. has 'root_dir' => ( is => 'ro', isa => 'Str', default => catdir( tmpdir(), 'chi-driver-file' ) );
  27. __PACKAGE__->meta->make_immutable();
  28. sub BUILDARGS {
  29. my ( $class, %params ) = @_;
  30. # Backward compat
  31. #
  32. if ( defined( $params{key_digest} ) ) {
  33. $params{key_digester} = $params{key_digest};
  34. $params{max_key_length} = 0;
  35. }
  36. return \%params;
  37. }
  38. sub _build_path_to_namespace {
  39. my $self = shift;
  40. my $namespace = $self->escape_for_filename( $self->namespace );
  41. $namespace = $self->digest_key($namespace)
  42. if length($namespace) > $self->max_key_length;
  43. return catdir( $self->root_dir, $namespace );
  44. }
  45. # Escape key to make safe for filesystem; if it then grows larger than
  46. # max_key_length, digest it.
  47. #
  48. sub escape_key {
  49. my ( $self, $key ) = @_;
  50. my $new_key = $self->escape_for_filename($key);
  51. if ( length($new_key) > length($key)
  52. && length($new_key) > $self->max_key_length() )
  53. {
  54. $new_key = $self->digest_key($new_key);
  55. }
  56. return $new_key;
  57. }
  58. sub unescape_key {
  59. my ( $self, $key ) = @_;
  60. return $self->unescape_for_filename($key);
  61. }
  62. sub fetch {
  63. my ( $self, $key ) = @_;
  64. my $file = $self->path_to_key($key);
  65. if ( defined $file && -f $file ) {
  66. return read_file($file);
  67. }
  68. else {
  69. return undef;
  70. }
  71. }
  72. sub store {
  73. my ( $self, $key, $data ) = @_;
  74. my $dir;
  75. my $file = $self->path_to_key( $key, \$dir ) or return undef;
  76. mkpath( $dir, 0, $self->{dir_create_mode} ) if !-d $dir;
  77. # Possibly generate a temporary file - if generate_temporary_filename returns undef,
  78. # store to the destination file directly
  79. #
  80. my $temp_file = $self->generate_temporary_filename( $dir, $file );
  81. my $store_file = defined($temp_file) ? $temp_file : $file;
  82. write_file( $store_file, $data, $self->{file_create_mode} );
  83. if ( defined($temp_file) ) {
  84. # Rename can fail in rare race conditions...try multiple times
  85. #
  86. for ( my $try = 0 ; $try < 3 ; $try++ ) {
  87. last if ( rename( $temp_file, $file ) );
  88. }
  89. if ( -f $temp_file ) {
  90. my $error = $!;
  91. unlink($temp_file);
  92. die "could not rename '$temp_file' to '$file': $error";
  93. }
  94. }
  95. }
  96. sub remove {
  97. my ( $self, $key ) = @_;
  98. my $file = $self->path_to_key($key) or return undef;
  99. unlink($file);
  100. }
  101. sub clear {
  102. my ($self) = @_;
  103. my $namespace_dir = $self->path_to_namespace;
  104. return if !-d $namespace_dir;
  105. my $renamed_dir = join( ".", $namespace_dir, unique_id() );
  106. rename( $namespace_dir, $renamed_dir );
  107. rmtree($renamed_dir);
  108. die "could not remove '$renamed_dir'"
  109. if -d $renamed_dir;
  110. }
  111. sub get_keys {
  112. my ($self) = @_;
  113. my @filepaths;
  114. my $re = quotemeta( $self->file_extension );
  115. my $wanted = sub { push( @filepaths, $_ ) if -f && /${re}$/ };
  116. my @keys = $self->_collect_keys_via_file_find( \@filepaths, $wanted );
  117. return @keys;
  118. }
  119. sub _collect_keys_via_file_find {
  120. my ( $self, $filepaths, $wanted ) = @_;
  121. my $namespace_dir = $self->path_to_namespace;
  122. return () if !-d $namespace_dir;
  123. find( { wanted => $wanted, no_chdir => 1 }, $namespace_dir );
  124. my @keys;
  125. my $key_start = length($namespace_dir) + 1 + $self->depth * 2;
  126. my $subtract = -1 * length( $self->file_extension );
  127. foreach my $filepath (@$filepaths) {
  128. my $key = substr( $filepath, $key_start, $subtract );
  129. $key = $self->unescape_key( join( "", splitdir($key) ) );
  130. push( @keys, $key );
  131. }
  132. return @keys;
  133. }
  134. sub generate_temporary_filename {
  135. my ( $self, $dir, $file ) = @_;
  136. # Generate a temporary filename using unique_id - faster than tempfile, as long as
  137. # we don't need automatic removal.
  138. # Note: $file not used here, but might be used in an override.
  139. #
  140. return fast_catfile( $dir, unique_id() );
  141. }
  142. sub get_namespaces {
  143. my ($self) = @_;
  144. my $root_dir = $self->root_dir();
  145. return () if !-d $root_dir;
  146. my @contents = read_dir($root_dir);
  147. my @namespaces =
  148. map { $self->unescape_for_filename($_) }
  149. grep { $self->is_escaped_for_filename($_) }
  150. grep { -d fast_catdir( $root_dir, $_ ) } @contents;
  151. return @namespaces;
  152. }
  153. my %hex_strings = map { ( $_, sprintf( "%x", $_ ) ) } ( 0x0 .. 0xf );
  154. sub path_to_key {
  155. my ( $self, $key, $dir_ref ) = @_;
  156. return undef if !defined($key);
  157. my @paths = ( $self->path_to_namespace );
  158. my $orig_key = $key;
  159. $key = $self->escape_key($key);
  160. # Hack: If key is exactly 32 hex chars, assume it's an md5 digest and
  161. # take a prefix of it for bucket. Digesting will usually happen in
  162. # transform_key and there's no good way for us to know it occurred.
  163. #
  164. if ( $key =~ /^[0-9a-f]{32}$/ ) {
  165. push( @paths,
  166. map { substr( $key, $_, 1 ) } ( 0 .. $self->{depth} - 1 ) );
  167. }
  168. else {
  169. # Hash key to a 32-bit integer (using non-escaped key for back compat)
  170. #
  171. my $bucket = jhash($orig_key);
  172. # Create $self->{depth} subdirectories, containing a maximum of 64
  173. # subdirectories each, by successively shifting 4 bits off the
  174. # bucket and converting to hex.
  175. #
  176. for ( my $d = $self->{depth} ; $d > 0 ; $d-- ) {
  177. push( @paths, $hex_strings{ $bucket & 0xf } );
  178. $bucket >>= 4;
  179. }
  180. }
  181. # Join paths together, computing dir separately if $dir_ref was passed.
  182. #
  183. my $filename = $key . $self->file_extension;
  184. my $filepath;
  185. if ( defined $dir_ref && ref($dir_ref) ) {
  186. my $dir = fast_catdir(@paths);
  187. $filepath = fast_catfile( $dir, $filename );
  188. $$dir_ref = $dir;
  189. }
  190. else {
  191. $filepath = fast_catfile( @paths, $filename );
  192. }
  193. return $filepath;
  194. }
  195. 1;
  196. =pod
  197. =head1 NAME
  198. CHI::Driver::File - File-based cache using one file per entry in a multi-level
  199. directory structure
  200. =head1 VERSION
  201. version 0.55
  202. =head1 SYNOPSIS
  203. use CHI;
  204. my $cache = CHI->new(
  205. driver => 'File',
  206. root_dir => '/path/to/cache/root',
  207. depth => 3,
  208. max_key_length => 64
  209. );
  210. =head1 DESCRIPTION
  211. This cache driver stores data on the filesystem, so that it can be shared
  212. between processes on a single machine, or even on multiple machines if using
  213. NFS.
  214. Each item is stored in its own file. By default, during a set, a temporary file
  215. is created and then atomically renamed to the proper file. While not the most
  216. efficient, it eliminates the need for locking (with multiple overlapping sets,
  217. the last one "wins") and makes this cache usable in environments like NFS where
  218. locking might normally be undesirable.
  219. By default, the base filename is the key itself, with unsafe characters escaped
  220. similar to URL escaping. If the escaped key is larger than L</max_key_length>
  221. (default 248 characters), it will be L<digested|CHI/key_digester>. You may want
  222. to lower L</max_key_length> if you are storing a lot of items as long filenames
  223. can be more expensive to work with.
  224. The files are evenly distributed within a multi-level directory structure with
  225. a customizable L</depth>, to minimize the time needed to search for a given
  226. entry.
  227. =head1 CONSTRUCTOR OPTIONS
  228. When using this driver, the following options can be passed to CHI->new() in
  229. addition to the L<CHI|general constructor options/constructor>.
  230. =over
  231. =item root_dir
  232. The location in the filesystem that will hold the root of the cache. Defaults
  233. to a directory called 'chi-driver-file' under the OS default temp directory
  234. (e.g. '/tmp' on UNIX). This directory will be created as needed on the first
  235. cache set.
  236. =item depth
  237. The number of subdirectories deep to place cache files. Defaults to 2. This
  238. should be large enough that no leaf directory has more than a few hundred
  239. files. Each non-leaf directory contains up to 16 subdirectories (0-9, A-F).
  240. =item dir_create_mode
  241. Permissions mode to use when creating directories. Defaults to 0775.
  242. =item file_create_mode
  243. Permissions mode to use when creating files, modified by the current umask.
  244. Defaults to 0666.
  245. =item file_extension
  246. Extension to append to filename. Default is ".dat".
  247. =back
  248. =head1 METHODS
  249. =over
  250. =item path_to_key ( $key )
  251. Returns the full path to the cache file representing $key, whether or not that
  252. entry exists. Returns the empty list if a valid path cannot be computed, for
  253. example if the key is too long.
  254. =item path_to_namespace
  255. Returns the full path to the directory representing this cache's namespace,
  256. whether or not it has any entries.
  257. =back
  258. =head1 TEMPORARY FILE RENAME
  259. By default, during a set, a temporary file is created and then atomically
  260. renamed to the proper file. This eliminates the need for locking. You can
  261. subclass and override method I<generate_temporary_filename> to either change
  262. the path of the temporary filename, or skip the temporary file and rename
  263. altogether by having it return undef.
  264. =head1 SEE ALSO
  265. L<CHI|CHI>
  266. =head1 AUTHOR
  267. Jonathan Swartz <swartz@pobox.com>
  268. =head1 COPYRIGHT AND LICENSE
  269. This software is copyright (c) 2011 by Jonathan Swartz.
  270. This is free software; you can redistribute it and/or modify it under
  271. the same terms as the Perl 5 programming language system itself.
  272. =cut
  273. __END__