| 1 | package Data::Chronicle::Subscriber; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Data::Chronicle; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Date::Utility; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use JSON::MaybeUTF8 qw(encode_json_utf8); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Data::Chronicle::Subscriber - Provides callback subscriptions to an efficient data storage for volatile and time-based data | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '0.21'; ## VERSION | |||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | This module contains helper methods which can be used to store and retrieve information | ||||||||||
| 22 | on an efficient storage with below properties: | ||||||||||
| 23 | |||||||||||
| 24 | =over 4 | ||||||||||
| 25 | |||||||||||
| 26 | =item B<Timeliness> | ||||||||||
| 27 | |||||||||||
| 28 | It is assumed that data to be stored are time-based meaning they change over time and the latest version is most important for us. | ||||||||||
| 29 | |||||||||||
| 30 | =item B<Efficient> | ||||||||||
| 31 | |||||||||||
| 32 | The module uses Redis cache to provide efficient data storage and retrieval. | ||||||||||
| 33 | |||||||||||
| 34 | =item B<Persistent> | ||||||||||
| 35 | |||||||||||
| 36 | In addition to caching every incoming data, it is also stored in PostgreSQL for future retrieval. | ||||||||||
| 37 | |||||||||||
| 38 | =item B<Transparent> | ||||||||||
| 39 | |||||||||||
| 40 | This modules hides all the internal details including distribution, caching, and database structure from the developer. He only needs to call a method | ||||||||||
| 41 | to save data and another method to retrieve it. All the underlying complexities are handled by the module. | ||||||||||
| 42 | |||||||||||
| 43 | =back | ||||||||||
| 44 | |||||||||||
| 45 | =head1 EXAMPLE | ||||||||||
| 46 | |||||||||||
| 47 | my $d = get_some_log_data(); | ||||||||||
| 48 | |||||||||||
| 49 | my $chronicle_w = Data::Chronicle::Writer->new( | ||||||||||
| 50 | cache_writer => $writer, | ||||||||||
| 51 | dbic => $dbic, | ||||||||||
| 52 | ttl => 86400, | ||||||||||
| 53 | publish_on_set => 1); | ||||||||||
| 54 | |||||||||||
| 55 | my $chronicle_s = Data::Chronicle::Subscriber->new( | ||||||||||
| 56 | cache_reader => $subscriber); | ||||||||||
| 57 | |||||||||||
| 58 | #subscribe to changes to syslog | ||||||||||
| 59 | $chronicle_s->subscribe("log_files", "syslog", sub { print 'Hello World' }); | ||||||||||
| 60 | |||||||||||
| 61 | #store data into Chronicle - each time we call `set` it will also call | ||||||||||
| 62 | #the stored subroutine | ||||||||||
| 63 | $chronicle_w->set("log_files", "syslog", $d); | ||||||||||
| 64 | |||||||||||
| 65 | #unsubscribe to changes to syslog | ||||||||||
| 66 | $chronicle_s->unsubscribe("log_files", "syslog"); | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | =head1 METHODS | ||||||||||
| 71 | |||||||||||
| 72 | =head2 cache_subscriber | ||||||||||
| 73 | |||||||||||
| 74 | cache_subscriber should be an instance of L<RedisDB>. | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | 2 | has 'cache_subscriber' => ( | |||||||||
| 79 | is => 'ro', | ||||||||||
| 80 | default => undef, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | =head2 subscribe | ||||||||||
| 84 | |||||||||||
| 85 | Example: | ||||||||||
| 86 | |||||||||||
| 87 | $chronicle_writer->subscribe("category1", "name1", $code_ref); | ||||||||||
| 88 | |||||||||||
| 89 | =cut | ||||||||||
| 90 | |||||||||||
| 91 | 1 | ▶ | sub subscribe { | ||||||||
|
Subroutine
subscribe: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 92 | 1 | my ($self, $category, $name, $callback) = @_; | |||||||||
| 93 | 1 | ▶ | die 'Subscription requires a coderef' if ref $callback ne 'CODE'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | |||||||||||
| 95 | 1 | my $key = $self->_generate_key($category, $name); | |||||||||
| 96 | 1 | return $self->cache_subscriber->subscribe($key, $callback); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | =head2 unsubscribe | ||||||||||
| 100 | |||||||||||
| 101 | Example: | ||||||||||
| 102 | |||||||||||
| 103 | $chronicle_writer->unsubscribe("category1", "name1"); | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 1 | ▶ | sub unsubscribe { | ||||||||
|
Subroutine
unsubscribe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 108 | 1 | my ($self, $category, $name) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 1 | my $key = $self->_generate_key($category, $name); | |||||||||
| 111 | 1 | return $self->cache_subscriber->unsubscribe($key); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 2 | ▶ | sub _generate_key { | ||||||||
|
Subroutine
_generate_key: called CC=1 SCAR=0.0
| |||||||||||
| 115 | 2 | my ($self, $category, $name) = @_; | |||||||||
| 116 | 2 | return $category . '::' . $name; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 2 | ▶ | no Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 120 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 121 | |||||||||||
| 122 | =head1 AUTHOR | ||||||||||
| 123 | |||||||||||
| 124 | Binary.com, C<< <support at binary.com> >> | ||||||||||
| 125 | |||||||||||
| 126 | =head1 BUGS | ||||||||||
| 127 | |||||||||||
| 128 | Please report any bugs or feature requests to C<bug-data-chronicle at rt.cpan.org>, or through | ||||||||||
| 129 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Chronicle>. I will be notified, and then you'll | ||||||||||
| 130 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | |||||||||||
| 135 | =head1 SUPPORT | ||||||||||
| 136 | |||||||||||
| 137 | You can find documentation for this module with the perldoc command. | ||||||||||
| 138 | |||||||||||
| 139 | perldoc Data::Chronicle::Subscriber | ||||||||||
| 140 | |||||||||||
| 141 | |||||||||||
| 142 | You can also look for information at: | ||||||||||
| 143 | |||||||||||
| 144 | =over 4 | ||||||||||
| 145 | |||||||||||
| 146 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 147 | |||||||||||
| 148 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-Chronicle> | ||||||||||
| 149 | |||||||||||
| 150 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 151 | |||||||||||
| 152 | L<http://annocpan.org/dist/Data-Chronicle> | ||||||||||
| 153 | |||||||||||
| 154 | =item * CPAN Ratings | ||||||||||
| 155 | |||||||||||
| 156 | L<http://cpanratings.perl.org/d/Data-Chronicle> | ||||||||||
| 157 | |||||||||||
| 158 | =item * Search CPAN | ||||||||||
| 159 | |||||||||||
| 160 | L<https://metacpan.org/release/Data-Chronicle/> | ||||||||||
| 161 | |||||||||||
| 162 | =back | ||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | 2 | 1; | |||||||||