/usr/share/doc/python-dogpile.cache-doc/html/usage.html is in python-dogpile.cache-doc 0.5.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Usage Guide — dogpile.cache 0.5.7 documentation</title>
<link rel="stylesheet" href="_static/nature.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/changelog.css" type="text/css" />
<link rel="stylesheet" href="_static/sphinx_paramlinks.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.5.7',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</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>
<link rel="top" title="dogpile.cache 0.5.7 documentation" href="index.html" />
<link rel="next" title="API" href="api.html" />
<link rel="prev" title="Front Matter" href="front.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<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 class="right" >
<a href="api.html" title="API"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="front.html" title="Front Matter"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">dogpile.cache 0.5.7 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="usage-guide">
<h1>Usage Guide<a class="headerlink" href="#usage-guide" title="Permalink to this headline">¶</a></h1>
<div class="section" id="overview">
<h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>At the time of this writing, popular key/value servers include
<a class="reference external" href="http://memcached.org">Memcached</a>, <a class="reference external" href="http://redis.io/">Redis</a>, and <a class="reference external" href="http://wiki.basho.com/">Riak</a>.
While these tools all have different usage focuses, they all have in common that the storage model
is based on the retrieval of a value based on a key; as such, they are all potentially
suitable for caching, particularly Memcached which is first and foremost designed for
caching.</p>
<p>With a caching system in mind, dogpile.cache provides an interface to a particular Python API
targeted at that system.</p>
<p>A dogpile.cache configuration consists of the following components:</p>
<ul class="simple">
<li>A <em>region</em>, which is an instance of <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>, and defines the configuration
details for a particular cache backend. The <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> can be considered
the “front end” used by applications.</li>
<li>A <em>backend</em>, which is an instance of <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a>, describing how values
are stored and retrieved from a backend. This interface specifies only
<a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend.get" title="dogpile.cache.api.CacheBackend.get"><code class="xref py py-meth docutils literal"><span class="pre">get()</span></code></a>, <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend.set" title="dogpile.cache.api.CacheBackend.set"><code class="xref py py-meth docutils literal"><span class="pre">set()</span></code></a> and <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend.delete" title="dogpile.cache.api.CacheBackend.delete"><code class="xref py py-meth docutils literal"><span class="pre">delete()</span></code></a>.
The actual kind of <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a> in use for a particular <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>
is determined by the underlying Python API being used to talk to the cache, such
as Pylibmc. The <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a> is instantiated behind the scenes and
not directly accessed by applications under normal circumstances.</li>
<li>Value generation functions. These are user-defined functions that generate
new values to be placed in the cache. While dogpile.cache offers the usual
“set” approach of placing data into the cache, the usual mode of usage is to only instruct
it to “get” a value, passing it a <em>creation function</em> which will be used to
generate a new value if and only if one is needed. This “get-or-create” pattern
is the entire key to the “Dogpile” system, which coordinates a single value creation
operation among many concurrent get operations for a particular key, eliminating
the issue of an expired value being redundantly re-generated by many workers simultaneously.</li>
</ul>
</div>
<div class="section" id="rudimentary-usage">
<h2>Rudimentary Usage<a class="headerlink" href="#rudimentary-usage" title="Permalink to this headline">¶</a></h2>
<p>dogpile.cache includes a Pylibmc backend. A basic configuration looks like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s1">'dogpile.cache.pylibmc'</span><span class="p">,</span>
<span class="n">expiration_time</span> <span class="o">=</span> <span class="mi">3600</span><span class="p">,</span>
<span class="n">arguments</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'url'</span><span class="p">:</span> <span class="p">[</span><span class="s2">"127.0.0.1"</span><span class="p">],</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">load_user_info</span><span class="p">(</span><span class="n">user_id</span><span class="p">):</span>
<span class="k">return</span> <span class="n">some_database</span><span class="o">.</span><span class="n">lookup_user_by_id</span><span class="p">(</span><span class="n">user_id</span><span class="p">)</span>
</pre></div>
</div>
<div class="sidebar">
<p class="first sidebar-title">pylibmc</p>
<p class="last">In this section, we’re illustrating Memcached usage
using the <a class="reference external" href="http://pypi.python.org/pypi/pylibmc">pylibmc</a> backend, which is a high performing
Python library for Memcached. It can be compared to the <a class="reference external" href="http://pypi.python.org/pypi/python-memcached">python-memcached</a>
client, which is also an excellent product. Pylibmc is written against Memcached’s native API
so is markedly faster, though might be considered to have rougher edges. The API is actually a bit
more verbose to allow for correct multithreaded usage.</p>
</div>
<p>Above, we create a <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> using the <a class="reference internal" href="api.html#dogpile.cache.region.make_region" title="dogpile.cache.region.make_region"><code class="xref py py-func docutils literal"><span class="pre">make_region()</span></code></a> function, then
apply the backend configuration via the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a> method, which returns the
region. The name of the backend is the only argument required by <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a>
itself, in this case <code class="docutils literal"><span class="pre">dogpile.cache.pylibmc</span></code>. However, in this specific case, the <code class="docutils literal"><span class="pre">pylibmc</span></code>
backend also requires that the URL of the memcached server be passed within the <code class="docutils literal"><span class="pre">arguments</span></code> dictionary.</p>
<p>The configuration is separated into two sections. Upon construction via <a class="reference internal" href="api.html#dogpile.cache.region.make_region" title="dogpile.cache.region.make_region"><code class="xref py py-func docutils literal"><span class="pre">make_region()</span></code></a>,
the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> object is available, typically at module
import time, for usage in decorating functions. Additional configuration details passed to
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a> are typically loaded from a configuration file and therefore
not necessarily available until runtime, hence the two-step configurational process.</p>
<p>Key arguments passed to <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a> include <em>expiration_time</em>, which is the expiration
time passed to the Dogpile lock, and <em>arguments</em>, which are arguments used directly
by the backend - in this case we are using arguments that are passed directly
to the pylibmc module.</p>
</div>
<div class="section" id="region-configuration">
<h2>Region Configuration<a class="headerlink" href="#region-configuration" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="api.html#dogpile.cache.region.make_region" title="dogpile.cache.region.make_region"><code class="xref py py-func docutils literal"><span class="pre">make_region()</span></code></a> function currently calls the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> constructor directly.</p>
<dl class="class">
<dt>
<em class="property">class </em><code class="descclassname">dogpile.cache.region.</code><code class="descname">CacheRegion</code><span class="sig-paren">(</span><em>name=None</em>, <em>function_key_generator=<function function_key_generator></em>, <em>function_multi_key_generator=<function function_multi_key_generator></em>, <em>key_mangler=None</em>, <em>async_creation_runner=None</em><span class="sig-paren">)</span></dt>
<dd><p>A front end to a particular cache backend.</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><span class="target" id="dogpile.cache.region.CacheRegion.params.name"></span><strong>name</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.params.name">¶</a> – Optional, a string name for the region.
This isn’t used internally
but can be accessed via the <code class="docutils literal"><span class="pre">.name</span></code> parameter, helpful
for configuring a region from a config file.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.params.function_key_generator"></span><strong>function_key_generator</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.params.function_key_generator">¶</a> – <p>Optional. A
function that will produce a “cache key” given
a data creation function and arguments, when using
the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a> method.
The structure of this function
should be two levels: given the data creation function,
return a new function that generates the key based on
the given arguments. Such as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_key_generator</span><span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="n">fn</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="n">fname</span> <span class="o">=</span> <span class="n">fn</span><span class="o">.</span><span class="n">__name__</span>
<span class="k">def</span> <span class="nf">generate_key</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
<span class="k">return</span> <span class="n">namespace</span> <span class="o">+</span> <span class="s2">"_"</span> <span class="o">+</span> <span class="n">fname</span> <span class="o">+</span> <span class="s2">"_"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">)</span>
<span class="k">return</span> <span class="n">generate_key</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">(</span>
<span class="n">function_key_generator</span> <span class="o">=</span> <span class="n">my_key_generator</span>
<span class="p">)</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s2">"dogpile.cache.dbm"</span><span class="p">,</span>
<span class="n">expiration_time</span><span class="o">=</span><span class="mi">300</span><span class="p">,</span>
<span class="n">arguments</span><span class="o">=</span><span class="p">{</span>
<span class="s2">"filename"</span><span class="p">:</span><span class="s2">"file.dbm"</span>
<span class="p">}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">namespace</span></code> is that passed to
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a>. It’s not consulted
outside this function, so in fact can be of any form.
For example, it can be passed as a tuple, used to specify
arguments to pluck from **kw:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_key_generator</span><span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="n">fn</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">generate_key</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">":"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
<span class="p">[</span><span class="n">kw</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">namespace</span><span class="p">]</span> <span class="o">+</span>
<span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">]</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">generate_key</span>
</pre></div>
</div>
<p>Where the decorator might be used as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@my_region.cache_on_arguments</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="p">(</span><span class="s1">'x'</span><span class="p">,</span> <span class="s1">'y'</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">my_function</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="k">return</span> <span class="n">my_data</span><span class="p">()</span>
</pre></div>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.params.function_multi_key_generator"></span><strong>function_multi_key_generator</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.params.function_multi_key_generator">¶</a> – <p>Optional.
Similar to <code class="docutils literal"><span class="pre">function_key_generator</span></code> parameter, but it’s used in
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_multi_on_arguments" title="dogpile.cache.region.CacheRegion.cache_multi_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_multi_on_arguments()</span></code></a>. Generated function
should return list of keys. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">my_multi_key_generator</span><span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="n">fn</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="n">namespace</span> <span class="o">=</span> <span class="n">fn</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="p">(</span><span class="n">namespace</span> <span class="ow">or</span> <span class="s1">''</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">generate_keys</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">return</span> <span class="p">[</span><span class="n">namespace</span> <span class="o">+</span> <span class="s1">':'</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span>
<span class="k">return</span> <span class="n">generate_keys</span>
</pre></div>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.params.key_mangler"></span><strong>key_mangler</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.params.key_mangler">¶</a> – Function which will be used on all incoming
keys before passing to the backend. Defaults to <code class="docutils literal"><span class="pre">None</span></code>,
in which case the key mangling function recommended by
the cache backend will be used. A typical mangler
is the SHA1 mangler found at <a class="reference internal" href="api.html#dogpile.cache.util.sha1_mangle_key" title="dogpile.cache.util.sha1_mangle_key"><code class="xref py py-func docutils literal"><span class="pre">sha1_mangle_key()</span></code></a>
which coerces keys into a SHA1
hash, so that the string length is fixed. To
disable all key mangling, set to <code class="docutils literal"><span class="pre">False</span></code>. Another typical
mangler is the built-in Python function <code class="docutils literal"><span class="pre">str</span></code>, which can be used
to convert non-string or Unicode keys to bytestrings, which is
needed when using a backend such as bsddb or dbm under Python 2.x
in conjunction with Unicode keys.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.params.async_creation_runner"></span><strong>async_creation_runner</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.params.async_creation_runner">¶</a> – <p>A callable that, when specified,
will be passed to and called by dogpile.lock when
there is a stale value present in the cache. It will be passed the
mutex and is responsible releasing that mutex when finished.
This can be used to defer the computation of expensive creator
functions to later points in the future by way of, for example, a
background thread, a long-running queue, or a task manager system
like Celery.</p>
<p>For a specific example using async_creation_runner, new values can
be created in a background thread like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">threading</span>
<span class="k">def</span> <span class="nf">async_creation_runner</span><span class="p">(</span><span class="n">cache</span><span class="p">,</span> <span class="n">somekey</span><span class="p">,</span> <span class="n">creator</span><span class="p">,</span> <span class="n">mutex</span><span class="p">):</span>
<span class="sd">''' Used by dogpile.core:Lock when appropriate '''</span>
<span class="k">def</span> <span class="nf">runner</span><span class="p">():</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">creator</span><span class="p">()</span>
<span class="n">cache</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">somekey</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">mutex</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
<span class="n">thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">runner</span><span class="p">)</span>
<span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">(</span>
<span class="n">async_creation_runner</span><span class="o">=</span><span class="n">async_creation_runner</span><span class="p">,</span>
<span class="p">)</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s1">'dogpile.cache.memcached'</span><span class="p">,</span>
<span class="n">expiration_time</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>
<span class="n">arguments</span><span class="o">=</span><span class="p">{</span>
<span class="s1">'url'</span><span class="p">:</span> <span class="s1">'127.0.0.1:11211'</span><span class="p">,</span>
<span class="s1">'distributed_lock'</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Remember that the first request for a key with no associated
value will always block; async_creator will not be invoked.
However, subsequent requests for cached-but-expired values will
still return promptly. They will be refreshed by whatever
asynchronous means the provided async_creation_runner callable
implements.</p>
<p>By default the async_creation_runner is disabled and is set
to <code class="docutils literal"><span class="pre">None</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.4.2: </span>added the async_creation_runner
feature.</p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>One you have a <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>, the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a> method can
be used to decorate functions, but the cache itself can’t be used until
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a> is called. The interface for that method is as follows:</p>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">configure</code><span class="sig-paren">(</span><em>backend</em>, <em>expiration_time=None</em>, <em>arguments=None</em>, <em>_config_argument_dict=None</em>, <em>_config_prefix=None</em>, <em>wrap=None</em>, <em>replace_existing_backend=False</em><span class="sig-paren">)</span></dt>
<dd><p>Configure a <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>.</p>
<p>The <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> itself
is returned.</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><span class="target" id="dogpile.cache.region.CacheRegion.configure.params.backend"></span><strong>backend</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.configure.params.backend">¶</a> – Required. This is the name of the
<a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a> to use, and is resolved by loading
the class from the <code class="docutils literal"><span class="pre">dogpile.cache</span></code> entrypoint.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.configure.params.expiration_time"></span><strong>expiration_time</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.configure.params.expiration_time">¶</a> – <p>Optional. The expiration time passed
to the dogpile system. May be passed as an integer number
of seconds, or as a <code class="docutils literal"><span class="pre">datetime.timedelta</span></code> value.</p>
<p>The <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create" title="dogpile.cache.region.CacheRegion.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get_or_create()</span></code></a>
method as well as the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a>
decorator (though note: <strong>not</strong> the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get" title="dogpile.cache.region.CacheRegion.get"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get()</span></code></a>
method) will call upon the value creation function after this
time period has passed since the last generation.</p>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.configure.params.arguments"></span><strong>arguments</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.configure.params.arguments">¶</a> – Optional. The structure here is passed
directly to the constructor of the <a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a>
in use, though is typically a dictionary.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.configure.params.wrap"></span><strong>wrap</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.configure.params.wrap">¶</a> – <p>Optional. A list of <a class="reference internal" href="api.html#dogpile.cache.proxy.ProxyBackend" title="dogpile.cache.proxy.ProxyBackend"><code class="xref py py-class docutils literal"><span class="pre">ProxyBackend</span></code></a>
classes and/or instances, each of which will be applied
in a chain to ultimately wrap the original backend,
so that custom functionality augmentation can be applied.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.0.</span></p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#changing-backend-behavior"><span>Changing Backend Behavior</span></a></p>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.configure.params.replace_existing_backend"></span><strong>replace_existing_backend</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.configure.params.replace_existing_backend">¶</a> – <p>if True, the existing cache backend
will be replaced. Without this flag, an exception is raised if
a backend is already configured.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.7.</span></p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>The <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> can also be configured from a dictionary, using the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure_from_config" title="dogpile.cache.region.CacheRegion.configure_from_config"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure_from_config()</span></code></a>
method:</p>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">configure_from_config</code><span class="sig-paren">(</span><em>config_dict</em>, <em>prefix</em><span class="sig-paren">)</span></dt>
<dd><p>Configure from a configuration dictionary
and a prefix.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">local_region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span>
<span class="n">memcached_region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span>
<span class="c1"># regions are ready to use for function</span>
<span class="c1"># decorators, but not yet for actual caching</span>
<span class="c1"># later, when config is available</span>
<span class="n">myconfig</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"cache.local.backend"</span><span class="p">:</span><span class="s2">"dogpile.cache.dbm"</span><span class="p">,</span>
<span class="s2">"cache.local.arguments.filename"</span><span class="p">:</span><span class="s2">"/path/to/dbmfile.dbm"</span><span class="p">,</span>
<span class="s2">"cache.memcached.backend"</span><span class="p">:</span><span class="s2">"dogpile.cache.pylibmc"</span><span class="p">,</span>
<span class="s2">"cache.memcached.arguments.url"</span><span class="p">:</span><span class="s2">"127.0.0.1, 10.0.0.1"</span><span class="p">,</span>
<span class="p">}</span>
<span class="n">local_region</span><span class="o">.</span><span class="n">configure_from_config</span><span class="p">(</span><span class="n">myconfig</span><span class="p">,</span> <span class="s2">"cache.local."</span><span class="p">)</span>
<span class="n">memcached_region</span><span class="o">.</span><span class="n">configure_from_config</span><span class="p">(</span><span class="n">myconfig</span><span class="p">,</span>
<span class="s2">"cache.memcached."</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="using-a-region">
<h2>Using a Region<a class="headerlink" href="#using-a-region" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> object is our front-end interface to a cache. It includes
the following methods:</p>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">get</code><span class="sig-paren">(</span><em>key</em>, <em>expiration_time=None</em>, <em>ignore_expiration=False</em><span class="sig-paren">)</span></dt>
<dd><p>Return a value from the cache, based on the given key.</p>
<p>If the value is not present, the method returns the token
<code class="docutils literal"><span class="pre">NO_VALUE</span></code>. <code class="docutils literal"><span class="pre">NO_VALUE</span></code> evaluates to False, but is separate from
<code class="docutils literal"><span class="pre">None</span></code> to distinguish between a cached value of <code class="docutils literal"><span class="pre">None</span></code>.</p>
<p>By default, the configured expiration time of the
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>, or alternatively the expiration
time supplied by the <code class="docutils literal"><span class="pre">expiration_time</span></code> argument,
is tested against the creation time of the retrieved
value versus the current time (as reported by <code class="docutils literal"><span class="pre">time.time()</span></code>).
If stale, the cached value is ignored and the <code class="docutils literal"><span class="pre">NO_VALUE</span></code>
token is returned. Passing the flag <code class="docutils literal"><span class="pre">ignore_expiration=True</span></code>
bypasses the expiration time check.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.3.0: </span><a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get" title="dogpile.cache.region.CacheRegion.get"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get()</span></code></a> now checks the value’s creation time
against the expiration time, rather than returning
the value unconditionally.</p>
</div>
<p>The method also interprets the cached value in terms
of the current “invalidation” time as set by
the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.invalidate" title="dogpile.cache.region.CacheRegion.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">invalidate()</span></code></a> method. If a value is present,
but its creation time is older than the current
invalidation time, the <code class="docutils literal"><span class="pre">NO_VALUE</span></code> token is returned.
Passing the flag <code class="docutils literal"><span class="pre">ignore_expiration=True</span></code> bypasses
the invalidation time check.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.3.0: </span>Support for the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.invalidate" title="dogpile.cache.region.CacheRegion.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.invalidate()</span></code></a>
method.</p>
</div>
<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><span class="target" id="dogpile.cache.region.CacheRegion.get.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get.params.key">¶</a> – Key to be retrieved. While it’s typical for a key to be a
string, it is ultimately passed directly down to the cache backend,
before being optionally processed by the key_mangler function, so can
be of any type recognized by the backend or by the key_mangler
function, if present.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.get.params.expiration_time"></span><strong>expiration_time</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get.params.expiration_time">¶</a> – <p>Optional expiration time value
which will supersede that configured on the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>
itself.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.3.0.</span></p>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.get.params.ignore_expiration"></span><strong>ignore_expiration</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get.params.ignore_expiration">¶</a> – <p>if <code class="docutils literal"><span class="pre">True</span></code>, the value is returned
from the cache if present, regardless of configured
expiration times or whether or not <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.invalidate" title="dogpile.cache.region.CacheRegion.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">invalidate()</span></code></a>
was called.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.3.0.</span></p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">get_or_create</code><span class="sig-paren">(</span><em>key</em>, <em>creator</em>, <em>expiration_time=None</em>, <em>should_cache_fn=None</em><span class="sig-paren">)</span></dt>
<dd><p>Return a cached value based on the given key.</p>
<p>If the value does not exist or is considered to be expired
based on its creation time, the given
creation function may or may not be used to recreate the value
and persist the newly generated value in the cache.</p>
<p>Whether or not the function is used depends on if the
<em>dogpile lock</em> can be acquired or not. If it can’t, it means
a different thread or process is already running a creation
function for this key against the cache. When the dogpile
lock cannot be acquired, the method will block if no
previous value is available, until the lock is released and
a new value available. If a previous value
is available, that value is returned immediately without blocking.</p>
<p>If the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.invalidate" title="dogpile.cache.region.CacheRegion.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">invalidate()</span></code></a> method has been called, and
the retrieved value’s timestamp is older than the invalidation
timestamp, the value is unconditionally prevented from
being returned. The method will attempt to acquire the dogpile
lock to generate a new value, or will wait
until the lock is released to return the new value.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.3.0: </span>The value is unconditionally regenerated if the creation
time is older than the last call to <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.invalidate" title="dogpile.cache.region.CacheRegion.invalidate"><code class="xref py py-meth docutils literal"><span class="pre">invalidate()</span></code></a>.</p>
</div>
<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><span class="target" id="dogpile.cache.region.CacheRegion.get_or_create.params.key"></span><strong>key</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get_or_create.params.key">¶</a> – Key to be retrieved. While it’s typical for a key to be a
string, it is ultimately passed directly down to the cache backend,
before being optionally processed by the key_mangler function, so can
be of any type recognized by the backend or by the key_mangler
function, if present.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.get_or_create.params.creator"></span><strong>creator</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get_or_create.params.creator">¶</a> – function which creates a new value.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.get_or_create.params.expiration_time"></span><strong>expiration_time</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get_or_create.params.expiration_time">¶</a> – optional expiration time which will overide
the expiration time already configured on this <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>
if not None. To set no expiration, use the value -1.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.get_or_create.params.should_cache_fn"></span><strong>should_cache_fn</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.get_or_create.params.should_cache_fn">¶</a> – <p>optional callable function which will receive
the value returned by the “creator”, and will then return True or
False, indicating if the value should actually be cached or not. If
it returns False, the value is still returned, but isn’t cached.
E.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">dont_cache_none</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
<span class="k">return</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>
<span class="n">value</span> <span class="o">=</span> <span class="n">region</span><span class="o">.</span><span class="n">get_or_create</span><span class="p">(</span><span class="s2">"some key"</span><span class="p">,</span>
<span class="n">create_value</span><span class="p">,</span>
<span class="n">should_cache_fn</span><span class="o">=</span><span class="n">dont_cache_none</span><span class="p">)</span>
</pre></div>
</div>
<p>Above, the function returns the value of create_value() if
the cache is invalid, however if the return value is None,
it won’t be cached.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.4.3.</span></p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p><a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a> - applies
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create" title="dogpile.cache.region.CacheRegion.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">get_or_create()</span></code></a> to any function using a decorator.</p>
<dl class="last docutils">
<dt><a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create_multi" title="dogpile.cache.region.CacheRegion.get_or_create_multi"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get_or_create_multi()</span></code></a> - multiple key/value</dt>
<dd>version</dd>
</dl>
</div>
</dd></dl>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">set</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span></dt>
<dd><p>Place a new value in the cache under the given key.</p>
</dd></dl>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">delete</code><span class="sig-paren">(</span><em>key</em><span class="sig-paren">)</span></dt>
<dd><p>Remove a value from the cache.</p>
<p>This operation is idempotent (can be called multiple times, or on a
non-existent key, safely)</p>
</dd></dl>
<dl class="method">
<dt>
<code class="descclassname">CacheRegion.</code><code class="descname">cache_on_arguments</code><span class="sig-paren">(</span><em>namespace=None</em>, <em>expiration_time=None</em>, <em>should_cache_fn=None</em>, <em>to_str=<type 'str'></em>, <em>function_key_generator=None</em><span class="sig-paren">)</span></dt>
<dd><p>A function decorator that will cache the return
value of the function using a key derived from the
function itself and its arguments.</p>
<p>The decorator internally makes use of the
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create" title="dogpile.cache.region.CacheRegion.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get_or_create()</span></code></a> method to access the
cache and conditionally call the function. See that
method for additional behavioral details.</p>
<p>E.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@someregion.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">generate_something</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">somedatabase</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>The decorated function can then be called normally, where
data will be pulled from the cache region unless a new
value is needed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">result</span> <span class="o">=</span> <span class="n">generate_something</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
<p>The function is also given an attribute <code class="docutils literal"><span class="pre">invalidate()</span></code>, which
provides for invalidation of the value. Pass to <code class="docutils literal"><span class="pre">invalidate()</span></code>
the same arguments you’d pass to the function itself to represent
a particular value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">generate_something</span><span class="o">.</span><span class="n">invalidate</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
<p>Another attribute <code class="docutils literal"><span class="pre">set()</span></code> is added to provide extra caching
possibilities relative to the function. This is a convenience
method for <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.set" title="dogpile.cache.region.CacheRegion.set"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.set()</span></code></a> which will store a given
value directly without calling the decorated function.
The value to be cached is passed as the first argument, and the
arguments which would normally be passed to the function
should follow:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">generate_something</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
<p>The above example is equivalent to calling
<code class="docutils literal"><span class="pre">generate_something(5,</span> <span class="pre">6)</span></code>, if the function were to produce
the value <code class="docutils literal"><span class="pre">3</span></code> as the value to be cached.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.4.1: </span>Added <code class="docutils literal"><span class="pre">set()</span></code> method to decorated function.</p>
</div>
<p>Similar to <code class="docutils literal"><span class="pre">set()</span></code> is <code class="docutils literal"><span class="pre">refresh()</span></code>. This attribute will
invoke the decorated function and populate a new value into
the cache with the new value, as well as returning that value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">newvalue</span> <span class="o">=</span> <span class="n">generate_something</span><span class="o">.</span><span class="n">refresh</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.0: </span>Added <code class="docutils literal"><span class="pre">refresh()</span></code> method to decorated
function.</p>
</div>
<p>Lastly, the <code class="docutils literal"><span class="pre">get()</span></code> method returns either the value cached
for the given key, or the token <code class="docutils literal"><span class="pre">NO_VALUE</span></code> if no such key
exists:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">value</span> <span class="o">=</span> <span class="n">generate_something</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.3: </span>Added <code class="docutils literal"><span class="pre">get()</span></code> method to decorated
function.</p>
</div>
<p>The default key generation will use the name
of the function, the module name for the function,
the arguments passed, as well as an optional “namespace”
parameter in order to generate a cache key.</p>
<p>Given a function <code class="docutils literal"><span class="pre">one</span></code> inside the module
<code class="docutils literal"><span class="pre">myapp.tools</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@region.cache_on_arguments</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s2">"foo"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">one</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</pre></div>
</div>
<p>Above, calling <code class="docutils literal"><span class="pre">one(3,</span> <span class="pre">4)</span></code> will produce a
cache key as follows:</p>
<div class="highlight-python"><div class="highlight"><pre>myapp.tools:one|foo|3 4
</pre></div>
</div>
<p>The key generator will ignore an initial argument
of <code class="docutils literal"><span class="pre">self</span></code> or <code class="docutils literal"><span class="pre">cls</span></code>, making the decorator suitable
(with caveats) for use with instance or class methods.
Given the example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s2">"foo"</span><span class="p">)</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="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</pre></div>
</div>
<p>The cache key above for <code class="docutils literal"><span class="pre">MyClass().one(3,</span> <span class="pre">4)</span></code> will
again produce the same cache key of <code class="docutils literal"><span class="pre">myapp.tools:one|foo|3</span> <span class="pre">4</span></code> -
the name <code class="docutils literal"><span class="pre">self</span></code> is skipped.</p>
<p>The <code class="docutils literal"><span class="pre">namespace</span></code> parameter is optional, and is used
normally to disambiguate two functions of the same
name within the same module, as can occur when decorating
instance or class methods as below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s1">'MC'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">somemethod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="s2">""</span>
<span class="k">class</span> <span class="nc">MyOtherClass</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">(</span><span class="n">namespace</span><span class="o">=</span><span class="s1">'MOC'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">somemethod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="s2">""</span>
</pre></div>
</div>
<p>Above, the <code class="docutils literal"><span class="pre">namespace</span></code> parameter disambiguates
between <code class="docutils literal"><span class="pre">somemethod</span></code> on <code class="docutils literal"><span class="pre">MyClass</span></code> and <code class="docutils literal"><span class="pre">MyOtherClass</span></code>.
Python class declaration mechanics otherwise prevent
the decorator from having awareness of the <code class="docutils literal"><span class="pre">MyClass</span></code>
and <code class="docutils literal"><span class="pre">MyOtherClass</span></code> names, as the function is received
by the decorator before it becomes an instance method.</p>
<p>The function key generation can be entirely replaced
on a per-region basis using the <code class="docutils literal"><span class="pre">function_key_generator</span></code>
argument present on <a class="reference internal" href="api.html#dogpile.cache.region.make_region" title="dogpile.cache.region.make_region"><code class="xref py py-func docutils literal"><span class="pre">make_region()</span></code></a> and
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a>. If defaults to
<a class="reference internal" href="api.html#dogpile.cache.util.function_key_generator" title="dogpile.cache.util.function_key_generator"><code class="xref py py-func docutils literal"><span class="pre">function_key_generator()</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><span class="target" id="dogpile.cache.region.CacheRegion.cache_on_arguments.params.namespace"></span><strong>namespace</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.cache_on_arguments.params.namespace">¶</a> – optional string argument which will be
established as part of the cache key. This may be needed
to disambiguate functions of the same name within the same
source file, such as those
associated with classes - note that the decorator itself
can’t see the parent class on a function as the class is
being declared.</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.cache_on_arguments.params.expiration_time"></span><strong>expiration_time</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.cache_on_arguments.params.expiration_time">¶</a> – <p>if not None, will override the normal
expiration time.</p>
<p>May be specified as a callable, taking no arguments, that
returns a value to be used as the <code class="docutils literal"><span class="pre">expiration_time</span></code>. This callable
will be called whenever the decorated function itself is called, in
caching or retrieving. Thus, this can be used to
determine a <em>dynamic</em> expiration time for the cached function
result. Example use cases include “cache the result until the
end of the day, week or time period” and “cache until a certain date
or time passes”.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5.0: </span><code class="docutils literal"><span class="pre">expiration_time</span></code> may be passed as a callable to
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_on_arguments" title="dogpile.cache.region.CacheRegion.cache_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_on_arguments()</span></code></a>.</p>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.cache_on_arguments.params.should_cache_fn"></span><strong>should_cache_fn</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.cache_on_arguments.params.should_cache_fn">¶</a> – <p>passed to <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create" title="dogpile.cache.region.CacheRegion.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get_or_create()</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.4.3.</span></p>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.cache_on_arguments.params.to_str"></span><strong>to_str</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.cache_on_arguments.params.to_str">¶</a> – <p>callable, will be called on each function argument
in order to convert to a string. Defaults to <code class="docutils literal"><span class="pre">str()</span></code>. If the
function accepts non-ascii unicode arguments on Python 2.x, the
<code class="docutils literal"><span class="pre">unicode()</span></code> builtin can be substituted, but note this will
produce unicode cache keys which may require key mangling before
reaching the cache.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.0.</span></p>
</div>
</li>
<li><span class="target" id="dogpile.cache.region.CacheRegion.cache_on_arguments.params.function_key_generator"></span><strong>function_key_generator</strong><a class="paramlink headerlink reference internal" href="#dogpile.cache.region.CacheRegion.cache_on_arguments.params.function_key_generator">¶</a> – <p>a function that will produce a
“cache key”. This function will supersede the one configured on the
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> itself.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5.5.</span></p>
</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p><a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.cache_multi_on_arguments" title="dogpile.cache.region.CacheRegion.cache_multi_on_arguments"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.cache_multi_on_arguments()</span></code></a></p>
<p class="last"><a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.get_or_create" title="dogpile.cache.region.CacheRegion.get_or_create"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.get_or_create()</span></code></a></p>
</div>
</dd></dl>
</div>
<div class="section" id="creating-backends">
<span id="id1"></span><h2>Creating Backends<a class="headerlink" href="#creating-backends" title="Permalink to this headline">¶</a></h2>
<p>Backends are located using the setuptools entrypoint system. To make life easier
for writers of ad-hoc backends, a helper function is included which registers any
backend in the same way as if it were part of the existing sys.path.</p>
<p>For example, to create a backend called <code class="docutils literal"><span class="pre">DictionaryBackend</span></code>, we subclass
<a class="reference internal" href="api.html#dogpile.cache.api.CacheBackend" title="dogpile.cache.api.CacheBackend"><code class="xref py py-class docutils literal"><span class="pre">CacheBackend</span></code></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache.api</span> <span class="kn">import</span> <span class="n">CacheBackend</span><span class="p">,</span> <span class="n">NO_VALUE</span>
<span class="k">class</span> <span class="nc">DictionaryBackend</span><span class="p">(</span><span class="n">CacheBackend</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="n">arguments</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cache</span> <span class="o">=</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cache</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">NO_VALUE</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">set</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">value</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">cache</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
<span class="k">def</span> <span class="nf">delete</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="bp">self</span><span class="o">.</span><span class="n">cache</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
</pre></div>
</div>
<p>Then make sure the class is available underneath the entrypoint
<code class="docutils literal"><span class="pre">dogpile.cache</span></code>. If we did this in a <code class="docutils literal"><span class="pre">setup.py</span></code> file, it would be
in <code class="docutils literal"><span class="pre">setup()</span></code> as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">entry_points</span><span class="o">=</span><span class="s2">"""</span>
<span class="s2"> [dogpile.cache]</span>
<span class="s2"> dictionary = mypackage.mybackend:DictionaryBackend</span>
<span class="s2"> """</span>
</pre></div>
</div>
<p>Alternatively, if we want to register the plugin in the same process
space without bothering to install anything, we can use <code class="docutils literal"><span class="pre">register_backend</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">register_backend</span>
<span class="n">register_backend</span><span class="p">(</span><span class="s2">"dictionary"</span><span class="p">,</span> <span class="s2">"mypackage.mybackend"</span><span class="p">,</span> <span class="s2">"DictionaryBackend"</span><span class="p">)</span>
</pre></div>
</div>
<p>Our new backend would be usable in a region like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">(</span><span class="s2">"myregion"</span><span class="p">)</span>
<span class="n">region</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span><span class="s2">"dictionary"</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">region</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">"somekey"</span><span class="p">,</span> <span class="s2">"somevalue"</span><span class="p">)</span>
</pre></div>
</div>
<p>The values we receive for the backend here are instances of
<code class="docutils literal"><span class="pre">CachedValue</span></code>. This is a tuple subclass of length two, of the form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="n">payload</span><span class="p">,</span> <span class="n">metadata</span><span class="p">)</span>
</pre></div>
</div>
<p>Where “payload” is the thing being cached, and “metadata” is information
we store in the cache - a dictionary which currently has just the “creation time”
and a “version identifier” as key/values. If the cache backend requires serialization,
pickle or similar can be used on the tuple - the “metadata” portion will always
be a small and easily serializable Python structure.</p>
</div>
<div class="section" id="changing-backend-behavior">
<span id="id2"></span><h2>Changing Backend Behavior<a class="headerlink" href="#changing-backend-behavior" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="api.html#dogpile.cache.proxy.ProxyBackend" title="dogpile.cache.proxy.ProxyBackend"><code class="xref py py-class docutils literal"><span class="pre">ProxyBackend</span></code></a> is a decorator class provided to easily augment existing
backend behavior without having to extend the original class. Using a decorator
class is also adventageous as it allows us to share the altered behavior between
different backends.</p>
<p>Proxies are added to the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> object using the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a>
method. Only the overridden methods need to be specified and the real backend can
be accessed with the <code class="docutils literal"><span class="pre">self.proxied</span></code> object from inside the <a class="reference internal" href="api.html#dogpile.cache.proxy.ProxyBackend" title="dogpile.cache.proxy.ProxyBackend"><code class="xref py py-class docutils literal"><span class="pre">ProxyBackend</span></code></a>.</p>
<p>For example, a simple class to log all calls to <code class="docutils literal"><span class="pre">.set()</span></code> would look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache.proxy</span> <span class="kn">import</span> <span class="n">ProxyBackend</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">LoggingProxy</span><span class="p">(</span><span class="n">ProxyBackend</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">set</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">value</span><span class="p">):</span>
<span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'Setting Cache Key: </span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">proxied</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p><a class="reference internal" href="api.html#dogpile.cache.proxy.ProxyBackend" title="dogpile.cache.proxy.ProxyBackend"><code class="xref py py-class docutils literal"><span class="pre">ProxyBackend</span></code></a> can be be configured to optionally take arguments (as long as the
<code class="xref py py-meth docutils literal"><span class="pre">ProxyBackend.__init__()</span></code> method is called properly, either directly
or via <code class="docutils literal"><span class="pre">super()</span></code>. In the example
below, the <code class="docutils literal"><span class="pre">RetryDeleteProxy</span></code> class accepts a <code class="docutils literal"><span class="pre">retry_count</span></code> parameter
on initialization. In the event of an exception on delete(), it will retry
this many times before returning:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache.proxy</span> <span class="kn">import</span> <span class="n">ProxyBackend</span>
<span class="k">class</span> <span class="nc">RetryDeleteProxy</span><span class="p">(</span><span class="n">ProxyBackend</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="n">retry_count</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">RetryDeleteProxy</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">retry_count</span> <span class="o">=</span> <span class="n">retry_count</span>
<span class="k">def</span> <span class="nf">delete</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">retries</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">retry_count</span>
<span class="k">while</span> <span class="n">retries</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="n">retries</span> <span class="o">-=</span> <span class="mi">1</span>
<span class="k">try</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">proxied</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">return</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">wrap</span></code> parameter of the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.configure" title="dogpile.cache.region.CacheRegion.configure"><code class="xref py py-meth docutils literal"><span class="pre">CacheRegion.configure()</span></code></a> accepts a list
which can contain any combination of instantiated proxy objects
as well as uninstantiated proxy classes.
Putting the two examples above together would look like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>
<span class="n">retry_proxy</span> <span class="o">=</span> <span class="n">RetryDeleteProxy</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s1">'dogpile.cache.pylibmc'</span><span class="p">,</span>
<span class="n">expiration_time</span> <span class="o">=</span> <span class="mi">3600</span><span class="p">,</span>
<span class="n">arguments</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'url'</span><span class="p">:[</span><span class="s2">"127.0.0.1"</span><span class="p">],</span>
<span class="p">},</span>
<span class="n">wrap</span> <span class="o">=</span> <span class="p">[</span> <span class="n">LoggingProxy</span><span class="p">,</span> <span class="n">retry_proxy</span> <span class="p">]</span>
<span class="p">)</span>
</pre></div>
</div>
<p>In the above example, the <code class="docutils literal"><span class="pre">LoggingProxy</span></code> object would be instantated by the
<a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion" title="dogpile.cache.region.CacheRegion"><code class="xref py py-class docutils literal"><span class="pre">CacheRegion</span></code></a> and applied to wrap requests on behalf of
the <code class="docutils literal"><span class="pre">retry_proxy</span></code> instance; that proxy in turn wraps
requests on behalf of the original dogpile.cache.pylibmc backend.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.4.4: </span>Added support for the <a class="reference internal" href="api.html#dogpile.cache.proxy.ProxyBackend" title="dogpile.cache.proxy.ProxyBackend"><code class="xref py py-class docutils literal"><span class="pre">ProxyBackend</span></code></a> class.</p>
</div>
</div>
<div class="section" id="recipes">
<h2>Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="invalidating-a-group-of-related-keys">
<h3>Invalidating a group of related keys<a class="headerlink" href="#invalidating-a-group-of-related-keys" title="Permalink to this headline">¶</a></h3>
<p>This recipe presents a way to track the cache keys related to a particular region,
for the purposes of invalidating a series of keys that relate to a particular id.</p>
<p>Three cached functions, <code class="docutils literal"><span class="pre">user_fn_one()</span></code>, <code class="docutils literal"><span class="pre">user_fn_two()</span></code>, <code class="docutils literal"><span class="pre">user_fn_three()</span></code>
each perform a different function based on a <code class="docutils literal"><span class="pre">user_id</span></code> integer value. The
region applied to cache them uses a custom key generator which tracks each cache
key generated, pulling out the integer “id” and replacing with a template.</p>
<p>When all three functions have been called, the key generator is now aware of
these three keys: <code class="docutils literal"><span class="pre">user_fn_one_%d</span></code>, <code class="docutils literal"><span class="pre">user_fn_two_%d</span></code>, and
<code class="docutils literal"><span class="pre">user_fn_three_%d</span></code>. The <code class="docutils literal"><span class="pre">invalidate_user_id()</span></code> function then knows that
for a particular <code class="docutils literal"><span class="pre">user_id</span></code>, it needs to hit all three of those keys
in order to invalidate everything having to do with that id.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">count</span>
<span class="n">user_keys</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">my_key_generator</span><span class="p">(</span><span class="n">namespace</span><span class="p">,</span> <span class="n">fn</span><span class="p">):</span>
<span class="n">fname</span> <span class="o">=</span> <span class="n">fn</span><span class="o">.</span><span class="n">__name__</span>
<span class="k">def</span> <span class="nf">generate_key</span><span class="p">(</span><span class="o">*</span><span class="n">arg</span><span class="p">):</span>
<span class="c1"># generate a key template:</span>
<span class="c1"># "fname_%d_arg1_arg2_arg3..."</span>
<span class="n">key_template</span> <span class="o">=</span> <span class="n">fname</span> <span class="o">+</span> <span class="s2">"_"</span> <span class="o">+</span> \
<span class="s2">"</span><span class="si">%d</span><span class="s2">"</span> <span class="o">+</span> \
<span class="s2">"_"</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="c1"># store key template</span>
<span class="n">user_keys</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">key_template</span><span class="p">)</span>
<span class="c1"># return cache key</span>
<span class="n">user_id</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="k">return</span> <span class="n">key_template</span> <span class="o">%</span> <span class="n">user_id</span>
<span class="k">return</span> <span class="n">generate_key</span>
<span class="k">def</span> <span class="nf">invalidate_user_id</span><span class="p">(</span><span class="n">region</span><span class="p">,</span> <span class="n">user_id</span><span class="p">):</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">user_keys</span><span class="p">:</span>
<span class="n">region</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">key</span> <span class="o">%</span> <span class="n">user_id</span><span class="p">)</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">(</span>
<span class="n">function_key_generator</span><span class="o">=</span><span class="n">my_key_generator</span>
<span class="p">)</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s2">"dogpile.cache.memory"</span>
<span class="p">)</span>
<span class="n">counter</span> <span class="o">=</span> <span class="n">count</span><span class="p">()</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">user_fn_one</span><span class="p">(</span><span class="n">user_id</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"user fn one: </span><span class="si">%d</span><span class="s2">, </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">counter</span><span class="p">),</span> <span class="n">user_id</span><span class="p">)</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">user_fn_two</span><span class="p">(</span><span class="n">user_id</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"user fn two: </span><span class="si">%d</span><span class="s2">, </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">counter</span><span class="p">),</span> <span class="n">user_id</span><span class="p">)</span>
<span class="nd">@region.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">user_fn_three</span><span class="p">(</span><span class="n">user_id</span><span class="p">):</span>
<span class="k">return</span> <span class="s2">"user fn three: </span><span class="si">%d</span><span class="s2">, </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">counter</span><span class="p">),</span> <span class="n">user_id</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_one</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_two</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_three</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_two</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="n">invalidate_user_id</span><span class="p">(</span><span class="n">region</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="k">print</span> <span class="s2">"invalidated:"</span>
<span class="k">print</span> <span class="n">user_fn_one</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_two</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_three</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="k">print</span> <span class="n">user_fn_two</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="asynchronous-data-updates-with-orm-events">
<h3>Asynchronous Data Updates with ORM Events<a class="headerlink" href="#asynchronous-data-updates-with-orm-events" title="Permalink to this headline">¶</a></h3>
<p>This recipe presents one technique of optimistically pushing new data
into the cache when an update is sent to a database.</p>
<p>Using SQLAlchemy for database querying, suppose a simple cache-decorated
function returns the results of a database query:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@region.cache_on_arguments</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">get_some_data</span><span class="p">(</span><span class="n">argument</span><span class="p">):</span>
<span class="c1"># query database to get data</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">Session</span><span class="p">()</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">DBClass</span><span class="p">)</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">DBClass</span><span class="o">.</span><span class="n">argument</span> <span class="o">==</span> <span class="n">argument</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="k">return</span> <span class="n">data</span>
</pre></div>
</div>
<p>We would like this particular function to be re-queried when the data
has changed. We could call <code class="docutils literal"><span class="pre">get_some_data.invalidate(argument,</span> <span class="pre">hard=False)</span></code>
at the point at which the data changes, however this only
leads to the invalidation of the old value; a new value is not generated until
the next call, and also means at least one client has to block while the
new value is generated. We could also call
<code class="docutils literal"><span class="pre">get_some_data.refresh(argument)</span></code>, which would perform the data refresh
at that moment, but then the writer is delayed by the re-query.</p>
<p>A third variant is to instead offload the work of refreshing for this query
into a background thread or process. This can be acheived using
a system such as the <a class="reference internal" href="api.html#dogpile.cache.region.CacheRegion.params.async_creation_runner" title="dogpile.cache.region.CacheRegion"><code class="xref py py-paramref docutils literal"><span class="pre">CacheRegion.async_creation_runner</span></code></a>.
However, an expedient approach for smaller use cases is to link cache refresh
operations to the ORM session’s commit, as below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">event</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">Session</span>
<span class="k">def</span> <span class="nf">cache_refresh</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">refresher</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="sd">"""</span>
<span class="sd"> Refresh the functions cache data in a new thread. Starts refreshing only</span>
<span class="sd"> after the session was committed so all database data is available.</span>
<span class="sd"> """</span>
<span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">Session</span><span class="p">),</span> \
<span class="s2">"Need a session, not a sessionmaker or scoped_session"</span>
<span class="nd">@event.listens_for</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="s2">"after_commit"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">do_refresh</span><span class="p">(</span><span class="n">session</span><span class="p">):</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">refresher</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">t</span><span class="o">.</span><span class="n">daemon</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">t</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>Within a sequence of data persistence, <code class="docutils literal"><span class="pre">cache_refresh</span></code> can be called
given a particular SQLAlchemy <code class="docutils literal"><span class="pre">Session</span></code> and a callable to do the work:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">add_new_data</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">argument</span><span class="p">):</span>
<span class="c1"># add some data</span>
<span class="n">session</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">something_new</span><span class="p">(</span><span class="n">argument</span><span class="p">))</span>
<span class="c1"># add a hook to refresh after the Session is committed.</span>
<span class="n">cache_refresh</span><span class="p">(</span><span class="n">session</span><span class="p">,</span> <span class="n">get_some_data</span><span class="o">.</span><span class="n">refresh</span><span class="p">,</span> <span class="n">argument</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that the event to refresh the data is associated with the <code class="docutils literal"><span class="pre">Session</span></code>
being used for persistence; however, the actual refresh operation is called
with a <strong>different</strong> <code class="docutils literal"><span class="pre">Session</span></code>, typically one that is local to the refresh
operation, either through a thread-local registry or via direct instantiation.</p>
</div>
<div class="section" id="prefixing-all-keys-in-redis">
<h3>Prefixing all keys in Redis<a class="headerlink" href="#prefixing-all-keys-in-redis" title="Permalink to this headline">¶</a></h3>
<p>If you use a redis instance as backend that contains other keys besides the ones
set by dogpile.cache, it is a good idea to uniquely prefix all dogpile.cache
keys, to avoid potential collisions with keys set by your own code. This can
easily be done using a key mangler function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache</span> <span class="kn">import</span> <span class="n">make_region</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">(</span>
<span class="n">key_mangler</span><span class="o">=</span><span class="k">lambda</span> <span class="n">key</span><span class="p">:</span> <span class="s2">"myapp:dogpile:"</span> <span class="o">+</span> <span class="n">key</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="encoding-decoding-data-into-another-format">
<h3>Encoding/Decoding data into another format<a class="headerlink" href="#encoding-decoding-data-into-another-format" title="Permalink to this headline">¶</a></h3>
<div class="sidebar">
<p class="first sidebar-title">A Note on Data Encoding</p>
<p class="last">Under the hood, dogpile.cache wraps cached data in an instance of
<code class="docutils literal"><span class="pre">dogpile.cache.api.CachedValue</span></code> and then pickles that data for storage
along with some bookkeeping metadata. If you implement a ProxyBackend to
encode/decode data, that transformation will happen on the pre-pickled data-
dogpile does not store the data ‘raw’ and will still pass a pickled payload
to the backend. This behavior can negate the hopeful improvements of some
encoding schemes.</p>
</div>
<p>Since dogpile is managing cached data, you may be concerned with the size of
your payloads. A possible method of helping minimize payloads is to use a
ProxyBackend to recode the data on-the-fly or otherwise transform data as it
enters or leaves persistent storage.</p>
<p>In the example below, we define 2 classes to implement msgpack encoding. Msgpack
(<a class="reference external" href="http://msgpack.org/">http://msgpack.org/</a>) is a serialization format that works exceptionally well
with json-like data and can serialize nested dicts into a much smaller payload
than Python’s own pickle. <code class="docutils literal"><span class="pre">_EncodedProxy</span></code> is our base class
for building data encoders, and inherits from dogpile’s own <cite>ProxyBackend</cite>. You
could just use one class. This class passes 4 of the main <cite>key/value</cite> functions
into a configurable decoder and encoder. The <code class="docutils literal"><span class="pre">MsgpackProxy</span></code> class simply
inherits from <code class="docutils literal"><span class="pre">_EncodedProxy</span></code> and implements the necessary <code class="docutils literal"><span class="pre">value_decode</span></code>
and <code class="docutils literal"><span class="pre">value_encode</span></code> functions.</p>
<p>Encoded ProxyBackend Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">dogpile.cache.proxy</span> <span class="kn">import</span> <span class="n">ProxyBackend</span>
<span class="kn">import</span> <span class="nn">msgpack</span>
<span class="k">class</span> <span class="nc">_EncodedProxy</span><span class="p">(</span><span class="n">ProxyBackend</span><span class="p">):</span>
<span class="sd">"""base class for building value-mangling proxies"""</span>
<span class="k">def</span> <span class="nf">value_decode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">"override me"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">value_encode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">"override me"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">set</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">v</span><span class="p">):</span>
<span class="n">v</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_encode</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">proxied</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</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">v</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxied</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="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_decode</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">set_multi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mapping</span><span class="p">):</span>
<span class="sd">"""encode to a new dict to preserve unencoded values in-place when</span>
<span class="sd"> called by `get_or_create_multi`</span>
<span class="sd"> """</span>
<span class="n">mapping_set</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">mapping</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
<span class="n">mapping_set</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_encode</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxied</span><span class="o">.</span><span class="n">set_multi</span><span class="p">(</span><span class="n">mapping_set</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_multi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">keys</span><span class="p">):</span>
<span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">proxied</span><span class="o">.</span><span class="n">get_multi</span><span class="p">(</span><span class="n">keys</span><span class="p">)</span>
<span class="n">translated</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">record</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">translated</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">value_decode</span><span class="p">(</span><span class="n">record</span><span class="p">))</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">raise</span>
<span class="k">return</span> <span class="n">translated</span>
<span class="k">class</span> <span class="nc">MsgpackProxy</span><span class="p">(</span><span class="n">_EncodedProxy</span><span class="p">):</span>
<span class="sd">"""custom decode/encode for value mangling"""</span>
<span class="k">def</span> <span class="nf">value_decode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">v</span> <span class="ow">or</span> <span class="n">v</span> <span class="ow">is</span> <span class="n">NO_VALUE</span><span class="p">:</span>
<span class="k">return</span> <span class="n">NO_VALUE</span>
<span class="c1"># you probably want to specify a custom decoder via `object_hook`</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">msgpack</span><span class="o">.</span><span class="n">unpackb</span><span class="p">(</span><span class="n">payload</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">"utf-8"</span><span class="p">)</span>
<span class="k">return</span> <span class="n">CachedValue</span><span class="p">(</span><span class="o">*</span><span class="n">v</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">value_encode</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
<span class="c1"># you probably want to specify a custom encoder via `default`</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">msgpack</span><span class="o">.</span><span class="n">packb</span><span class="p">(</span><span class="n">payload</span><span class="p">,</span> <span class="n">use_bin_type</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">return</span> <span class="n">v</span>
<span class="c1"># extend our region configuration from above with a 'wrap'</span>
<span class="n">region</span> <span class="o">=</span> <span class="n">make_region</span><span class="p">()</span><span class="o">.</span><span class="n">configure</span><span class="p">(</span>
<span class="s1">'dogpile.cache.pylibmc'</span><span class="p">,</span>
<span class="n">expiration_time</span> <span class="o">=</span> <span class="mi">3600</span><span class="p">,</span>
<span class="n">arguments</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">'url'</span><span class="p">:</span> <span class="p">[</span><span class="s2">"127.0.0.1"</span><span class="p">],</span>
<span class="p">},</span>
<span class="n">wrap</span> <span class="o">=</span> <span class="p">[</span><span class="n">MsgpackProxy</span><span class="p">,</span> <span class="p">]</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Usage Guide</a><ul>
<li><a class="reference internal" href="#overview">Overview</a></li>
<li><a class="reference internal" href="#rudimentary-usage">Rudimentary Usage</a></li>
<li><a class="reference internal" href="#region-configuration">Region Configuration</a></li>
<li><a class="reference internal" href="#using-a-region">Using a Region</a></li>
<li><a class="reference internal" href="#creating-backends">Creating Backends</a></li>
<li><a class="reference internal" href="#changing-backend-behavior">Changing Backend Behavior</a></li>
<li><a class="reference internal" href="#recipes">Recipes</a><ul>
<li><a class="reference internal" href="#invalidating-a-group-of-related-keys">Invalidating a group of related keys</a></li>
<li><a class="reference internal" href="#asynchronous-data-updates-with-orm-events">Asynchronous Data Updates with ORM Events</a></li>
<li><a class="reference internal" href="#prefixing-all-keys-in-redis">Prefixing all keys in Redis</a></li>
<li><a class="reference internal" href="#encoding-decoding-data-into-another-format">Encoding/Decoding data into another format</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="front.html"
title="previous chapter">Front Matter</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="api.html"
title="next chapter">API</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/usage.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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 class="right" >
<a href="api.html" title="API"
>next</a> |</li>
<li class="right" >
<a href="front.html" title="Front Matter"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">dogpile.cache 0.5.7 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2011-2015 Mike Bayer.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
</div>
</body>
</html>
|