expire.html

来自「perl教程」· HTML 代码 · 共 286 行 · 第 1/2 页

HTML
286 行
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Memoize::Expire - Plug-in module for automatic expiration of memoized values</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Memoize::Expire - Plug-in module for automatic expiration of memoized values</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#interface">INTERFACE</a></li>
	<li><a href="#alternatives">ALTERNATIVES</a></li>
	<li><a href="#caveats">CAVEATS</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Memoize::Expire - Plug-in module for automatic expiration of memoized values</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
  <span class="keyword">use</span> <span class="variable">Memoize</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">Memoize::Expire</span><span class="operator">;</span>
  <span class="keyword">tie</span> <span class="keyword">my</span> <span class="variable">%cache</span> <span class="operator">=&gt;</span> <span class="string">'Memoize::Expire'</span><span class="operator">,</span>
                     <span class="string">LIFETIME</span> <span class="operator">=&gt;</span> <span class="variable">$lifetime</span><span class="operator">,</span>    <span class="comment"># In seconds</span>
                     <span class="string">NUM_USES</span> <span class="operator">=&gt;</span> <span class="variable">$n_uses</span><span class="operator">;</span>
</pre>
<pre>
  <span class="variable">memoize</span> <span class="string">'function'</span><span class="operator">,</span> <span class="string">SCALAR_CACHE</span> <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">HASH</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">%cache</span> <span class="operator">]</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Memoize::Expire is a plug-in module for Memoize.  It allows the cached
values for memoized functions to expire automatically.  This manual
assumes you are already familiar with the Memoize module.  If not, you
should study that manual carefully first, paying particular attention
to the HASH feature.</p>
<p>Memoize::Expire is a layer of software that you can insert in between
Memoize itself and whatever underlying package implements the cache.
The layer presents a hash variable whose values expire whenever they
get too old, have been used too often, or both. You tell <code>Memoize</code> to
use this forgetful hash as its cache instead of the default, which is
an ordinary hash.</p>
<p>To specify a real-time timeout, supply the <code>LIFETIME</code> option with a
numeric value.  Cached data will expire after this many seconds, and
will be looked up afresh when it expires.  When a data item is looked
up afresh, its lifetime is reset.</p>
<p>If you specify <code>NUM_USES</code> with an argument of <em>n</em>, then each cached
data item will be discarded and looked up afresh after the <em>n</em>th time
you access it.  When a data item is looked up afresh, its number of
uses is reset.</p>
<p>If you specify both arguments, data will be discarded from the cache
when either expiration condition holds.</p>
<p>Memoize::Expire uses a real hash internally to store the cached data.
You can use the <code>HASH</code> option to Memoize::Expire to supply a tied
hash in place of the ordinary hash that Memoize::Expire will normally
use.  You can use this feature to add Memoize::Expire as a layer in
between a persistent disk hash and Memoize.  If you do this, you get a
persistent disk cache whose entries expire automatically.  For
example:</p>
<pre>
  <span class="comment">#   Memoize</span>
  <span class="comment">#      |</span>
  <span class="comment">#   Memoize::Expire  enforces data expiration policy</span>
  <span class="comment">#      |</span>
  <span class="comment">#   DB_File  implements persistence of data in a disk file</span>
  <span class="comment">#      |</span>
  <span class="comment">#   Disk file</span>
</pre>
<pre>
  <span class="keyword">use</span> <span class="variable">Memoize</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">Memoize::Expire</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">DB_File</span><span class="operator">;</span>
</pre>
<pre>
  <span class="comment"># Set up persistence</span>
  <span class="keyword">tie</span> <span class="keyword">my</span> <span class="variable">%disk_cache</span> <span class="operator">=&gt;</span> <span class="string">'DB_File'</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">,</span> <span class="variable">O_CREAT</span><span class="operator">|</span><span class="variable">O_RDWR</span><span class="operator">,</span> <span class="number">0666</span><span class="operator">];</span>
</pre>
<pre>
  <span class="comment"># Set up expiration policy, supplying persistent hash as a target</span>
  <span class="keyword">tie</span> <span class="keyword">my</span> <span class="variable">%cache</span> <span class="operator">=&gt;</span> <span class="string">'Memoize::Expire'</span><span class="operator">,</span> 
                     <span class="string">LIFETIME</span> <span class="operator">=&gt;</span> <span class="variable">$lifetime</span><span class="operator">,</span>    <span class="comment"># In seconds</span>
                     <span class="string">NUM_USES</span> <span class="operator">=&gt;</span> <span class="variable">$n_uses</span><span class="operator">,</span>
                     <span class="string">HASH</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">%disk_cache</span><span class="operator">;</span>
</pre>
<pre>
  <span class="comment"># Set up memoization, supplying expiring persistent hash for cache</span>
  <span class="variable">memoize</span> <span class="string">'function'</span><span class="operator">,</span> <span class="string">SCALAR_CACHE</span> <span class="operator">=&gt;</span> <span class="operator">[</span> <span class="string">HASH</span> <span class="operator">=&gt;</span> <span class="operator">\</span><span class="variable">%cache</span> <span class="operator">]</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="interface">INTERFACE</a></h1>
<p>There is nothing special about Memoize::Expire.  It is just an
example.  If you don't like the policy that it implements, you are
free to write your own expiration policy module that implements
whatever policy you desire.  Here is how to do that.  Let us suppose
that your module will be named MyExpirePolicy.</p>
<p>Short summary: You need to create a package that defines four methods:</p>
<dl>
<dt><strong><a name="item_tiehash">TIEHASH</a></strong>

<dd>
<p>Construct and return cache object.</p>
</dd>
</li>
<dt><strong><a name="item_exists">EXISTS</a></strong>

<dd>
<p>Given a function argument, is the corresponding function value in the
cache, and if so, is it fresh enough to use?</p>
</dd>
</li>
<dt><strong><a name="item_fetch">FETCH</a></strong>

<dd>

⌨️ 快捷键说明

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