| 1 | package OpenInteract::Cache::File; | ||
| 2 | |||
| 3 | # $Id: File.pm,v 1.4 2002/09/08 20:56:46 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( OpenInteract::Cache ); | ||
| 7 | use Cache::FileCache; | ||
| 8 | |||
| 9 | $OpenInteract::Cache::File::VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/); | ||
| 10 | |||
| 11 | my $DEFAULT_SIZE = 2000000; # 10 MB -- max size of cache | ||
| 12 | my $DEFAULT_EXPIRE = 86400; # 1 day | ||
| 13 | |||
| 14 | sub initialize { | ||
| 15 | my ( $self, $CONFIG ) = @_; | ||
| 16 | |||
| 17 | # Allow values that are passed in to override anything | ||
| 18 | # set in the config object | ||
| 19 | |||
| 20 | my $cache_dir = $CONFIG->get_dir( 'cache_content' ); | ||
| 21 | unless ( -d $cache_dir ) { | ||
| 22 | warn "Sorry, I cannot create a filesystem cache without a ", | ||
| 23 | "valid directory. (Given [$cache_dir])\n"; | ||
| 24 | return undef; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $cache_info = $CONFIG->{cache_info}{data}; | ||
| 28 | my $max_size = $cache_info->{max_size}; | ||
| 29 | my $default_expire = $cache_info->{default_expire}; | ||
| 30 | my $cache_depth = $cache_info->{directory_depth}; | ||
| 31 | |||
| 32 | # If a value isn't set, use the default from the class | ||
| 33 | # configuration above. | ||
| 34 | |||
| 35 | $max_size ||= $DEFAULT_SIZE; | ||
| 36 | $default_expire ||= $DEFAULT_EXPIRE; | ||
| 37 | |||
| 38 | my $R = OpenInteract::Request->instance; | ||
| 39 | $R->DEBUG && $R->scrib( 1, "Using the following cache settings ", | ||
| 40 | "[Dir $cache_dir] [Size $max_size] ", | ||
| 41 | "[Expire $default_expire] [Depth $cache_depth]" ); | ||
| 42 | return Cache::FileCache->new({ default_expires_in => $default_expire, | ||
| 43 | max_size => $max_size, | ||
| 44 | cache_root => $cache_dir, | ||
| 45 | cache_depth => $cache_depth }); | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub get_data { | ||
| 50 | my ( $self, $cache, $key ) = @_; | ||
| 51 | return $cache->get( $key ); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub set_data { | ||
| 56 | my ( $self, $cache, $key, $data, $expires ) = @_; | ||
| 57 | $cache->set( $key, $data, $expires ); | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub clear_data { | ||
| 63 | my ( $self, $cache, $key ) = @_; | ||
| 64 | $cache->remove( $key ); | ||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ |