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 );
18has 'use_oid' => ( is => 'rw', isa => 'Bool', default => 0 );
19
20__PACKAGE__->meta->make_immutable();
21
22sub BUILD {
23 my ( $self, $args ) = @_;
24
25 if ( $self->{conn} && $self->{db_name} ) {
26 $self->{db} = $self->{conn}->get_database( $self->{db_name} );
27 }
28 elsif ( !$self->{db} ) {
29 croak 'No Database Set';
30 }
31
32 $self->{collection} = $self->db->get_collection( $self->namespace() );
33
34 return;
35}
36
37sub _collection {
38 my ($self) = @_;
39 return $self->db->get_collection( $self->namespace() );
40}
41
42sub fetch {
43 my ( $self, $key ) = @_;
44 $key =
45 ( $self->{use_oid} )
46 ? MongoDB::OID->new( value => unpack( "H*", $key ) )
47 : $key;
48
49 my $results = $self->collection->find_one( { _id => $key }, { data => 1 } );
50 return ($results) ? $results->{data} : undef;
51}
52
53sub store {
54 my ( $self, $key, $data ) = @_;
55 $key =
56 ( $self->{use_oid} )
57 ? MongoDB::OID->new( value => unpack( "H*", $key ) )
58 : $key;
59
60 $self->collection->save( { _id => $key, data => $data },
61 { safe => $self->{safe} } );
62 return;
63}
64
65sub remove {
66 my ( $self, $key ) = @_;
67 $key =
68 ( $self->{use_oid} )
69 ? MongoDB::OID->new( value => unpack( "H*", $key ) )
70 : $key;
71
72 $self->collection->remove( { _id => $key }, { safe => $self->{safe} } );
73 return;
74}
75
76sub clear {
77 shift->collection->drop;
78 return;
79}
80
81sub get_keys {
82 return ( !shift->{use_oid} )
83 ? map { $_->{_id} } shift->collection->find( {}, { _id => 1 } )->all
84 : croak 'Cannot get keys when converting keys to OID';
85}
86
87sub get_namespaces {
88 return shift->db->collection_names();
89}
90
911;
92
93=pod
94
95=head1 NAME
96
97CHI::Driver::MongoDB - Use MongoDB for cache storage
98
99=head1 VERSION
100
101version 0.01
102
103=head1 SYNOPSIS
104
105 use CHI;
106
107 # Supply a MongoDB database handle
108 #
109 my $cache = CHI->new( driver => 'MongoDB',
110 db => MongoDB::Connection->new->get_database('db_name') );
111
112 # Or supply a MongoDB Connection handla and database name
113 #
114 my $cache = CHI->new( driver => 'MongoDB',
115 conn => MongoDB::Connection->new,
116 db_name => 'db_name' );
117=head1 DESCRIPTION
118
119This driver uses a MongoDB table to store the cache.
120
121=for readme stop
122
123=head1 CONSTRUCTOR PARAMETERS
124
125=over
126
127=item namespace
128
129The namespace you pass in will be as the collection name. That
130means that if you don't specify a namespace the cache will be
131stored in a collection called C<chi_Default>.
132
133=item db
134
135The MongoDB::Database handle used to communicate with the db.
136
137=item conn
138
139Optional MongoDB::Connection handle to use instead of the db
140
141=item db_name
142
143Optional database name to use in conjunction with the conn
144
145=item safe
146
147Optional flag to confirm insertion/removal. This will slow down writes significantly.
148
149=item use_oid
150
151Optional flag to convert key to OID. This speeds up gets, slows retrieval, and removes the ability to get_keys.
152
153=back
154
155=for readme continue
156
157=head1 AUTHORS
158
159Nick Mohoric <nick.mohoric@gmail.com>
160
161=head1 COPYRIGHT AND LICENSE
162
163This software is copyright (c) 2011 by Nick Mohoric.
164
165This is free software; you can redistribute it and/or modify it under
166the same terms as the Perl 5 programming language system itself.
167
168=cut
169
170__END__
171