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 'db_name' => ( is => 'ro', isa => 'Str', default => 'chi' ); 16 17__PACKAGE__->meta->make_immutable(); 18 19sub BUILD { 20 my ( $self, $args ) = @_; 21 22 return if $self->{db}; 23 24 if ( $self->{conn} && $self->{db_name} ) { 25 $self->{db} = $self->{conn}->get_database( $self->{db_name} ); 26 } 27 else { 28 croak 'No Database Set'; 29 } 30 31 return; 32} 33 34sub _collection { 35 my ($self) = @_; 36 return $self->db->get_collection( $self->namespace() ); 37} 38 39sub fetch { 40 my ( $self, $key ) = @_; 41 my $results = 42 $self->_collection->find_one( { _id => $key }, { data => 1 } ); 43 return ($results) ? $results->{data} : undef; 44} 45 46sub store { 47 my ( $self, $key, $data ) = @_; 48 $self->_collection->save( { _id => $key, data => $data }, { safe => 1 } ); 49 return; 50} 51 52sub remove { 53 my ( $self, $key ) = @_; 54 $self->_collection->remove( { _id => $key }, { safe => 1 } ); 55 return; 56} 57 58sub clear { 59 shift->_collection->drop; 60 return; 61} 62 63sub get_keys { 64 return map { $_->{_id} } shift->_collection->find( {}, { _id => 1 } )->all; 65} 66 67sub get_namespaces { 68 return shift->db->collection_names( { safe => 1 } ); 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 123Option database name to use in conjunction with the conn 124 125=back 126 127=for readme continue 128 129=head1 AUTHORS 130 131Nick Mohoric <nick.mohoric@gmail.com> 132 133=head1 COPYRIGHT AND LICENSE 134 135This software is copyright (c) 2011 by Nick Mohoric. 136 137This is free software; you can redistribute it and/or modify it under 138the same terms as the Perl 5 programming language system itself. 139 140=cut 141 142__END__ 143