123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646 |
- <!DOCTYPE html>
-
- <html lang="en" data-content_root="./">
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
-
- <title>Welcome to cachings’s documentation! — caching documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
-
- <link rel="stylesheet" type="text/css" href="_static/pygments.css?v=80d5e7a1" />
- <link rel="stylesheet" type="text/css" href="_static/pydoctheme.css?v=41b4f12d" />
- <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="_static/pygments_dark.css?v=b20cc3f5" />
-
- <script src="_static/documentation_options.js?v=5929fcd5"></script>
- <script src="_static/doctools.js?v=9a2dae69"></script>
- <script src="_static/sphinx_highlight.js?v=dc90522c"></script>
-
- <link rel="index" title="Index" href="genindex.html" />
- <link rel="search" title="Search" href="search.html" /><link rel="stylesheet" href="_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
- <link rel="shortcut icon" type="image/png" href="_static/py.svg" />
- <script type="text/javascript" src="_static/copybutton.js"></script>
- <script type="text/javascript" src="_static/menu.js"></script>
- <script type="text/javascript" src="_static/search-focus.js"></script>
- <script type="text/javascript" src="_static/themetoggle.js"></script>
- </head>
- <body>
- <div class="mobile-nav">
- <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
- aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
- <nav class="nav-content" role="navigation">
- <label for="menuToggler" class="toggler__label">
- <span></span>
- </label>
- <span class="nav-items-wrapper">
- <a href="https://www.python.org/" class="nav-logo">
- <img src="_static/py.svg" alt="Python logo"/>
- </a>
- <span class="version_switcher_placeholder"></span>
- <form role="search" class="search" action="search.html" method="get">
- <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
- <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
- </svg>
- <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
- <input type="submit" value="Go"/>
- </form>
- </span>
- </nav>
- <div class="menu-wrapper">
- <nav class="menu" role="navigation" aria-label="main navigation">
- <div class="language_switcher_placeholder"></div>
-
- <label class="theme-selector-label">
- Theme
- <select class="theme-selector" oninput="activateTheme(this.value)">
- <option value="auto" selected>Auto</option>
- <option value="light">Light</option>
- <option value="dark">Dark</option>
- </select>
- </label>
- <div>
- <h3><a href="#">Table of Contents</a></h3>
- <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>
- <li><a class="reference internal" href="#caching.property_cache_json"><code class="docutils literal notranslate"><span class="pre">property_cache_json</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle</span></code></a><ul>
- <li><a class="reference internal" href="#caching.property_cache_pickle.add_source_get_keys"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.add_source_get_keys()</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle.full_update"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.full_update()</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle.get"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.get()</span></code></a></li>
- </ul>
- </li>
- </ul>
- </li>
- <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
- </ul>
-
- </div>
- <div role="note" aria-label="source link">
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="_sources/index.rst.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
- </div>
- <search id="searchbox" style="display: none" role="search">
- <h3 id="searchlabel">Quick search</h3>
- <div class="searchformwrapper">
- <form class="search" action="search.html" method="get">
- <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
- <input type="submit" value="Go" />
- </form>
- </div>
- </search>
- <script>document.getElementById('searchbox').style.display = "block"</script>
- </nav>
- </div>
- </div>
-
- <div class="related" role="navigation" aria-label="Related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><img src="_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
- <li><a href="https://www.python.org/">Python</a> »</li>
- <li class="switchers">
- <div class="language_switcher_placeholder"></div>
- <div class="version_switcher_placeholder"></div>
- </li>
- <li>
-
- <a href="#">caching documentation</a> »
-
- </li>
- <li class="nav-item nav-item-this"><a href="">Welcome to cachings’s documentation!</a></li>
- <li class="right">
-
-
- <div class="inline-search" role="search">
- <form class="inline-search" action="search.html" method="get">
- <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
- <input type="submit" value="Go" />
- </form>
- </div>
- |
- </li>
- <li class="right">
- <label class="theme-selector-label">
- Theme
- <select class="theme-selector" oninput="activateTheme(this.value)">
- <option value="auto" selected>Auto</option>
- <option value="light">Light</option>
- <option value="dark">Dark</option>
- </select>
- </label> |</li>
-
- </ul>
- </div>
-
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <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="Link to this heading">¶</a></h1>
- <section id="caching-caching-module">
- <h2>caching (Caching Module)<a class="headerlink" href="#caching-caching-module" title="Link to this heading">¶</a></h2>
- <p><strong>Author:</strong></p>
- <ul class="simple">
- <li><p>Dirk Alders <<a class="reference external" href="mailto:sudo-dirk%40mount-mockery.de">sudo-dirk<span>@</span>mount-mockery<span>.</span>de</a>></p></li>
- </ul>
- <p><strong>Description:</strong></p>
- <blockquote>
- <div><p>This Module supports functions and classes for caching e.g. properties of other instances.</p>
- </div></blockquote>
- <p><strong>Submodules:</strong></p>
- <ul class="simple">
- <li><p><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></p></li>
- <li><p><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></p></li>
- </ul>
- <p><strong>Unittest:</strong></p>
- <blockquote>
- <div><p>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.</p>
- </div></blockquote>
- </section>
- <dl class="py class">
- <dt class="sig sig-object py" id="caching.property_cache_json">
- <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">caching.</span></span><span class="sig-name descname"><span class="pre">property_cache_json</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source_instance</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cache_filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">load_all_on_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback_on_data_storage</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_age</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">store_on_get</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_json" title="Link to this definition">¶</a></dt>
- <dd><p>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> for detailed information.</p>
- <div class="admonition important">
- <p class="admonition-title">Important</p>
- <ul class="simple">
- <li><p>This class uses json. You should <strong>only</strong> use keys of type string!</p></li>
- <li><p>Unicode types are transfered to strings</p></li>
- </ul>
- <p>See limitations of json.</p>
- </div>
- <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">caching</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">import</span> <span class="nn">sys</span>
- <span class="kn">import</span> <span class="nn">time</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">DATA_VERSION</span> <span class="o">=</span> <span class="mf">0.1</span>
- <span class="n">KEY_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
- <span class="n">KEY_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
- <span class="n">KEY_THREE</span> <span class="o">=</span> <span class="s1">'three'</span>
- <span class="n">KEY_FOUR</span> <span class="o">=</span> <span class="s1">'four'</span>
- <span class="n">KEY_FIVE</span> <span class="o">=</span> <span class="s1">'five'</span>
- <span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">KEY_ONE</span><span class="p">,</span> <span class="n">KEY_TWO</span><span class="p">,</span> <span class="n">KEY_THREE</span><span class="p">,</span> <span class="n">KEY_FOUR</span><span class="p">,</span> <span class="n">KEY_FIVE</span><span class="p">]</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">DATA_VERSION</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">try</span><span class="p">:</span>
- <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="sa">f</span><span class="s1">'__</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s1">__'</span><span class="p">)()</span>
- <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</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">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="bp">self</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">def</span> <span class="nf">__1__</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">print_n_sleep</span><span class="p">(</span><span class="s2">"__1__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'one'</span>
-
- <span class="k">def</span> <span class="nf">__2__</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">print_n_sleep</span><span class="p">(</span><span class="s2">"__2__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'two'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__three__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'three'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__four__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'four'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__five__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'five'</span>
-
-
- <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
- <span class="c1"># Logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span>
- <span class="nb">format</span><span class="o">=</span><span class="s2">"</span><span class="si">%(asctime)s</span><span class="s2">: </span><span class="si">%(levelname)8s</span><span class="s2"> - </span><span class="si">%(name)s</span><span class="s2"> - </span><span class="si">%(message)s</span><span class="s2">"</span><span class="p">,</span>
- <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">,</span>
- <span class="n">stream</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
- <span class="p">)</span>
- <span class="c1"># Example</span>
- <span class="n">tm</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
- <span class="n">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="s1">'cache.json'</span><span class="p">)</span>
- <span class="n">data</span><span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">KEY_THREE</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">\n</span><span class="s1">--------------------------------'</span><span class="p">)</span>
- <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">keys</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">get</span><span class="p">(</span><span class="n">key</span><span class="p">))</span>
- <span class="nb">print</span><span class="p">(</span><span class="s1">'--------------------------------</span><span class="se">\n</span><span class="s1">The execution time was </span><span class="si">%.1f</span><span class="s1">s'</span> <span class="o">%</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">tm</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="o">--------------------------------</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">19</span><span class="p">,</span><span class="mi">816</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">19</span><span class="p">,</span><span class="mi">817</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">19</span><span class="p">,</span><span class="mi">817</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__1__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">22</span><span class="p">,</span><span class="mi">817</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">one</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928902</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">22</span><span class="p">,</span><span class="mi">818</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">22</span><span class="p">,</span><span class="mi">819</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'2'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__2__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">25</span><span class="p">,</span><span class="mi">819</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">two</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928905</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">25</span><span class="p">,</span><span class="mi">820</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">two</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">25</span><span class="p">,</span><span class="mi">821</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Key</span> <span class="s1">'three'</span> <span class="ow">is</span> <span class="n">excluded</span> <span class="n">by</span> <span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">()</span><span class="o">.</span> <span class="n">Uncached</span> <span class="n">data</span> <span class="n">will</span> <span class="n">be</span> <span class="n">returned</span><span class="o">.</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__three__</span>
- <span class="n">three</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">28</span><span class="p">,</span><span class="mi">821</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'four'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__four__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">31</span><span class="p">,</span><span class="mi">822</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="n">four</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">four</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928911</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">31</span><span class="p">,</span><span class="mi">822</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">31</span><span class="p">,</span><span class="mi">823</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'five'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__five__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">824</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="n">five</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">five</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928914</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">824</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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>
- <span class="o">--------------------------------</span>
- <span class="n">The</span> <span class="n">execution</span> <span class="n">time</span> <span class="n">was</span> <span class="mf">15.0</span><span class="n">s</span>
- </pre></div>
- </div>
- <p>With every following execution the time cosumption my by much smaller:</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="o">--------------------------------</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">865</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">865</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="n">one</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">865</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'2'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">two</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">34</span><span class="p">,</span><span class="mi">865</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Key</span> <span class="s1">'three'</span> <span class="ow">is</span> <span class="n">excluded</span> <span class="n">by</span> <span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">()</span><span class="o">.</span> <span class="n">Uncached</span> <span class="n">data</span> <span class="n">will</span> <span class="n">be</span> <span class="n">returned</span><span class="o">.</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__three__</span>
- <span class="n">three</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">37</span><span class="p">,</span><span class="mi">865</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'four'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">four</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">37</span><span class="p">,</span><span class="mi">866</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'five'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">five</span>
- <span class="o">--------------------------------</span>
- <span class="n">The</span> <span class="n">execution</span> <span class="n">time</span> <span class="n">was</span> <span class="mf">3.0</span><span class="n">s</span>
- </pre></div>
- </div>
- </dd></dl>
-
- <dl class="py class">
- <dt class="sig sig-object py" id="caching.property_cache_pickle">
- <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">caching.</span></span><span class="sig-name descname"><span class="pre">property_cache_pickle</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source_instance</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cache_filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">load_all_on_init</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">callback_on_data_storage</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_age</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">store_on_get</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle" title="Link to this definition">¶</a></dt>
- <dd><p>This class caches the data from a given <cite>source_instance</cite>. It takes the data from the cache instead of generating the data from the <cite>source_instance</cite>,
- if the conditions for the cache usage are given.</p>
- <div class="admonition-required-properties-for-the-source-instance admonition">
- <p class="admonition-title">Required properties for the <cite>source_instance</cite></p>
- <ul class="simple">
- <li><p><strong>uid():</strong> returns the unique id of the source’s source or None, if you don’t want to use the unique id.</p></li>
- <li><p><strong>keys():</strong> returns a list of all available keys.</p></li>
- <li><p><strong>data_version():</strong> 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).</p></li>
- <li><p><strong>get(key, default):</strong> returns the property for a key. If key does not exists, default will be returned.</p></li>
- </ul>
- </div>
- <div class="admonition hint">
- <p class="admonition-title">Hint</p>
- <p>You are able to use all parameters and methods of the <cite>source_instance</cite> identically with the property_cache instance.</p>
- </div>
- <dl class="field-list simple">
- <dt class="field-odd">Parameters<span class="colon">:</span></dt>
- <dd class="field-odd"><ul class="simple">
- <li><p><strong>source_instance</strong> (<em>instance</em>) – The source instance holding the data</p></li>
- <li><p><strong>cache_filename</strong> (<em>str</em>) – File name, where the properties are stored as cache</p></li>
- <li><p><strong>load_all_on_init</strong> (<em>bool</em>) – True will load all data from the source instance, when the cache will be initialised the first time.</p></li>
- <li><p><strong>callback_on_data_storage</strong> (<em>method</em>) – The callback will be executed every time when the cache file is stored. It will be executed with the instance of this class as first argument.</p></li>
- <li><p><strong>max_age</strong> (<em>int</em><em> or </em><em>None</em>) – The maximum age of the cached data in seconds or None for no maximum age.</p></li>
- <li><p><strong>store_on_get</strong> (<em>bool</em>) – False will prevent cache storage with execution of the <cite>.get(key, default)</cite> method. You need to store the cache somewhere else.</p></li>
- </ul>
- </dd>
- </dl>
- <div class="admonition-the-cache-will-be-used-if-all-following-conditions-are-given admonition">
- <p class="admonition-title">The cache will be used, if all following conditions are given</p>
- <ul class="simple">
- <li><p>The key is in the list returned by <cite>.keys()</cite> method of the <cite>source_instance</cite></p></li>
- <li><p>The key is not in the list of keys added by the <cite>.add_source_get_keys()</cite> method.</p></li>
- <li><p>The cache age is less then the given max_age parameter or the given max_age is None.</p></li>
- <li><p>The uid of the source instance (e.g. a checksum or unique id of the source) is identically to to uid stored in the cache.</p></li>
- <li><p>The data version of the <cite>source_instance</cite> is <= the data version stored in the cache.</p></li>
- <li><p>The value is available in the previous stored information</p></li>
- </ul>
- </div>
- <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">caching</span>
- <span class="kn">import</span> <span class="nn">logging</span>
- <span class="kn">import</span> <span class="nn">sys</span>
- <span class="kn">import</span> <span class="nn">time</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">DATA_VERSION</span> <span class="o">=</span> <span class="mf">0.1</span>
- <span class="n">KEY_ONE</span> <span class="o">=</span> <span class="s1">'1'</span>
- <span class="n">KEY_TWO</span> <span class="o">=</span> <span class="s1">'2'</span>
- <span class="n">KEY_THREE</span> <span class="o">=</span> <span class="s1">'three'</span>
- <span class="n">KEY_FOUR</span> <span class="o">=</span> <span class="s1">'four'</span>
- <span class="n">KEY_FIVE</span> <span class="o">=</span> <span class="s1">'five'</span>
- <span class="n">KEYS</span> <span class="o">=</span> <span class="p">[</span><span class="n">KEY_ONE</span><span class="p">,</span> <span class="n">KEY_TWO</span><span class="p">,</span> <span class="n">KEY_THREE</span><span class="p">,</span> <span class="n">KEY_FOUR</span><span class="p">,</span> <span class="n">KEY_FIVE</span><span class="p">]</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">DATA_VERSION</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">try</span><span class="p">:</span>
- <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="sa">f</span><span class="s1">'__</span><span class="si">{</span><span class="n">key</span><span class="si">}</span><span class="s1">__'</span><span class="p">)()</span>
- <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</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">def</span> <span class="nf">print_n_sleep</span><span class="p">(</span><span class="bp">self</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">def</span> <span class="nf">__1__</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">print_n_sleep</span><span class="p">(</span><span class="s2">"__1__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'one'</span>
-
- <span class="k">def</span> <span class="nf">__2__</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">print_n_sleep</span><span class="p">(</span><span class="s2">"__2__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'two'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__three__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'three'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__four__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'four'</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="bp">self</span><span class="o">.</span><span class="n">print_n_sleep</span><span class="p">(</span><span class="s2">"__five__"</span><span class="p">)</span>
- <span class="k">return</span> <span class="s1">'five'</span>
-
-
- <span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
- <span class="c1"># Logging configuration</span>
- <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span>
- <span class="nb">format</span><span class="o">=</span><span class="s2">"</span><span class="si">%(asctime)s</span><span class="s2">: </span><span class="si">%(levelname)8s</span><span class="s2"> - </span><span class="si">%(name)s</span><span class="s2"> - </span><span class="si">%(message)s</span><span class="s2">"</span><span class="p">,</span>
- <span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">,</span>
- <span class="n">stream</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
- <span class="p">)</span>
- <span class="c1"># Example</span>
- <span class="n">tm</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
- <span class="n">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="s1">'cache.json'</span><span class="p">)</span>
- <span class="n">data</span><span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">KEY_THREE</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">\n</span><span class="s1">--------------------------------'</span><span class="p">)</span>
- <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">keys</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">get</span><span class="p">(</span><span class="n">key</span><span class="p">))</span>
- <span class="nb">print</span><span class="p">(</span><span class="s1">'--------------------------------</span><span class="se">\n</span><span class="s1">The execution time was </span><span class="si">%.1f</span><span class="s1">s'</span> <span class="o">%</span> <span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span><span class="o">-</span><span class="n">tm</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="o">--------------------------------</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">37</span><span class="p">,</span><span class="mi">924</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">37</span><span class="p">,</span><span class="mi">924</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">37</span><span class="p">,</span><span class="mi">924</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'1'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__1__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">40</span><span class="p">,</span><span class="mi">925</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">one</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928920</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">40</span><span class="p">,</span><span class="mi">926</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">40</span><span class="p">,</span><span class="mi">926</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'2'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__2__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">43</span><span class="p">,</span><span class="mi">927</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">two</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928923</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">43</span><span class="p">,</span><span class="mi">928</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">two</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">43</span><span class="p">,</span><span class="mi">928</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Key</span> <span class="s1">'three'</span> <span class="ow">is</span> <span class="n">excluded</span> <span class="n">by</span> <span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">()</span><span class="o">.</span> <span class="n">Uncached</span> <span class="n">data</span> <span class="n">will</span> <span class="n">be</span> <span class="n">returned</span><span class="o">.</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__three__</span>
- <span class="n">three</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">46</span><span class="p">,</span><span class="mi">929</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'four'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__four__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">49</span><span class="p">,</span><span class="mi">930</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="n">four</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">four</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928929</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">49</span><span class="p">,</span><span class="mi">930</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">49</span><span class="p">,</span><span class="mi">931</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Loading</span> <span class="nb">property</span> <span class="k">for</span> <span class="n">key</span><span class="o">=</span><span class="s1">'five'</span> <span class="kn">from</span> <span class="nn">source</span> <span class="n">instance</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__five__</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">931</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Adding</span> <span class="n">key</span><span class="o">=</span><span class="n">five</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="n">five</span> <span class="k">with</span> <span class="n">timestamp</span><span class="o">=</span><span class="mi">1726928932</span> <span class="n">to</span> <span class="n">chache</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">932</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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>
- <span class="o">--------------------------------</span>
- <span class="n">The</span> <span class="n">execution</span> <span class="n">time</span> <span class="n">was</span> <span class="mf">15.0</span><span class="n">s</span>
- </pre></div>
- </div>
- <p>With every following execution the time cosumption my by much smaller:</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="o">--------------------------------</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">973</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">973</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</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="n">one</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">973</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'2'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">two</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">52</span><span class="p">,</span><span class="mi">973</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Key</span> <span class="s1">'three'</span> <span class="ow">is</span> <span class="n">excluded</span> <span class="n">by</span> <span class="o">.</span><span class="n">add_source_get_keys</span><span class="p">()</span><span class="o">.</span> <span class="n">Uncached</span> <span class="n">data</span> <span class="n">will</span> <span class="n">be</span> <span class="n">returned</span><span class="o">.</span>
- <span class="n">slow</span> <span class="n">get</span> <span class="n">executed</span> <span class="k">for</span> <span class="n">__three__</span>
- <span class="n">three</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">55</span><span class="p">,</span><span class="mi">974</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'four'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">four</span>
- <span class="mi">2024</span><span class="o">-</span><span class="mi">09</span><span class="o">-</span><span class="mi">21</span> <span class="mi">16</span><span class="p">:</span><span class="mi">28</span><span class="p">:</span><span class="mi">55</span><span class="p">,</span><span class="mi">974</span><span class="p">:</span> <span class="n">DEBUG</span> <span class="o">-</span> <span class="n">caching</span> <span class="o">-</span> <span class="n">Providing</span> <span class="nb">property</span> <span class="k">for</span> <span class="s1">'five'</span> <span class="kn">from</span> <span class="nn">cache</span>
- <span class="n">five</span>
- <span class="o">--------------------------------</span>
- <span class="n">The</span> <span class="n">execution</span> <span class="n">time</span> <span class="n">was</span> <span class="mf">3.0</span><span class="n">s</span>
- </pre></div>
- </div>
- <dl class="py method">
- <dt class="sig sig-object py" id="caching.property_cache_pickle.add_source_get_keys">
- <span class="sig-name descname"><span class="pre">add_source_get_keys</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">keys</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.add_source_get_keys" title="Link to this definition">¶</a></dt>
- <dd><p>This will add one or more keys to a list of keys which will always be provided by the <cite>source_instance</cite> instead of the cache.</p>
- <dl class="field-list simple">
- <dt class="field-odd">Parameters<span class="colon">:</span></dt>
- <dd class="field-odd"><p><strong>keys</strong> (<em>list</em><em>, </em><em>tuple</em><em>, </em><em>str</em>) – The key or keys to be added</p>
- </dd>
- </dl>
- </dd></dl>
-
- <dl class="py method">
- <dt class="sig sig-object py" id="caching.property_cache_pickle.full_update">
- <span class="sig-name descname"><span class="pre">full_update</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.full_update" title="Link to this definition">¶</a></dt>
- <dd><p>With the execution of this method, the complete source data which needs to be cached, will be read from the source instance
- and the resulting cache will be stored to the given file.</p>
- <div class="admonition hint">
- <p class="admonition-title">Hint</p>
- <p>Use this method, if you initiallised the class with <cite>store_on_get=False</cite></p>
- </div>
- </dd></dl>
-
- <dl class="py method">
- <dt class="sig sig-object py" id="caching.property_cache_pickle.get">
- <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#caching.property_cache_pickle.get" title="Link to this definition">¶</a></dt>
- <dd><p>Method to get the cached property. If the key does not exists in the cache or <cite>source_instance</cite>, <cite>default</cite> will be returned.</p>
- <dl class="field-list simple">
- <dt class="field-odd">Parameters<span class="colon">:</span></dt>
- <dd class="field-odd"><ul class="simple">
- <li><p><strong>key</strong> – key for value to get.</p></li>
- <li><p><strong>default</strong> – value to be returned, if key does not exists.</p></li>
- </ul>
- </dd>
- <dt class="field-even">Returns<span class="colon">:</span></dt>
- <dd class="field-even"><p>value for a given key or default value.</p>
- </dd>
- </dl>
- </dd></dl>
-
- </dd></dl>
-
- </section>
- <section id="indices-and-tables">
- <h1>Indices and tables<a class="headerlink" href="#indices-and-tables" title="Link to this heading">¶</a></h1>
- <ul class="simple">
- <li><p><a class="reference internal" href="genindex.html"><span class="std std-ref">Index</span></a></p></li>
- <li><p><a class="reference internal" href="py-modindex.html"><span class="std std-ref">Module Index</span></a></p></li>
- <li><p><a class="reference internal" href="search.html"><span class="std std-ref">Search Page</span></a></p></li>
- </ul>
- </section>
-
-
- <div class="clearer"></div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar" role="navigation" aria-label="Main">
- <div class="sphinxsidebarwrapper">
- <div>
- <h3><a href="#">Table of Contents</a></h3>
- <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>
- <li><a class="reference internal" href="#caching.property_cache_json"><code class="docutils literal notranslate"><span class="pre">property_cache_json</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle</span></code></a><ul>
- <li><a class="reference internal" href="#caching.property_cache_pickle.add_source_get_keys"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.add_source_get_keys()</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle.full_update"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.full_update()</span></code></a></li>
- <li><a class="reference internal" href="#caching.property_cache_pickle.get"><code class="docutils literal notranslate"><span class="pre">property_cache_pickle.get()</span></code></a></li>
- </ul>
- </li>
- </ul>
- </li>
- <li><a class="reference internal" href="#indices-and-tables">Indices and tables</a></li>
- </ul>
-
- </div>
- <div role="note" aria-label="source link">
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="_sources/index.rst.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
- </div>
- <search id="searchbox" style="display: none" role="search">
- <h3 id="searchlabel">Quick search</h3>
- <div class="searchformwrapper">
- <form class="search" action="search.html" method="get">
- <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
- <input type="submit" value="Go" />
- </form>
- </div>
- </search>
- <script>document.getElementById('searchbox').style.display = "block"</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related" role="navigation" aria-label="Related">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li><img src="_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
- <li><a href="https://www.python.org/">Python</a> »</li>
- <li class="switchers">
- <div class="language_switcher_placeholder"></div>
- <div class="version_switcher_placeholder"></div>
- </li>
- <li>
-
- <a href="#">caching documentation</a> »
-
- </li>
- <li class="nav-item nav-item-this"><a href="">Welcome to cachings’s documentation!</a></li>
- <li class="right">
-
-
- <div class="inline-search" role="search">
- <form class="inline-search" action="search.html" method="get">
- <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
- <input type="submit" value="Go" />
- </form>
- </div>
- |
- </li>
- <li class="right">
- <label class="theme-selector-label">
- Theme
- <select class="theme-selector" oninput="activateTheme(this.value)">
- <option value="auto" selected>Auto</option>
- <option value="light">Light</option>
- <option value="dark">Dark</option>
- </select>
- </label> |</li>
-
- </ul>
- </div>
- <div class="footer">
- ©
- Copyright
- 2021, Dirk Alders.
- <br />
- This page is licensed under the Python Software Foundation License Version 2.
- <br />
- Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
- <br />
-
-
- <br />
-
- The Python Software Foundation is a non-profit corporation.
- <a href="https://www.python.org/psf/donations/">Please donate.</a>
- <br />
- <br />
-
- <br />
-
- Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.0.2.
- </div>
-
- </body>
- </html>
|