Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty result sets should not be fetched again #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion flask_sqlalchemy_cache/core.py
Expand Up @@ -79,7 +79,7 @@ def get_value(self, merge=True, createfunc=None,
ignore_expiration=ignore_expiration)
else:
cached_value = cache.get(cache_key)
if not cached_value:
if cached_value is None:
cached_value = createfunc()
cache.set(cache_key, cached_value, timeout=expiration_time)

Expand Down
15 changes: 12 additions & 3 deletions flask_sqlalchemy_cache/tests/test_minimal.py
@@ -1,10 +1,12 @@
# coding: UTF-8
import unittest
from unittest.mock import Mock, MagicMock

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy, Model
from flask.ext.cache import Cache
from flask.ext.sqlalchemy_cache import CachingQuery, FromCache
from flask_cache import Cache
from flask_sqlalchemy import SQLAlchemy, Model
from flask_sqlalchemy_cache import CachingQuery, FromCache


Model.query_class = CachingQuery

Expand Down Expand Up @@ -69,6 +71,13 @@ def test_no_results(self):
# regression test (check #3) to handle zero results gracefully
Country.query.filter_by(name="URSS").options(FromCache(cache)).all()

def test_empty_results(self):
# An empty result set should not trigger another set
mock_cache = Mock()
mock_cache.get = MagicMock(return_value=[])
Country.query.filter_by(name="URSS").options(FromCache(mock_cache)).all()
mock_cache.set.assert_not_called()

def test_special_chars(self):
unicode_name = u"Côte d'Ivoire"
unicode_country = Country(unicode_name)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Expand Up @@ -4,7 +4,7 @@

setup(
name='Flask-SQLAlchemy-Cache',
version='0.1.5',
version='0.1.6',
description='CachingQuery implementation to Flask using Flask-SQLAlchemy and Flask-Cache',
author='Iuri de Silvio',
author_email='iurisilvio@gmail.com',
Expand All @@ -13,7 +13,7 @@
platforms='any',
packages=['flask_sqlalchemy_cache'],
install_requires=[
'Flask>=0.9',
'Flask >=0.12, <1',
'Flask-Cache',
'Flask-SQLAlchemy',
],
Expand Down