596 lines
63 KiB
HTML
596 lines
63 KiB
HTML
|
|
|||
|
|
|||
|
<!DOCTYPE html>
|
|||
|
<html class="writer-html4" lang="en" >
|
|||
|
<head>
|
|||
|
<meta charset="utf-8">
|
|||
|
|
|||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|||
|
|
|||
|
<title>Welcome to cachings’s documentation! — caching documentation</title>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!--[if lt IE 9]>
|
|||
|
<script src="_static/js/html5shiv.min.js"></script>
|
|||
|
<![endif]-->
|
|||
|
|
|||
|
|
|||
|
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
|
|||
|
<script type="text/javascript" src="_static/jquery.js"></script>
|
|||
|
<script type="text/javascript" src="_static/underscore.js"></script>
|
|||
|
<script type="text/javascript" src="_static/doctools.js"></script>
|
|||
|
<script type="text/javascript" src="_static/language_data.js"></script>
|
|||
|
|
|||
|
<script type="text/javascript" src="_static/js/theme.js"></script>
|
|||
|
|
|||
|
|
|||
|
<link rel="index" title="Index" href="genindex.html" />
|
|||
|
<link rel="search" title="Search" href="search.html" />
|
|||
|
</head>
|
|||
|
|
|||
|
<body class="wy-body-for-nav">
|
|||
|
|
|||
|
|
|||
|
<div class="wy-grid-for-nav">
|
|||
|
|
|||
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|||
|
<div class="wy-side-scroll">
|
|||
|
<div class="wy-side-nav-search" >
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<a href="#" class="icon icon-home" alt="Documentation Home"> caching
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</a>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div role="search">
|
|||
|
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
|||
|
<input type="text" name="q" placeholder="Search docs" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<!-- Local TOC -->
|
|||
|
<div class="local-toc"><ul>
|
|||
|
<li><a class="reference internal" href="#">Welcome to cachings’s documentation!</a><ul>
|
|||
|
<li><a class="reference internal" href="#caching-caching-module">caching (Caching Module)</a></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
</div>
|
|||
|
</nav>
|
|||
|
|
|||
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|||
|
|
|||
|
|
|||
|
<nav class="wy-nav-top" aria-label="top navigation">
|
|||
|
|
|||
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|||
|
<a href="#">caching</a>
|
|||
|
|
|||
|
</nav>
|
|||
|
|
|||
|
|
|||
|
<div class="wy-nav-content">
|
|||
|
|
|||
|
<div class="rst-content">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|||
|
|
|||
|
<ul class="wy-breadcrumbs">
|
|||
|
|
|||
|
<li><a href="#" class="icon icon-home"></a> »</li>
|
|||
|
|
|||
|
<li>Welcome to cachings’s documentation!</li>
|
|||
|
|
|||
|
|
|||
|
<li class="wy-breadcrumbs-aside">
|
|||
|
|
|||
|
|
|||
|
<a href="_sources/index.rst.txt" rel="nofollow"> View page source</a>
|
|||
|
|
|||
|
|
|||
|
</li>
|
|||
|
|
|||
|
</ul>
|
|||
|
|
|||
|
|
|||
|
<hr/>
|
|||
|
</div>
|
|||
|
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
|||
|
<div itemprop="articleBody">
|
|||
|
|
|||
|
<div class="section" id="module-caching">
|
|||
|
<span id="welcome-to-cachings-s-documentation"></span><h1>Welcome to cachings’s documentation!<a class="headerlink" href="#module-caching" title="Permalink to this headline">¶</a></h1>
|
|||
|
<div class="section" id="caching-caching-module">
|
|||
|
<h2>caching (Caching Module)<a class="headerlink" href="#caching-caching-module" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p><strong>Author:</strong></p>
|
|||
|
<ul class="simple">
|
|||
|
<li>Dirk Alders <<a class="reference external" href="mailto:sudo-dirk%40mount-mockery.de">sudo-dirk<span>@</span>mount-mockery<span>.</span>de</a>></li>
|
|||
|
</ul>
|
|||
|
<p><strong>Description:</strong></p>
|
|||
|
<blockquote>
|
|||
|
<div>This Module supports functions and classes for caching e.g. properties of other instances.</div></blockquote>
|
|||
|
<p><strong>Submodules:</strong></p>
|
|||
|
<ul class="simple">
|
|||
|
<li><a class="reference internal" href="#caching.property_cache_json" title="caching.property_cache_json"><code class="xref py py-class docutils literal notranslate"><span class="pre">caching.property_cache_json</span></code></a></li>
|
|||
|
<li><a class="reference internal" href="#caching.property_cache_pickle" title="caching.property_cache_pickle"><code class="xref py py-class docutils literal notranslate"><span class="pre">caching.property_cache_pickle</span></code></a></li>
|
|||
|
</ul>
|
|||
|
<p><strong>Unittest:</strong></p>
|
|||
|
<blockquote>
|
|||
|
<div>See also the <a class="reference download internal" download="" href="_downloads/f5f51665bfc67c10ccc039770b738067/unittest.pdf"><code class="xref download docutils literal notranslate"><span class="pre">unittest</span></code></a> documentation.</div></blockquote>
|
|||
|
<dl class="class">
|
|||
|
<dt id="caching.property_cache_json">
|
|||
|
<em class="property">class </em><code class="descclassname">caching.</code><code class="descname">property_cache_json</code><span class="sig-paren">(</span><em>source_instance</em>, <em>cache_filename</em>, <em>load_all_on_init=False</em>, <em>callback_on_data_storage=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_json" title="Permalink to this definition">¶</a></dt>
|
|||
|
<dd><p>Class to cache properties, which take longer on initialising than reading a file in json format. See also parent <a class="reference internal" href="#caching.property_cache_pickle" title="caching.property_cache_pickle"><code class="xref py py-class docutils literal notranslate"><span class="pre">property_cache_pickle</span></code></a></p>
|
|||
|
<table class="docutils field-list" frame="void" rules="none">
|
|||
|
<col class="field-name" />
|
|||
|
<col class="field-body" />
|
|||
|
<tbody valign="top">
|
|||
|
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|||
|
<li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
|
|||
|
<li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
|
|||
|
<li><strong>load_all_on_init</strong> – Optionally init behaviour control parameter. True will load all available properties from source on init, False not.</li>
|
|||
|
</ul>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<div class="admonition warning">
|
|||
|
<p class="first admonition-title">Warning</p>
|
|||
|
<ul class="last simple">
|
|||
|
<li>This class uses json. You should <strong>only</strong> use keys of type string!</li>
|
|||
|
<li>Unicode types are transfered to strings</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="admonition note">
|
|||
|
<p class="first admonition-title">Note</p>
|
|||
|
<p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
|
|||
|
<ul class="last simple">
|
|||
|
<li>uid(): returns the unique id of the source.</li>
|
|||
|
<li>keys(): returns a list of all available keys.</li>
|
|||
|
<li>data_version(): returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).</li>
|
|||
|
<li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<p>Reasons for updating the complete data set:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li>UID of source_instance has changed (in comparison to the cached value).</li>
|
|||
|
<li>data_version is increased</li>
|
|||
|
</ul>
|
|||
|
<p><strong>Example:</strong></p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
|
|||
|
<span class="c1"># -*- coding: UTF-8 -*-</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|||
|
<span class="kn">import</span> <span class="nn">time</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'../..'</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">caching</span>
|
|||
|
<span class="kn">import</span> <span class="nn">report</span>
|
|||
|
|
|||
|
|
|||
|
<span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">'root'</span><span class="p">,</span> <span class="s1">'DEBUG'</span><span class="p">),</span> <span class="p">])</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">class</span> <span class="nc">test_slow_data</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|||
|
<span class="n">_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
|
|||
|
<span class="n">_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
|
|||
|
<span class="n">_THREE</span> <span class="o">=</span> <span class="s1">'_property_cache_data_version_'</span>
|
|||
|
<span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">'_property_cache_uid_'</span>
|
|||
|
<span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">'__property_cache_uid_'</span>
|
|||
|
<span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">_ONE</span><span class="p">,</span> <span class="n">_TWO</span><span class="p">,</span> <span class="n">_THREE</span><span class="p">,</span> <span class="n">_FOUR</span><span class="p">,</span> <span class="n">_FIVE</span><span class="p">]</span>
|
|||
|
<span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
|||
|
<span class="k">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
|
|||
|
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'one'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'two'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'three'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'four'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'five'</span>
|
|||
|
<span class="k">return</span> <span class="n">default</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="kc">None</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">class</span> <span class="nc">tsd_cache_json</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">):</span>
|
|||
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|||
|
<span class="n">test_slow_data</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span> <span class="o">=</span> <span class="n">caching</span><span class="o">.</span><span class="n">property_cache_json</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="s1">'cache.json'</span><span class="p">,</span> <span class="n">load_all_on_init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="n">test_slow_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
|
|||
|
|
|||
|
|
|||
|
<span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_json</span><span class="p">()</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Testing property_cache (json):</span><span class="se">\\</span><span class="s1">n--------------------------------'</span><span class="p">)</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">one</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">two</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">three</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">four</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">five</span><span class="p">())</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Will result on the first execution to the following output (with a long execution time):</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">json</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">892</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Cache</span> <span class="n">file</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">exists</span> <span class="p">(</span><span class="n">yet</span><span class="p">)</span><span class="o">.</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">893</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">1</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">29</span><span class="p">,</span><span class="mi">896</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">29</span><span class="p">,</span><span class="mi">897</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="n">one</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
|
|||
|
<span class="n">two</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_data_version_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">35</span><span class="p">,</span><span class="mi">904</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">35</span><span class="p">,</span><span class="mi">904</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="n">three</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_uid_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">38</span><span class="p">,</span><span class="mi">905</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">38</span><span class="p">,</span><span class="mi">906</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="n">four</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__property_cache_uid_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">41</span><span class="p">,</span><span class="mi">908</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">41</span><span class="p">,</span><span class="mi">910</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="n">five</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">json</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">014</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="n">properties</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">json</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">42</span><span class="p">,</span><span class="mi">015</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|||
|
<span class="n">one</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
|
|||
|
<span class="n">two</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">016</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|||
|
<span class="n">three</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">016</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|||
|
<span class="n">four</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">45</span><span class="p">,</span><span class="mi">017</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">JsonCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|||
|
<span class="n">five</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</dd></dl>
|
|||
|
|
|||
|
<dl class="class">
|
|||
|
<dt id="caching.property_cache_pickle">
|
|||
|
<em class="property">class </em><code class="descclassname">caching.</code><code class="descname">property_cache_pickle</code><span class="sig-paren">(</span><em>source_instance</em>, <em>cache_filename</em>, <em>load_all_on_init=False</em>, <em>callback_on_data_storage=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle" title="Permalink to this definition">¶</a></dt>
|
|||
|
<dd><p>Class to cache properties, which take longer on initialising than reading a file in pickle format.</p>
|
|||
|
<table class="docutils field-list" frame="void" rules="none">
|
|||
|
<col class="field-name" />
|
|||
|
<col class="field-body" />
|
|||
|
<tbody valign="top">
|
|||
|
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
|
|||
|
<li><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</li>
|
|||
|
<li><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</li>
|
|||
|
<li><strong>load_all_on_init</strong> – Optionally init behaviour control parameter. True will load all available properties from source on init, False not.</li>
|
|||
|
</ul>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<div class="admonition note">
|
|||
|
<p class="first admonition-title">Note</p>
|
|||
|
<p>source_instance needs to have at least the following methods: uid(), keys(), data_version(), get()</p>
|
|||
|
<ul class="last simple">
|
|||
|
<li>uid(): returns the unique id of the source.</li>
|
|||
|
<li>keys(): returns a list of all available keys.</li>
|
|||
|
<li>data_version(): returns a version number of the current data (it should be increased, if the get method of the source instance returns improved values or the data structure had been changed).</li>
|
|||
|
<li>get(key, default): returns the property for a key. If key does not exists, default will be returned.</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<p>Reasons for updating the complete data set:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li>UID of source_instance has changed (in comparison to the cached value).</li>
|
|||
|
<li>data_version is increased</li>
|
|||
|
</ul>
|
|||
|
<p><strong>Example:</strong></p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
|
|||
|
<span class="c1"># -*- coding: UTF-8 -*-</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|||
|
<span class="kn">import</span> <span class="nn">time</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'../..'</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="kn">import</span> <span class="nn">caching</span>
|
|||
|
<span class="kn">import</span> <span class="nn">report</span>
|
|||
|
|
|||
|
|
|||
|
<span class="n">report</span><span class="o">.</span><span class="n">stdoutLoggingConfigure</span><span class="p">(</span><span class="n">log_name_lvl</span><span class="o">=</span><span class="p">[(</span><span class="s1">'root'</span><span class="p">,</span> <span class="s1">'DEBUG'</span><span class="p">),</span> <span class="p">])</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">class</span> <span class="nc">test_slow_data</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
|
|||
|
<span class="n">_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
|
|||
|
<span class="n">_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
|
|||
|
<span class="n">_THREE</span> <span class="o">=</span> <span class="s1">'_property_cache_data_version_'</span>
|
|||
|
<span class="n">_FOUR</span> <span class="o">=</span> <span class="s1">'_property_cache_uid_'</span>
|
|||
|
<span class="n">_FIVE</span> <span class="o">=</span> <span class="s1">'__property_cache_uid_'</span>
|
|||
|
<span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">_ONE</span><span class="p">,</span> <span class="n">_TWO</span><span class="p">,</span> <span class="n">_THREE</span><span class="p">,</span> <span class="n">_FOUR</span><span class="p">,</span> <span class="n">_FIVE</span><span class="p">]</span>
|
|||
|
<span class="n">VERS</span> <span class="o">=</span> <span class="mf">0.1</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">data_version</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">VERS</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">three</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">four</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">five</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
|||
|
<span class="k">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="n">k</span><span class="p">):</span>
|
|||
|
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">'slow get executed for </span><span class="si">%s</span><span class="se">\n</span><span class="s1">'</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
|
|||
|
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ONE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'one'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'two'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_THREE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'three'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FOUR</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'four'</span>
|
|||
|
<span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_FIVE</span><span class="p">:</span>
|
|||
|
<span class="n">print_n_sleep</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="k">return</span> <span class="s1">'five'</span>
|
|||
|
<span class="k">return</span> <span class="n">default</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">KEYS</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">uid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="kc">None</span>
|
|||
|
|
|||
|
|
|||
|
<span class="k">class</span> <span class="nc">tsd_cache_pickle</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">):</span>
|
|||
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|||
|
<span class="n">test_slow_data</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span> <span class="o">=</span> <span class="n">caching</span><span class="o">.</span><span class="n">property_cache_pickle</span><span class="p">(</span><span class="n">test_slow_data</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="s1">'cache.pickle'</span><span class="p">,</span> <span class="n">load_all_on_init</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="n">test_slow_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_TWO</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cached_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">default</span><span class="p">)</span>
|
|||
|
|
|||
|
|
|||
|
<span class="n">data</span> <span class="o">=</span> <span class="n">tsd_cache_pickle</span><span class="p">()</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Testing property_cache (pickle):</span><span class="se">\\</span><span class="s1">n--------------------------------'</span><span class="p">)</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">one</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">two</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">three</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">four</span><span class="p">())</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">five</span><span class="p">())</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Will result on the first execution to the following output (with a long execution time):</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">pickle</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">08</span><span class="p">,</span><span class="mi">749</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Cache</span> <span class="n">file</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">exists</span> <span class="p">(</span><span class="n">yet</span><span class="p">)</span><span class="o">.</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">08</span><span class="p">,</span><span class="mi">749</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">1</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">11</span><span class="p">,</span><span class="mi">752</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">11</span><span class="p">,</span><span class="mi">753</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="n">one</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
|
|||
|
<span class="n">two</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_data_version_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">17</span><span class="p">,</span><span class="mi">760</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">17</span><span class="p">,</span><span class="mi">760</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="n">three</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">_property_cache_uid_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">20</span><span class="p">,</span><span class="mi">764</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">20</span><span class="p">,</span><span class="mi">764</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="n">four</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__property_cache_uid_</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">768</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">768</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">cache</span><span class="o">-</span><span class="n">file</span> <span class="n">stored</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="n">five</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>With every following execution (slow for getting “two” which is not cached - see implementation):</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Testing</span> <span class="n">property_cache</span> <span class="p">(</span><span class="n">pickle</span><span class="p">):</span>\<span class="n">n</span><span class="o">--------------------------------</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">828</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">INFO</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Loading</span> <span class="n">properties</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="n">cache</span><span class="o">.</span><span class="n">pickle</span><span class="p">)</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">23</span><span class="p">,</span><span class="mi">828</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'one'</span><span class="p">)</span>
|
|||
|
<span class="n">one</span>
|
|||
|
<span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="mi">2</span>
|
|||
|
<span class="n">two</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">828</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_data_version_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'three'</span><span class="p">)</span>
|
|||
|
<span class="n">three</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">829</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'_property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'four'</span><span class="p">)</span>
|
|||
|
<span class="n">four</span>
|
|||
|
<span class="mi">2021</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">07</span> <span class="mi">21</span><span class="p">:</span><span class="mi">58</span><span class="p">:</span><span class="mi">26</span><span class="p">,</span><span class="mi">829</span><span class="p">:</span> <span class="n">root</span><span class="o">.</span><span class="n">caching</span> <span class="o">-</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">PickCache</span><span class="p">:</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'__property_cache_uid_'</span> <span class="kn">from</span> <span class="nn">cache</span> <span class="p">(</span><span class="s1">'five'</span><span class="p">)</span>
|
|||
|
<span class="n">five</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<dl class="method">
|
|||
|
<dt id="caching.property_cache_pickle.get">
|
|||
|
<code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.get" title="Permalink to this definition">¶</a></dt>
|
|||
|
<dd><p>Method to get the cached property. If key does not exists in cache, the property will be loaded from source_instance and stored in cache (file).</p>
|
|||
|
<table class="docutils field-list" frame="void" rules="none">
|
|||
|
<col class="field-name" />
|
|||
|
<col class="field-body" />
|
|||
|
<tbody valign="top">
|
|||
|
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
|
|||
|
<li><strong>key</strong> – key for value to get.</li>
|
|||
|
<li><strong>default</strong> – value to be returned, if key does not exists.</li>
|
|||
|
</ul>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">value for a given key or default value.</p>
|
|||
|
</td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</dd></dl>
|
|||
|
|
|||
|
<dl class="method">
|
|||
|
<dt id="caching.property_cache_pickle.keys">
|
|||
|
<code class="descname">keys</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.keys" title="Permalink to this definition">¶</a></dt>
|
|||
|
<dd><p>Method to get the available keys (from <code class="xref py py-data docutils literal notranslate"><span class="pre">source_instance</span></code>).</p>
|
|||
|
</dd></dl>
|
|||
|
|
|||
|
</dd></dl>
|
|||
|
|
|||
|
</div>
|
|||
|
<div class="toctree-wrapper compound">
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="indices-and-tables">
|
|||
|
<h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Permalink to this headline">¶</a></h1>
|
|||
|
<ul class="simple">
|
|||
|
<li><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></li>
|
|||
|
<li><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></li>
|
|||
|
<li><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
</div>
|
|||
|
<footer>
|
|||
|
|
|||
|
|
|||
|
<hr/>
|
|||
|
|
|||
|
<div role="contentinfo">
|
|||
|
<p>
|
|||
|
|
|||
|
© Copyright 2021, Dirk Alders
|
|||
|
|
|||
|
</p>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
|
|||
|
|
|||
|
<a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
|
|||
|
|
|||
|
provided by <a href="https://readthedocs.org">Read the Docs</a>.
|
|||
|
|
|||
|
</footer>
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
</section>
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
<script type="text/javascript">
|
|||
|
jQuery(function () {
|
|||
|
SphinxRtdTheme.Navigation.enable(true);
|
|||
|
});
|
|||
|
</script>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|