| 1 | #!/usr/bin/perl -W | ||
| 2 | package AI::MicroStructure::Cache; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Getopt::Long; | ||
| 6 | use Digest::MD5 qw(md5_hex); | ||
| 7 | use Storable qw(lock_retrieve lock_store); | ||
| 8 | our $VERSION = '0.013'; | ||
| 9 | sub new { | ||
| 10 | my $pkg = shift; | ||
| 11 | my $self = bless {}, $pkg; | ||
| 12 | |||
| 13 | our %opts = ( | ||
| 14 | timeout => -1, | ||
| 15 | cache => -1, | ||
| 16 | flush=>0, | ||
| 17 | max_cache_age => -1, | ||
| 18 | cache_file => "~/zcall_cache", | ||
| 19 | |||
| 20 | ); | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | GetOptions (\%opts, | ||
| 25 | "timeout=i", | ||
| 26 | "cache!", | ||
| 27 | "flush=i", | ||
| 28 | "max_cache_age=i", | ||
| 29 | "cache_file=s"); | ||
| 30 | |||
| 31 | |||
| 32 | $self->{cache_file} = $opts{cache_file}; | ||
| 33 | $self->{cache_file} = $pkg->tildeexp($self->{cache_file}); | ||
| 34 | $self->{opts} = %opts; | ||
| 35 | eval { | ||
| 36 | |||
| 37 | $self->{cache} = lock_retrieve($self->{cache_file}); | ||
| 38 | if($opts{flush}==1){ | ||
| 39 | $self->{cache} = {}; | ||
| 40 | lock_store($self->{cache},$self->{cache_file}); | ||
| 41 | } | ||
| 42 | }; | ||
| 43 | |||
| 44 | |||
| 45 | |||
| 46 | return $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub insertCache { | ||
| 51 | |||
| 52 | my $self = shift; | ||
| 53 | my $key = shift; | ||
| 54 | |||
| 55 | $self->{"make-time"} = time; | ||
| 56 | $self->{"expire"} = $self->{"make-time"}+$self->{opts}{max_cache_age}; | ||
| 57 | $self->{data} = {} unless(defined($self->{data})); | ||
| 58 | $self->{data}->{$key}=$self->{data}->{$key}?++$self->{data}->{$key}:1; | ||
| 59 | lock_store($self->{cache},$self->{cache_file}); | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | $self->insert(@_) if @_; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub tildeexp { | ||
| 68 | my $self= shift; | ||
| 69 | my $path = shift; | ||
| 70 | $path =~ s{^~([^/]*)} { | ||
| 71 | $1 | ||
| 72 | ? (getpwnam($1))[7] | ||
| 73 | : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7]) | ||
| 74 | }ex; | ||
| 75 | return $path; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub store{ | ||
| 79 | |||
| 80 | my $self= shift; | ||
| 81 | my $cache = shift; | ||
| 82 | |||
| 83 | lock_store($cache,$self->{cache_file}); | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | sub members { | ||
| 88 | return values %{$_[0]}; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub size { | ||
| 92 | return scalar keys %{$_[0]}; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub insert { | ||
| 96 | my $self = shift; | ||
| 97 | my $data = shift; | ||
| 98 | |||
| 99 | $self->{cache} = lock_retrieve($self->{cache_file}); | ||
| 100 | $self->{cache} = $data; | ||
| 101 | |||
| 102 | lock_store($data, $self->{cache_file}); | ||
| 103 | |||
| 104 | } | ||
| 105 | |||
| 106 | sub refetch { | ||
| 107 | my ($self, $call) = @_; | ||
| 108 | |||
| 109 | $self->{cache}->{$call}->{data}=time; | ||
| 110 | |||
| 111 | lock_store($self->{cache}, $self->{cache_file}); | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | sub check_cache { | ||
| 116 | my ($self, $call) = @_; | ||
| 117 | |||
| 118 | $self->{cache} = lock_retrieve($self->{cache_file}); | ||
| 119 | |||
| 120 | if(!$self->{cache}->{$call}->{data}){ | ||
| 121 | return 0; | ||
| 122 | }else{ | ||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | sub retrieve { return @_; } | ||
| 127 | sub includes { exists $_[0]->{ $_[1]->name } } | ||
| 128 | sub includes_name { exists $_[0]->{ $_[1] } } | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | 1; | ||
| 133 | # ABSTRACT: this is part of AI-MicroStructure the cache will be containing micro structure parts in http user session context | ||
| 134 | |||
| 135 | |||
| 136 | =head1 NAME | ||
| 137 | |||
| 138 | AI::MicroStructure::Cache | ||
| 139 | |||
| 140 | =head1 DESCRIPTION | ||
| 141 | |||
| 142 | Creates Concepts for words | ||
| 143 | |||
| 144 | =head1 SYNOPSIS | ||
| 145 | |||
| 146 | ~$ micro new world | ||
| 147 | |||
| 148 | ~$ micro structures | ||
| 149 | |||
| 150 | ~$ micro any 2 | ||
| 151 | |||
| 152 | ~$ micro drop world | ||
| 153 | |||
| 154 | ~$ micro | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Hagen Geissler <santex@cpan.org> | ||
| 159 | |||
| 160 | =head1 COPYRIGHT AND LICENCE | ||
| 161 | |||
| 162 | Hagen Geissler <santex@cpan.org> | ||
| 163 | |||
| 164 | =head1 SUPPORT AND DOCUMENTATION | ||
| 165 | |||
| 166 | ☞ [sample using concepts](http://quantup.com) | ||
| 167 | |||
| 168 | ☞ [PDF info on my works](https://github.com/santex) | ||
| 169 | |||
| 170 | |||
| 171 | =head1 SEE ALSO | ||
| 172 | |||
| 173 | AI-MicroStructure | ||
| 174 | AI-MicroStructure-Cache | ||
| 175 | AI-MicroStructure-Deamon | ||
| 176 | AI-MicroStructure-Relations | ||
| 177 | AI-MicroStructure-Concept | ||
| 178 | AI-MicroStructure-Data | ||
| 179 | AI-MicroStructure-Driver | ||
| 180 | AI-MicroStructure-Plugin-Pdf | ||
| 181 | AI-MicroStructure-Plugin-Twitter | ||
| 182 | AI-MicroStructure-Plugin-Wiki | ||
| 183 | |||
| 184 | |||
| 185 | __END__ | ||
| 186 | |||
| 187 | __DATA__ | ||
| 188 |