| 1 | package Data::Model::Driver::Cache::HASH; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Data::Model::Driver::Cache'; | ||
| 5 | |||
| 6 | my %CACHE; | ||
| 7 | |||
| 8 | sub add_to_cache { | ||
| 9 | my($self, $key, $data) = @_; | ||
| 10 | |||
| 11 | my $ret = $CACHE{$key} = $data; | ||
| 12 | return if !defined $ret; | ||
| 13 | return $ret; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub get_from_cache { | ||
| 17 | my($self, $key) = @_; | ||
| 18 | |||
| 19 | my $ret = $CACHE{$key}; | ||
| 20 | return if !defined $ret; | ||
| 21 | return $ret; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub remove_from_cache { | ||
| 25 | my($self, $key) = @_; | ||
| 26 | |||
| 27 | my $ret = delete $CACHE{$key}; | ||
| 28 | return if !defined $ret; | ||
| 29 | return $ret; | ||
| 30 | } | ||
| 31 | |||
| 32 | 1; | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Data::Model::Driver::Cache::HASH - Penetration cache is offered to the basic driver | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | package MyDB; | ||
| 41 | use base 'Data::Model'; | ||
| 42 | use Data::Model::Schema; | ||
| 43 | use Data::Model::Driver::DBI; | ||
| 44 | use Data::Model::Driver::Cache::HASH; | ||
| 45 | |||
| 46 | my $dbi_connect_options = {}; | ||
| 47 | my $fallback_driver = Data::Model::Driver::DBI->new( | ||
| 48 | dsn => 'dbi:mysql:host=localhost:database=test', | ||
| 49 | username => 'user', | ||
| 50 | password => 'password', | ||
| 51 | connect_options => $dbi_connect_options, | ||
| 52 | ); | ||
| 53 | |||
| 54 | my $driver = Data::Model::Driver::Cache::HASH->new( | ||
| 55 | fallback => $fallback_driver, | ||
| 56 | ); | ||
| 57 | |||
| 58 | base_driver $driver; | ||
| 59 | install_model model_name => schema { | ||
| 60 | .... | ||
| 61 | }; | ||
| 62 | |||
| 63 | =head1 DESCRIPTION | ||
| 64 | |||
| 65 | Penetration cache is offered to the basic driver. | ||
| 66 | Cash is stored in the standard hash for Perl. | ||
| 67 | |||
| 68 | When cash does not hit, it asks fallback driver. | ||
| 69 | |||
| 70 | =head1 SEE ALSO | ||
| 71 | |||
| 72 | L<Data::Model> | ||
| 73 | |||
| 74 | =head1 AUTHOR | ||
| 75 | |||
| 76 | Kazuhiro Osawa E<lt>yappo <at> shibuya <döt> plE<gt> | ||
| 77 | |||
| 78 | =head1 LICENSE | ||
| 79 | |||
| 80 | This library is free software; you can redistribute it and/or modify | ||
| 81 | it under the same terms as Perl itself. | ||
| 82 | |||
| 83 | =cut |