Skip to content

Commit 7952b34

Browse files
committed
Refactor backends
1 parent e558847 commit 7952b34

25 files changed

+1581
-1557
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ dist/
1010
.coverage
1111
.cache
1212
.xprocess
13+
.vscode

LICENSE

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,40 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3030
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3131
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3232
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
34+
35+
The "cache" module from werkzeug is licensed under a BSD-3 Clause license as is
36+
stated below:
37+
38+
Copyright (c) 2017, Pallets Team
39+
40+
All rights reserved.
41+
42+
43+
Redistribution and use in source and binary forms, with or without
44+
modification, are permitted provided that the following conditions are
45+
met:
46+
47+
* Redistributions of source code must retain the above copyright notice,
48+
this list of conditions and the following disclaimer.
49+
50+
* Redistributions in binary form must reproduce the above copyright
51+
notice, this list of conditions and the following disclaimer in the
52+
documentation and/or other materials provided with the distribution.
53+
54+
* Neither the name of the copyright holder nor the names of its
55+
contributors may be used to endorse or promote products derived from
56+
this software without specific prior written permission.
57+
58+
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
59+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
60+
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
61+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
62+
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
63+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
65+
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
66+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68+
THIS SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF
69+
SUCH DAMAGE.

MANIFEST.in

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
include LICENSE CHANGES CONTRIBUTORS requirements.txt tox.ini
2-
include test_cache.py test_template.html
3-
graft docs *
4-
recursive-exclude docs *.pyc
5-
recursive-exclude docs *.pyo
1+
include LICENSE
2+
include CONTRIBUTORS
3+
include CHANGES
4+
include README.md
5+
include requirements.txt
6+
include tox.ini
7+
include setup.cfg
8+
9+
graft docs
10+
graft tests
11+
612
prune docs/_build
13+
14+
global-exclude __pycache__
15+
global-exclude *.py[co]
16+
global-exclude *.sw[a-z]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: text tox clean wheel upload docs
1+
.PHONY: test tox clean wheel upload docs
22

33
test:
44
pytest

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Adds easy cache support to Flask.
1212
This is a fork of the [Flask-Cache](https://github.com/thadeusb/flask-cache)
1313
extension.
1414

15+
Flask-Caching also includes the ``cache`` module from werkzeug licensed under a
16+
BSD-3 Clause License.
17+
1518

1619
Setup
1720
-----

docs/index.rst

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Flask-Caching is an extension to `Flask`_ that adds caching support for
77
various backends to any Flask application. Besides providing support for all
88
`werkzeug`_'s original caching backends through a uniformed API,
99
it is also possible to develop your own caching backend by subclassing
10-
:class:`flask_caching.backends.cache.BaseCache` class.
10+
:class:`flask_caching.backends.base.BaseCache` class.
1111

1212

1313
Installation
@@ -279,9 +279,9 @@ The following configuration values exist for Flask-Caching:
279279
* **simple**: SimpleCache
280280
* **filesystem**: FileSystemCache
281281
* **redis**: RedisCache (redis required)
282+
* **redissentinel**: RedisSentinelCache (redis required)
282283
* **uwsgi**: UWSGICache (uwsgi required)
283284
* **memcached**: MemcachedCache (pylibmc or memcache required)
284-
* **gaememcached**: GAEMemcachedCache
285285
* **saslmemcached**: SASLMemcachedCache (pylibmc required)
286286
* **spreadsaslmemcached**: SpreadSASLMemcachedCache (pylibmc required)
287287

@@ -301,8 +301,7 @@ The following configuration values exist for Flask-Caching:
301301
``CACHE_KEY_PREFIX`` A prefix that is added before all keys.
302302
This makes it possible to use the same
303303
memcached server for different apps.
304-
Used only for RedisCache, MemcachedCache and
305-
GAEMemcachedCache.
304+
Used only for RedisCache and MemcachedCache
306305
``CACHE_UWSGI_NAME`` The name of the uwsgi caching instance to
307306
connect to, for example: mycache@localhost:3031,
308307
defaults to an empty string, which means uWSGI
@@ -389,11 +388,11 @@ Set ``CACHE_TYPE`` to ``redis`` to use this type.
389388

390389
- CACHE_DEFAULT_TIMEOUT
391390
- CACHE_KEY_PREFIX
391+
- CACHE_OPTIONS
392392
- CACHE_REDIS_HOST
393393
- CACHE_REDIS_PORT
394394
- CACHE_REDIS_PASSWORD
395395
- CACHE_REDIS_DB
396-
- CACHE_OPTIONS
397396
- CACHE_REDIS_URL
398397

399398
Entries in CACHE_OPTIONS are passed to the redis client as ``**kwargs``
@@ -403,10 +402,10 @@ RedisSentinelCache
403402

404403
Set ``CACHE_TYPE`` to ``redissentinel`` to use this type.
405404

405+
- CACHE_KEY_PREFIX
406406
- CACHE_REDIS_SENTINELS
407407
- CACHE_REDIS_SENTINEL_MASTER
408408
- CACHE_REDIS_PASSWORD
409-
- CACHE_KEY_PREFIX
410409
- CACHE_REDIS_DB
411410

412411
Entries in CACHE_OPTIONS are passed to the redis client as ``**kwargs``
@@ -442,13 +441,6 @@ Relevant configuration values
442441

443442
Alternatively, see `Custom Cache Backends`_.
444443

445-
GAEMemcachedCache
446-
`````````````````
447-
448-
Set ``CACHE_TYPE`` to ``gaememcached`` to use this type.
449-
450-
Is MemcachedCache under a different name.
451-
452444
SASLMemcachedCache
453445
``````````````````
454446

@@ -462,10 +454,10 @@ Relevant configuration values
462454

463455
- CACHE_DEFAULT_TIMEOUT
464456
- CACHE_KEY_PREFIX
457+
- CACHE_OPTIONS
465458
- CACHE_MEMCACHED_SERVERS
466459
- CACHE_MEMCACHED_USERNAME
467460
- CACHE_MEMCACHED_PASSWORD
468-
- CACHE_OPTIONS
469461

470462
.. note:: Since the SASL Memcached cache types do not use werkzeug's original built-in cache
471463
infrastructure, they can be configured with CACHE_OPTIONS.

flask_caching/__init__.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from flask import current_app, request, url_for
2222
from werkzeug.utils import import_string
2323

24-
from ._compat import PY2, iteritems
24+
from flask_caching._compat import PY2, iteritems
2525

2626
__version__ = "1.4.0"
2727

@@ -198,7 +198,7 @@ def init_app(self, app, config=None):
198198
and not config["CACHE_NO_NULL_WARNING"]
199199
):
200200
warnings.warn(
201-
"Flask-Cache: CACHE_TYPE is set to null, "
201+
"Flask-Caching: CACHE_TYPE is set to null, "
202202
"caching is effectively disabled."
203203
)
204204

@@ -219,7 +219,7 @@ def _set_cache(self, app, config):
219219
cache_obj = getattr(backends, import_me)
220220
except AttributeError:
221221
raise ImportError(
222-
"%s is not a valid FlaskCache backend" % (import_me)
222+
"%s is not a valid Flask-Caching backend" % (import_me)
223223
)
224224
else:
225225
cache_obj = import_string(import_me)
@@ -411,8 +411,13 @@ def make_cache_key(*args, **kwargs):
411411
# Convert non-keyword arguments (which is the way
412412
# `make_cache_key` expects them) to keyword arguments
413413
# (the way `url_for` expects them)
414-
argspec = inspect.getargspec(f)
415-
for arg_name, arg in zip(argspec.args, args):
414+
try:
415+
# Python >= 3.0
416+
argspec_args = inspect.getfullargspec(f).args
417+
except AttributeError:
418+
argspec_args = inspect.getargspec(f).args
419+
420+
for arg_name, arg in zip(argspec_args, args):
416421
kwargs[arg_name] = arg
417422

418423
return _make_cache_key(args, kwargs, use_request=False)
@@ -619,7 +624,7 @@ def _memoize_kwargs_to_args(self, f, *args, **kwargs):
619624

620625
new_args.append(arg)
621626

622-
new_args.extend(args[len(arg_names) :])
627+
new_args.extend(args[len(arg_names):])
623628
return (
624629
tuple(new_args),
625630
OrderedDict(
@@ -639,7 +644,11 @@ def _bypass_cache(self, unless, f, *args, **kwargs):
639644
bypass_cache = False
640645

641646
if callable(unless):
642-
argspec = inspect.getargspec(unless)
647+
try:
648+
# Python >= 3.0
649+
argspec = inspect.getfullargspec(unless)
650+
except AttributeError:
651+
argspec = inspect.getargspec(unless)
643652

644653
# If unless() takes args, pass them in.
645654
if len(argspec.args) > 0 and argspec.varargs and argspec.keywords:
@@ -714,6 +723,7 @@ def big_foo(a, b):
714723
renewal of cached functions.
715724
:param hash_method: Default hashlib.md5. The hash method used to
716725
generate the keys for cached results.
726+
717727
.. versionadded:: 0.5
718728
params ``make_name``, ``unless``
719729
"""

flask_caching/_compat.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,43 @@
2121
iterkeys = lambda d: iter(d.keys())
2222
itervalues = lambda d: iter(d.values())
2323
iteritems = lambda d: iter(d.items())
24+
25+
text_type = str
26+
string_types = (str, )
27+
integer_types = (int, )
28+
29+
def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
30+
if x is None:
31+
return None
32+
if isinstance(x, (bytes, bytearray, memoryview)): # noqa
33+
return bytes(x)
34+
if isinstance(x, str):
35+
return x.encode(charset, errors)
36+
raise TypeError('Expected bytes')
37+
38+
def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
39+
if x is None or isinstance(x, str):
40+
return x
41+
return x.decode(charset, errors)
2442
else:
2543
range_type = xrange
2644
iterkeys = lambda d: d.iterkeys()
2745
itervalues = lambda d: d.itervalues()
2846
iteritems = lambda d: d.iteritems()
47+
text_type = unicode
48+
string_types = (str, unicode)
49+
integer_types = (int, long)
50+
51+
def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
52+
if x is None:
53+
return None
54+
if isinstance(x, (bytes, bytearray, buffer)):
55+
return bytes(x)
56+
if isinstance(x, unicode):
57+
return x.encode(charset, errors)
58+
raise TypeError('Expected bytes')
59+
60+
def to_native(x, charset=sys.getdefaultencoding(), errors='strict'):
61+
if x is None or isinstance(x, str):
62+
return x
63+
return x.encode(charset, errors)

0 commit comments

Comments
 (0)