Repository: mrf345/flask_minify

Issue:
Cache KeyError
In my code, I've been using `flask_minify`, and I've been disabling the cache (for dynamic reasons). When I saw [this PR](https://github.com/KTibow/scorecard/pull/17) from Dependabot on my repo, I saw that the checks passed, so I merged it in. Whenever I tried to connect to the server, though, it gave me this exception:
```
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
     response = self.full_dispatch_request()
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
     return self.finalize_request(rv)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
     'style'))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
     response = self.full_dispatch_request()
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
     return self.finalize_request(rv)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
     'style'))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 266, in main
     self.html))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'
```
What's going on here? How can I fix it?

Discussion:

Patch:
diff --git a/README.md b/README.md
index 45cbb75..a4efb7f 100644
--- a/README.md
+++ b/README.md
@@ -61,24 +61,24 @@ def __init__(
 
         Parameters
         ----------
-            app: Flask.app
-                Flask app instance to be passed.
-            js: bool
-                To minify the js output.
-            cssless: bool
-                To minify spaces in css.
-            fail_safe: bool
-                to avoid raising error while minifying.
-            bypass: list
-                list of endpoints to bypass minifying for. (Regex)
-            bypass_caching: list
-                list of endpoints to bypass caching for. (Regex)
-            caching_limit: int
-                to limit the number of minified response variations.
-            passive: bool
-                to disable active minifying.
-            static: bool
-                to enable minifying static files css, less and js.
+        app: Flask.app
+            Flask app instance to be passed.
+        js: bool
+            To minify the js output.
+        cssless: bool
+            To minify spaces in css.
+        fail_safe: bool
+            to avoid raising error while minifying.
+        bypass: list
+            list of endpoints to bypass minifying for. (Regex)
+        bypass_caching: list
+            list of endpoints to bypass caching for. (Regex)
+        caching_limit: int
+            to limit the number of minified response variations.
+        passive: bool
+            to disable active minifying.
+        static: bool
+            to enable minifying static files css, less and js.
 
         Notes
         -----
@@ -103,5 +103,5 @@ def __init__(
 ```
 
 ## Development:
-- **Run Tests:** `pytest`
-- **Run style checks:** `flake8`
+- **Run Tests:** `flake8`
+- **Run style checks:** `pytest`
diff --git a/flask_minify/about.py b/flask_minify/about.py
index 73d12bb..fcef128 100644
--- a/flask_minify/about.py
+++ b/flask_minify/about.py
@@ -1,4 +1,4 @@
-__version__ = '0.22'
+__version__ = '0.23'
 __doc__ = 'Flask extension to minify html, css, js and less.'
 __license__ = 'MIT'
 __author__ = 'Mohamed Feddad'
diff --git a/flask_minify/main.py b/flask_minify/main.py
index dd41132..fc937f2 100644
--- a/flask_minify/main.py
+++ b/flask_minify/main.py
@@ -202,13 +202,15 @@ class Minify(object):
         str
             stored or restored minifed content.
         '''
+        def _cache_dict():
+            return self.cache.get(self.endpoint, {})
+
         key = hashing(content).hexdigest()
         bypassed = bool(self.get_endpoint_matches(self.bypass_caching))
-        limit_reached = len(self.cache.get(self.endpoint,
-                                           {})) >= self.caching_limit
+        limit_reached = len(_cache_dict()) >= self.caching_limit
 
         def _cached():
-            return self.cache.get(self.endpoint, {}).get(key)
+            return _cache_dict().get(key)
 
         def _minified():
             return self.get_minified(content,
@@ -217,7 +219,9 @@ class Minify(object):
                                      ) if minify else content
 
         if not _cached() and not bypassed:
-            limit_reached and self.cache[self.endpoint].popitem()
+            if limit_reached and _cache_dict():
+                _cache_dict().popitem()
+
             self.cache\
                 .setdefault(self.endpoint, {})\
                 .update({key: _minified()})


Test patch:
diff --git a/tests/integration.py b/tests/integration.py
index e10811f..78bb5f0 100644
--- a/tests/integration.py
+++ b/tests/integration.py
@@ -13,7 +13,7 @@ def client():
     store_minify.fail_safe = False
     store_minify.cssless = True
     store_minify.js = True
-    store_minify.caching_limit = 1
+    store_minify.caching_limit = 0
     store_minify.bypass_caching = []
     store_minify.cache = {}
     store_minify.passive = False