this repo has no description
1package CHI::Driver::MongoDB; 2 3use strict; 4use warnings; 5 6use Moose; 7use Carp qw(croak); 8 9our $VERSION = '0.01'; 10 11extends 'CHI::Driver'; 12 13has 'conn' => ( is => 'ro', isa => 'MongoDB::Connection' ); 14has 'db' => ( is => 'ro', isa => 'MongoDB::Database' ); 15has 'collection' => ( is => 'ro', isa => 'MongoDB::Collection' ); 16has 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' ); 17has 'safe' => ( is => 'rw', isa => 'Bool', default => 0 ); 18 19__PACKAGE__->meta->make_immutable(); 20 21sub BUILD { 22 my ( $self, $args ) = @_; 23 24 if ( $self->{conn} && $self->{db_name} ) { 25 $self->{db} = $self->{conn}->get_database( $self->{db_name} ); 26 } 27 elsif ( !$self->{db} ) { 28 croak 'No Database Set'; 29 } 30 31 $self->{collection} = $self->db->get_collection( $self->namespace() ); 32 33 return; 34} 35 36sub fetch { 37 my ( $self, $key ) = @_; 38 39 my $results = $self->collection->find_one( { _id => "$key" }, { data => 1 } ); 40 return ($results) ? $results->{data} : undef; 41} 42 43sub store { 44 my ( $self, $key, $data ) = @_; 45 46 $self->collection->save( { _id => "$key", data => $data }, 47 { safe => $self->{safe} } ); 48 return; 49} 50 51sub remove { 52 my ( $self, $key ) = @_; 53 54 $self->collection->remove( { _id => "$key" }, { safe => $self->{safe} } ); 55 return; 56} 57 58sub clear { 59 shift->collection->drop; 60 return; 61} 62 63sub get_keys { 64 map { $_->{_id} } shift->collection->find( {}, { _id => 1 } )->all; 65} 66 67sub get_namespaces { 68 return shift->db->collection_names(); 69} 70 711; 72 73=pod 74 75=head1 NAME 76 77CHI::Driver::MongoDB - Use MongoDB for cache storage 78 79=head1 VERSION 80 81version 0.01 82 83=head1 SYNOPSIS 84 85 use CHI; 86 87 # Supply a MongoDB database handle 88 # 89 my $cache = CHI->new( driver => 'MongoDB', 90 db => MongoDB::Connection->new->get_database('db_name') ); 91 92 # Or supply a MongoDB Connection handla and database name 93 # 94 my $cache = CHI->new( driver => 'MongoDB', 95 conn => MongoDB::Connection->new, 96 db_name => 'db_name' ); 97=head1 DESCRIPTION 98 99This driver uses a MongoDB table to store the cache. 100 101=for readme stop 102 103=head1 CONSTRUCTOR PARAMETERS 104 105=over 106 107=item namespace 108 109The namespace you pass in will be as the collection name. That 110means that if you don't specify a namespace the cache will be 111stored in a collection called C<chi_Default>. 112 113=item db 114 115The MongoDB::Database handle used to communicate with the db. 116 117=item conn 118 119Optional MongoDB::Connection handle to use instead of the db 120 121=item db_name 122 123Optional database name to use in conjunction with the conn 124 125=item safe 126 127Optional flag to confirm insertion/removal. This will slow down writes significantly. 128 129=back 130 131=for readme continue 132 133=head1 AUTHORS 134 135Nick Mohoric <nick.mohoric@gmail.com> 136 137=head1 COPYRIGHT AND LICENSE 138 139This software is copyright (c) 2011 by Nick Mohoric. 140 141This is free software; you can redistribute it and/or modify it under 142the same terms as the Perl 5 programming language system itself. 143 144=cut 145 146__END__ 147