.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Historical 3pm" .TH Historical 3pm "2018-06-21" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Cache::Historical \- Cache historical values .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Cache::Historical; \& \& my $cache = Cache::Historical\->new(); \& \& # Set a key\*(Aqs value on a specific date \& $cache\->set( $dt, $key, $value ); \& \& # Get a key\*(Aqs value on a specific date \& my $value = $cache\->get( $dt, $key ); \& \& # Same as \*(Aqget\*(Aq, but if we don\*(Aqt have a value at $dt, but we \& # do have values for dates < $dt, return the previous \& # historic value. \& $cache\->get_interpolated( $dt, $key ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Cache::Historical caches historical values by key and date. If you have something like historical stock quotes, for example .PP .Vb 4 \& 2008\-01\-02 msft 35.22 \& 2008\-01\-03 msft 35.37 \& 2008\-01\-04 msft 34.38 \& 2008\-01\-07 msft 34.61 .Ve .PP then you can store them in Cache::Historical like .PP .Vb 1 \& my $cache = Cache::Historical\->new(); \& \& my $fmt = DateTime::Format::Strptime\->new( \& pattern => "%Y\-%m\-%d"); \& \& $cache\->set( $fmt\->parse_datetime("2008\-01\-02"), "msft", 35.22 ); \& $cache\->set( $fmt\->parse_datetime("2008\-01\-03"), "msft", 35.37 ); \& $cache\->set( $fmt\->parse_datetime("2008\-01\-04"), "msft", 34.38 ); \& $cache\->set( $fmt\->parse_datetime("2008\-01\-07"), "msft", 34.61 ); .Ve .PP and retrieve them later by date: .PP .Vb 1 \& my $dt = $fmt\->parse_datetime("2008\-01\-03"); \& \& # Returns 35.37 \& my $value = $cache\->get( $dt, "msft" ); .Ve .PP Even if there's no value available for a given date, but there are historical values that predate the requested date, \f(CW\*(C`get_interpolated()\*(C'\fR will return the next best historical value: .PP .Vb 1 \& my $dt = $fmt\->parse_datetime("2008\-01\-06"); \& \& # Returns undef, no value available for 2008\-01\-06 \& my $value = $cache\->get( $dt, "msft" ); \& \& # Returns 34.48, the value for 2008\-01\-04, instead. \& $value = $cache\->get_interpolated( $dt, "msft" ); .Ve .SS "Methods" .IX Subsection "Methods" .IP "\fInew()\fR" 4 .IX Item "new()" Creates the object. Takes the SQLite file to put the date into as an additional parameter: .Sp .Vb 3 \& my $cache = Cache::Historical\->new( \& sqlite_file => "/tmp/mydata.dat", \& ); .Ve .Sp The SQLite file defaults to .Sp .Vb 1 \& $HOME/.cache\-historical/cache\-historical.dat .Ve .Sp so if you have multiple caches, you need to use different SQLite files. .IP "\fItime_range()\fR" 4 .IX Item "time_range()" .Vb 2 \& # List the time range for which we have values for $key \& my($from, $to) = $cache\->time_range( $key ); .Ve .IP "\fIkeys()\fR" 4 .IX Item "keys()" .Vb 2 \& # List all keys \& my @keys = $cache\->keys(); .Ve .IP "\fIvalues()\fR" 4 .IX Item "values()" .Vb 3 \& # List all the values we have for $key, sorted by date \& # ([$dt, $value], [$dt, $value], ...) \& my @results = $cache\->values( $key ); .Ve .IP "\fIclear()\fR" 4 .IX Item "clear()" .Vb 2 \& # Remove all values for a specific key \& $cache\->clear( $key ); \& \& # Clear the entire cache \& $cache\->clear(); .Ve .IP "\fIlast_update()\fR" 4 .IX Item "last_update()" .Vb 2 \& # Return a DateTime object of the last update of a given key \& my $when = $cache\->last_update( $key ); .Ve .IP "\fIsince_last_update()\fR" 4 .IX Item "since_last_update()" .Vb 3 \& # Return a DateTime::Duration object since the time of the last \& # update of a given key. \& my $since = $cache\->since_last_update( $key ); .Ve .SH "LEGALESE" .IX Header "LEGALESE" Copyright 2007\-2011 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. .SH "AUTHOR" .IX Header "AUTHOR" 2007, Mike Schilli