| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ## Domain Registry Interface, local global cache | 
| 2 |  |  |  |  |  |  | ## | 
| 3 |  |  |  |  |  |  | ## Copyright (c) 2005,2008,2009,2011,2013 Patrick Mevzek . All rights reserved. | 
| 4 |  |  |  |  |  |  | ## | 
| 5 |  |  |  |  |  |  | ## This file is part of Net::DRI | 
| 6 |  |  |  |  |  |  | ## | 
| 7 |  |  |  |  |  |  | ## Net::DRI is free software; you can redistribute it and/or modify | 
| 8 |  |  |  |  |  |  | ## it under the terms of the GNU General Public License as published by | 
| 9 |  |  |  |  |  |  | ## the Free Software Foundation; either version 2 of the License, or | 
| 10 |  |  |  |  |  |  | ## (at your option) any later version. | 
| 11 |  |  |  |  |  |  | ## | 
| 12 |  |  |  |  |  |  | ## See the LICENSE file that comes with this distribution for more details. | 
| 13 |  |  |  |  |  |  | ######################################################################################### | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | package Net::DRI::Cache; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 76 |  |  | 76 |  | 573 | use strict; | 
|  | 76 |  |  |  |  | 77 |  | 
|  | 76 |  |  |  |  | 1582 |  | 
| 18 | 76 |  |  | 76 |  | 212 | use warnings; | 
|  | 76 |  |  |  |  | 83 |  | 
|  | 76 |  |  |  |  | 1555 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 76 |  |  | 76 |  | 202 | use base qw(Class::Accessor::Chained::Fast); | 
|  | 76 |  |  |  |  | 66 |  | 
|  | 76 |  |  |  |  | 32142 |  | 
| 21 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw/ttl/); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 76 |  |  | 76 |  | 278678 | use Net::DRI::Util; | 
|  | 76 |  |  |  |  | 153 |  | 
|  | 76 |  |  |  |  | 2406 |  | 
| 24 | 76 |  |  | 76 |  | 413 | use Net::DRI::Exception; | 
|  | 76 |  |  |  |  | 81 |  | 
|  | 76 |  |  |  |  | 31272 |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =pod | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 NAME | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Net::DRI::Cache - Local cache for Net::DRI | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | Please see the README file for details. | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =head1 SUPPORT | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | For now, support questions should be sent to: | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | Enetdri@dotandco.comE | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | Please also see the SUPPORT file in the distribution. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | Ehttp://www.dotandco.com/services/software/Net-DRI/E | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 AUTHOR | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Patrick Mevzek, Enetdri@dotandco.comE | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Copyright (c) 2005,2008,2009,2011,2013 Patrick Mevzek . | 
| 55 |  |  |  |  |  |  | All rights reserved. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify | 
| 58 |  |  |  |  |  |  | it under the terms of the GNU General Public License as published by | 
| 59 |  |  |  |  |  |  | the Free Software Foundation; either version 2 of the License, or | 
| 60 |  |  |  |  |  |  | (at your option) any later version. | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | See the LICENSE file that comes with this distribution for more details. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =cut | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | #################################################################################################### | 
| 67 |  |  |  |  |  |  | sub new | 
| 68 |  |  |  |  |  |  | { | 
| 69 | 71 |  |  | 71 | 1 | 491 | my ($c,$ttl)=@_; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 71 |  |  |  |  | 263 | my $self={ | 
| 72 |  |  |  |  |  |  | ttl  => $ttl, ## if negative, never use cache | 
| 73 |  |  |  |  |  |  | data => {}, | 
| 74 |  |  |  |  |  |  | }; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 71 |  |  |  |  | 1693 | bless($self,$c); | 
| 77 | 71 |  |  |  |  | 650 | return $self; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub set | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 25 |  |  | 25 | 1 | 440 | my ($self,$regname,$type,$key,$data,$ttl)=@_; | 
| 83 | 25 | 50 |  |  |  | 48 | Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($regname,$type,$key); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 25 |  |  |  |  | 42 | my $now=Net::DRI::Util::microtime(); | 
| 86 | 25 | 100 |  |  |  | 59 | $ttl=$self->{ttl} unless defined($ttl); | 
| 87 | 25 | 50 |  |  |  | 62 | my $until=($ttl==0)? 0 : $now+1000000*$ttl; | 
| 88 | 25 |  |  |  |  | 62 | my %c=(_on    => $now, | 
| 89 |  |  |  |  |  |  | _from  => $regname, | 
| 90 |  |  |  |  |  |  | _until => $until, | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 25 | 50 | 33 |  |  | 92 | if ($data && (ref $data eq 'HASH')) | 
| 94 |  |  |  |  |  |  | { | 
| 95 | 25 |  |  |  |  | 66 | while(my ($k,$v)=each(%$data)) | 
| 96 |  |  |  |  |  |  | { | 
| 97 | 110 |  |  |  |  | 227 | $c{$k}=$v; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 25 | 100 |  |  |  | 39 | if ($self->{ttl} >= 0) ## we really store something | 
| 102 |  |  |  |  |  |  | { | 
| 103 | 4 | 100 |  |  |  | 12 | $self->{data}->{$type}={} unless exists $self->{data}->{$type}; | 
| 104 |  |  |  |  |  |  | ## We store only the last version of a given key, so start from scratch | 
| 105 | 4 |  |  |  |  | 8 | $self->{data}->{$type}->{$key}=\%c; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 25 |  |  |  |  | 59 | return \%c; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | sub set_result_from_cache | 
| 112 |  |  |  |  |  |  | { | 
| 113 | 0 |  |  | 0 | 0 | 0 | my ($self,$type,$key)=@_; | 
| 114 | 0 | 0 |  |  |  | 0 | Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($type,$key); | 
| 115 | 0 | 0 |  |  |  | 0 | return unless exists $self->{data}->{$type}; | 
| 116 | 0 |  |  |  |  | 0 | $self->{data}->{$type}->{$key}->{result_from_cache}=1; | 
| 117 | 0 |  |  |  |  | 0 | return; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | sub get | 
| 121 |  |  |  |  |  |  | { | 
| 122 | 10 |  |  | 10 | 1 | 19 | my ($self,$type,$key,$data,$from)=@_; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 10 | 100 |  |  |  | 33 | return if ($self->{ttl} < 0); | 
| 125 | 5 | 50 |  |  |  | 13 | Net::DRI::Exception::err_insufficient_parameters() unless Net::DRI::Util::all_valid($type,$key); | 
| 126 | 5 |  |  |  |  | 13 | ($type,$key)=Net::DRI::Util::normalize_name($type,$key); | 
| 127 | 5 | 50 |  |  |  | 13 | return unless exists $self->{data}->{$type}; | 
| 128 | 5 | 50 |  |  |  | 14 | return unless exists $self->{data}->{$type}->{$key}; | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 5 |  |  |  |  | 7 | my $c=$self->{data}->{$type}->{$key}; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 5 | 100 | 66 |  |  | 17 | if ($c->{_until} > 0 && (Net::DRI::Util::microtime() > $c->{_until})) | 
| 133 |  |  |  |  |  |  | { | 
| 134 | 1 |  |  |  |  | 4 | delete $self->{data}->{$type}->{$key}; | 
| 135 | 1 |  |  |  |  | 11 | return; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 4 | 100 | 66 |  |  | 18 | return if (defined $from && ($c->{_from} ne $from)); | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 3 | 50 |  |  |  | 6 | if (defined $data) | 
| 141 |  |  |  |  |  |  | { | 
| 142 | 3 | 50 |  |  |  | 14 | return $c->{$data} if exists $c->{$data}; | 
| 143 |  |  |  |  |  |  | } else | 
| 144 |  |  |  |  |  |  | { | 
| 145 | 0 |  |  |  |  | 0 | return $c; | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 0 |  |  |  |  | 0 | return; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub delete_expired | 
| 152 |  |  |  |  |  |  | { | 
| 153 | 1 |  |  | 1 | 0 | 2 | my $self=shift; | 
| 154 | 1 |  |  |  |  | 4 | my $now=Net::DRI::Util::microtime(); | 
| 155 | 1 |  |  |  |  | 2 | my $c=$self->{data}; | 
| 156 | 1 |  |  |  |  | 6 | while(my ($type,$c1)=each(%$c)) | 
| 157 |  |  |  |  |  |  | { | 
| 158 | 1 |  |  |  |  | 1 | while(my ($key,$c2)=each(%{$c1})) | 
|  | 2 |  |  |  |  | 9 |  | 
| 159 |  |  |  |  |  |  | { | 
| 160 | 1 | 50 | 33 |  |  | 11 | delete $c->{$type}->{$key} if ($c2->{_until} > 0 && ($now > $c2->{_until})); | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  | } | 
| 163 | 1 |  |  |  |  | 2 | return; | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | sub delete ## no critic (Subroutines::ProhibitBuiltinHomonyms) | 
| 167 |  |  |  |  |  |  | { | 
| 168 | 1 |  |  | 1 | 0 | 1 | my $self=shift; | 
| 169 | 1 |  |  |  |  | 2 | $self->{data}={}; | 
| 170 | 1 |  |  |  |  | 3 | return; | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | #################################################################################################### | 
| 174 |  |  |  |  |  |  | 1; |