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