⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memoize::expire.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...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" '''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 turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD.  Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\.    de IX.    tm Index:\\$1\t\\n%\t"\\$2"...    nr % 0.    rr F.\}.el \{\.    de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear.  Run.  Save yourself.  No user-serviceable parts..    \" fudge factors for nroff and troff.if n \{\.    ds #H 0.    ds #V .8m.    ds #F .3m.    ds #[ \f1.    ds #] \fP.\}.if t \{\.    ds #H ((1u-(\\\\n(.fu%2u))*.13m).    ds #V .6m.    ds #F 0.    ds #[ \&.    ds #] \&.\}.    \" simple accents for nroff and troff.if n \{\.    ds ' \&.    ds ` \&.    ds ^ \&.    ds , \&.    ds ~ ~.    ds /.\}.if t \{\.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u".    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}.    \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E.    \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'.    \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\.    ds : e.    ds 8 ss.    ds o a.    ds d- d\h'-1'\(ga.    ds D- D\h'-1'\(hy.    ds th \o'bp'.    ds Th \o'LP'.    ds ae ae.    ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Memoize::Expire 3".TH Memoize::Expire 3 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" 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"Memoize::Expire \- Plug\-in module for automatic expiration of memoized values.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 5\&  use Memoize;\&  use Memoize::Expire;\&  tie my %cache => \*(AqMemoize::Expire\*(Aq,\&                     LIFETIME => $lifetime,    # In seconds\&                     NUM_USES => $n_uses;\&\&  memoize \*(Aqfunction\*(Aq, SCALAR_CACHE => [HASH => \e%cache ];.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"Memoize::Expire is a plug-in module for Memoize.  It allows the cachedvalues for memoized functions to expire automatically.  This manualassumes you are already familiar with the Memoize module.  If not, youshould study that manual carefully first, paying particular attentionto the \s-1HASH\s0 feature..PPMemoize::Expire is a layer of software that you can insert in betweenMemoize itself and whatever underlying package implements the cache.The layer presents a hash variable whose values expire whenever theyget too old, have been used too often, or both. You tell \f(CW\*(C`Memoize\*(C'\fR touse this forgetful hash as its cache instead of the default, which isan ordinary hash..PPTo specify a real-time timeout, supply the \f(CW\*(C`LIFETIME\*(C'\fR option with anumeric value.  Cached data will expire after this many seconds, andwill be looked up afresh when it expires.  When a data item is lookedup afresh, its lifetime is reset..PPIf you specify \f(CW\*(C`NUM_USES\*(C'\fR with an argument of \fIn\fR, then each cacheddata item will be discarded and looked up afresh after the \fIn\fRth timeyou access it.  When a data item is looked up afresh, its number ofuses is reset..PPIf you specify both arguments, data will be discarded from the cachewhen either expiration condition holds..PPMemoize::Expire uses a real hash internally to store the cached data.You can use the \f(CW\*(C`HASH\*(C'\fR option to Memoize::Expire to supply a tiedhash in place of the ordinary hash that Memoize::Expire will normallyuse.  You can use this feature to add Memoize::Expire as a layer inbetween a persistent disk hash and Memoize.  If you do this, you get apersistent disk cache whose entries expire automatically.  Forexample:.PP.Vb 7\&  #   Memoize\&  #      |\&  #   Memoize::Expire  enforces data expiration policy\&  #      |\&  #   DB_File  implements persistence of data in a disk file\&  #      |\&  #   Disk file\&\&  use Memoize;\&  use Memoize::Expire;\&  use DB_File;\&\&  # Set up persistence\&  tie my %disk_cache => \*(AqDB_File\*(Aq, $filename, O_CREAT|O_RDWR, 0666];\&\&  # Set up expiration policy, supplying persistent hash as a target\&  tie my %cache => \*(AqMemoize::Expire\*(Aq, \&                     LIFETIME => $lifetime,    # In seconds\&                     NUM_USES => $n_uses,\&                     HASH => \e%disk_cache; \&\&  # Set up memoization, supplying expiring persistent hash for cache\&  memoize \*(Aqfunction\*(Aq, SCALAR_CACHE => [ HASH => \e%cache ];.Ve.SH "INTERFACE".IX Header "INTERFACE"There is nothing special about Memoize::Expire.  It is just anexample.  If you don't like the policy that it implements, you arefree to write your own expiration policy module that implementswhatever policy you desire.  Here is how to do that.  Let us supposethat your module will be named MyExpirePolicy..PPShort summary: You need to create a package that defines four methods:.IP " \s-1TIEHASH\s0" 4.IX Item " TIEHASH"Construct and return cache object..IP " \s-1EXISTS\s0" 4.IX Item " EXISTS"Given a function argument, is the corresponding function value in thecache, and if so, is it fresh enough to use?.IP " \s-1FETCH\s0" 4.IX Item " FETCH"Given a function argument, look up the corresponding function value inthe cache and return it..IP " \s-1STORE\s0" 4.IX Item " STORE"Given a function argument and the corresponding function value, storethem into the cache..IP " \s-1CLEAR\s0" 4.IX Item " CLEAR"(Optional.)  Flush the cache completely..PPThe user who wants the memoization cache to be expired according toyour policy will say so by writing.PP.Vb 2\&  tie my %cache => \*(AqMyExpirePolicy\*(Aq, args...;\&  memoize \*(Aqfunction\*(Aq, SCALAR_CACHE => [HASH => \e%cache];.Ve.PPThis will invoke \f(CW\*(C`MyExpirePolicy\->TIEHASH(args)\*(C'\fR.MyExpirePolicy::TIEHASH should do whatever is appropriate to set upthe cache, and it should return the cache object to the caller..PPFor example, MyExpirePolicy::TIEHASH might create an object thatcontains a regular Perl hash (which it will to store the cachedvalues) and some extra information about the arguments and how old thedata is and things like that.  Let us call this object `C'..PPWhen Memoize needs to check to see if an entry is in the cachealready, it will invoke \f(CW\*(C`C\->EXISTS(key)\*(C'\fR.  \f(CW\*(C`key\*(C'\fR is the normalizedfunction argument.  MyExpirePolicy::EXISTS should return 0 if the keyis not in the cache, or if it has expired, and 1 if an unexpired valueis in the cache.  It should \fInot\fR return \f(CW\*(C`undef\*(C'\fR, because there is abug in some versions of Perl that will cause a spurious \s-1FETCH\s0 if the\&\s-1EXISTS\s0 method returns \f(CW\*(C`undef\*(C'\fR..PPIf your \s-1EXISTS\s0 function returns true, Memoize will try to fetch thecached value by invoking \f(CW\*(C`C\->FETCH(key)\*(C'\fR.  MyExpirePolicy::FETCH shouldreturn the cached value.  Otherwise, Memoize will call the memoizedfunction to compute the appropriate value, and will store it into thecache by calling \f(CW\*(C`C\->STORE(key, value)\*(C'\fR..PPHere is a very brief example of a policy module that expires eachcache item after ten seconds..PP.Vb 1\&        package Memoize::TenSecondExpire;\&\&        sub TIEHASH {\&          my ($package, %args) = @_;\&          my $cache = $args{HASH} || {};\&          bless $cache => $package;\&        }\&\&        sub EXISTS {\&          my ($cache, $key) = @_;\&          if (exists $cache\->{$key} && \&              $cache\->{$key}{EXPIRE_TIME} > time) {\&            return 1\&          } else {\&            return 0;  # Do NOT return \`undef\*(Aq here.\&          }\&        }\&\&        sub FETCH {\&          my ($cache, $key) = @_;\&          return $cache\->{$key}{VALUE};\&        }\&\&        sub STORE {\&          my ($cache, $key, $newvalue) = @_;\&          $cache\->{$key}{VALUE} = $newvalue;\&          $cache\->{$key}{EXPIRE_TIME} = time + 10;\&        }.Ve.PPTo use this expiration policy, the user would say.PP.Vb 3\&        use Memoize;\&        tie my %cache10sec => \*(AqMemoize::TenSecondExpire\*(Aq;\&        memoize \*(Aqfunction\*(Aq, SCALAR_CACHE => [HASH => \e%cache10sec];.Ve.PPMemoize would then call \f(CW\*(C`function\*(C'\fR whenever a cached value wasentirely absent or was older than ten seconds..PPYou should always support a \f(CW\*(C`HASH\*(C'\fR argument to \f(CW\*(C`TIEHASH\*(C'\fR that tiesthe underlying cache so that the user can specify that the cache isalso persistent or that it has some other interesting semantics.  Theexample above demonstrates how to do this, as does \f(CW\*(C`Memoize::Expire\*(C'\fR..SH "ALTERNATIVES".IX Header "ALTERNATIVES"Brent Powers has a \f(CW\*(C`Memoize::ExpireLRU\*(C'\fR module that was designed towork with Memoize and provides expiration of least-recently-used data.The cache is held at a fixed number of entries, and when new datacomes in, the least-recently used data is expired.  See<http://search.cpan.org/search?mode=module&query=ExpireLRU>..PPJoshua Chamas's Tie::Cache module may be useful as an expirationmanager.  (If you try this, let me know how it works out.).PPIf you develop any useful expiration managers that you think should bedistributed with Memoize, please let me know..SH "CAVEATS".IX Header "CAVEATS"This module is experimental, and may contain bugs.  Please report bugsto the address below..PPNumber-of-uses is stored as a 16\-bit unsigned integer, so can't exceed65535..PPBecause of clock granularity, expiration times may occur up to onesecond sooner than you expect.  For example, suppose you store a valuewith a lifetime of ten seconds, and you store it at 12:00:00.998 on acertain day.  Memoize will look at the clock and see 12:00:00.  Then9.01 seconds later, at 12:00:10.008 you try to read it back.  Memoizewill look at the clock and see 12:00:10 and conclude that the valuehas expired.  This will probably not occur if you have\&\f(CW\*(C`Time::HiRes\*(C'\fR installed..SH "AUTHOR".IX Header "AUTHOR"Mark-Jason Dominus (mjd\-perl\-memoize+@plover.com).PPMike Cariaso provided valuable insight into the best way to solve thisproblem..SH "SEE ALSO".IX Header "SEE ALSO"\&\fIperl\fR\|(1).PPThe Memoize man page..PPhttp://www.plover.com/~mjd/perl/Memoize/  (for news and updates).PPI maintain a mailing list on which I occasionally announce newversions of Memoize.  The list is for announcements only, notdiscussion.  To join, send an empty message tomjd\-perl\-memoize\-request@Plover.com.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -