Repository: pallets/werkzeug
Pull request #1394 (2018-11-12 20:26:25)

Cache deprecation warning
Creates a deprecation warning when a cache instance is created.

Issue: #1249 


Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 134f7040..120a832a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -80,6 +80,10 @@ Unreleased
     (`#1390`_)
 -   In :func:`~security.generate_password_hash`, PBKDF2 uses 150000
     iterations by default, increased from 50000. (`#1377`_)
+-   ``werkzeug.contrib.cache`` issues a deprecation warning. If you are
+    using Flask you can use Flask-Caching. If you are using Werkzeug you
+    are welcome to extract the code (see LICENSE) into a separate
+    project. (`#1249`_)
 
 .. _`#209`: https://github.com/pallets/werkzeug/pull/209
 .. _`#609`: https://github.com/pallets/werkzeug/pull/609
@@ -96,6 +100,7 @@ Unreleased
 .. _`#1233`: https://github.com/pallets/werkzeug/pull/1233
 .. _`#1237`: https://github.com/pallets/werkzeug/pull/1237
 .. _`#1245`: https://github.com/pallets/werkzeug/pull/1245
+.. _`#1249`: https://github.com/pallets/werkzeug/issues/1249
 .. _`#1252`: https://github.com/pallets/werkzeug/pull/1252
 .. _`#1255`: https://github.com/pallets/werkzeug/pull/1255
 .. _`#1259`: https://github.com/pallets/werkzeug/pull/1259
diff --git a/werkzeug/contrib/__init__.py b/werkzeug/contrib/__init__.py
index 3e572ebe..8edee7fd 100644
--- a/werkzeug/contrib/__init__.py
+++ b/werkzeug/contrib/__init__.py
@@ -14,3 +14,11 @@
     :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
     :license: BSD, see LICENSE for more details.
 """
+import warnings
+
+
+class WerkzeugContribDeprecationWarning(DeprecationWarning):
+    pass
+
+
+warnings.simplefilter("module", WerkzeugContribDeprecationWarning)
diff --git a/werkzeug/contrib/cache.py b/werkzeug/contrib/cache.py
index fd704390..271d53f4 100644
--- a/werkzeug/contrib/cache.py
+++ b/werkzeug/contrib/cache.py
@@ -61,6 +61,7 @@ import re
 import errno
 import tempfile
 import platform
+import warnings
 from hashlib import md5
 from time import time
 try:
@@ -70,6 +71,7 @@ except ImportError:  # pragma: no cover
 
 from werkzeug._compat import iteritems, string_types, text_type, \
     integer_types, to_native
+from werkzeug.contrib import WerkzeugContribDeprecationWarning
 from werkzeug.posixemulation import rename
 
 
@@ -100,6 +102,12 @@ class BaseCache(object):
     """
 
     def __init__(self, default_timeout=300):
+        warnings.warn(
+            'Werkzeug cache is deprecated as of version 0.15 and will'
+            ' be removed in version 1.0.',
+            WerkzeugContribDeprecationWarning,
+            stacklevel=3
+        )
         self.default_timeout = default_timeout
 
     def _normalize_timeout(self, timeout):


Test patch:
diff --git a/tests/contrib/cache/test_cache.py b/tests/contrib/cache/test_cache.py
index 4bd034e3..2d382919 100644
--- a/tests/contrib/cache/test_cache.py
+++ b/tests/contrib/cache/test_cache.py
@@ -13,7 +13,7 @@ import errno
 import pytest
 
 from werkzeug._compat import text_type
-from werkzeug.contrib import cache
+from werkzeug.contrib import WerkzeugContribDeprecationWarning, cache
 
 try:
     import redis
@@ -61,6 +61,10 @@ class CacheTestsBase(object):
 
 
 class GenericCacheTests(CacheTestsBase):
+    def test_deprecation_warning(self):
+        with pytest.warns(WerkzeugContribDeprecationWarning, match='cache is deprecated'):
+            cache.BaseCache()
+
     def test_generic_get_dict(self, c):
         assert c.set('a', 'a')
         assert c.set('b', 'b')
@@ -152,13 +156,14 @@ class TestSimpleCache(GenericCacheTests):
         return cache.SimpleCache
 
     def test_purge(self):
-        c = cache.SimpleCache(threshold=2)
-        c.set('a', 'a')
-        c.set('b', 'b')
-        c.set('c', 'c')
-        c.set('d', 'd')
-        # Cache purges old items *before* it sets new ones.
-        assert len(c._cache) == 3
+        with pytest.warns(WerkzeugContribDeprecationWarning, match='cache is deprecated'):
+            c = cache.SimpleCache(threshold=2)
+            c.set('a', 'a')
+            c.set('b', 'b')
+            c.set('c', 'c')
+            c.set('d', 'd')
+            # Cache purges old items *before* it sets new ones.
+            assert len(c._cache) == 3
 
 
 class TestFileSystemCache(GenericCacheTests):
@@ -168,7 +173,8 @@ class TestFileSystemCache(GenericCacheTests):
 
     def test_filesystemcache_prune(self, make_cache):
         THRESHOLD = 13
-        c = make_cache(threshold=THRESHOLD)
+        with pytest.warns(WerkzeugContribDeprecationWarning, match='cache is deprecated'):
+            c = make_cache(threshold=THRESHOLD)
 
         for i in range(2 * THRESHOLD):
             assert c.set(str(i), i)
@@ -188,7 +194,8 @@ class TestFileSystemCache(GenericCacheTests):
 
     def test_no_threshold(self, make_cache):
         THRESHOLD = 0
-        c = make_cache(threshold=THRESHOLD)
+        with pytest.warns(WerkzeugContribDeprecationWarning, match='cache is deprecated'):
+            c = make_cache(threshold=THRESHOLD)
 
         for i in range(10):
             assert c.set(str(i), i)