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

Hard code our cache values instead of configure them #209

Merged
merged 1 commit into from
Feb 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions dev/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ paths:
urls:
documentation: "http://pythonhosted.org/"

cache:
browser:
index: 1
simple: 1
packages: 1
project_detail: 1
user_profile: 1
legacy_json: 1
legacy_rss: 1
varnish:
index: 120
simple: 120
packages: 120
project_detail: 120
user_profile: 120
legacy_json: 120
legacy_rss: 120


security:
csp:
default-src:
Expand Down
14 changes: 12 additions & 2 deletions tests/legacy/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@


def test_index(monkeypatch):
response = pretend.stub(status_code=200, headers=Headers())
response = pretend.stub(
status_code=200,
headers=Headers(),
cache_control=pretend.stub(),
surrogate_control=pretend.stub(),
)
render = pretend.call_recorder(lambda *a, **k: response)
monkeypatch.setattr(simple, "render_response", render)

Expand Down Expand Up @@ -92,7 +97,12 @@ def test_index(monkeypatch):
)
def test_project(project_name, hosting_mode, release_urls,
e_project_urls, monkeypatch):
response = pretend.stub(status_code=200, headers=Headers())
response = pretend.stub(
status_code=200,
headers=Headers(),
cache_control=pretend.stub(),
surrogate_control=pretend.stub(),
)
render = pretend.call_recorder(lambda *a, **k: response)
url_for = lambda *a, **k: "/foo/"

Expand Down
40 changes: 19 additions & 21 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,19 @@ def test_render_response():
]


@pytest.mark.parametrize(("browser", "varnish", "status"), [
({}, {}, 200),
({"test": 120}, {}, 200),
({}, {"test": 120}, 200),
({"test": 120}, {"test": 120}, 200),
({}, {}, 400),
({"test": 120}, {}, 400),
({}, {"test": 120}, 400),
({"test": 120}, {"test": 120}, 400),
])
@pytest.mark.parametrize(
("browser", "varnish", "status"),
[
(None, None, 200),
(1, None, 200),
(None, 120, 200),
(1, 120, 200),
(None, None, 400),
(1, None, 400),
(None, 120, 400),
(1, 120, 400),
],
)
def test_cache_deco(browser, varnish, status):
response = pretend.stub(
status_code=status,
Expand All @@ -108,26 +111,21 @@ def test_cache_deco(browser, varnish, status):
)
view = pretend.call_recorder(lambda *a, **kw: response)

app = pretend.stub(
config=pretend.stub(
cache=pretend.stub(
browser=browser,
varnish=varnish,
),
),
)
app = pretend.stub()
request = pretend.stub()

resp = cache("test")(view)(app, request)
resp = cache(browser=browser, varnish=varnish)(view)(app, request)

assert resp is response

if 200 <= resp.status_code < 400:
if browser:
assert resp.cache_control.max_age == browser["test"]
assert resp.cache_control.public
assert resp.cache_control.max_age == browser

if varnish:
assert resp.surrogate_control.max_age == varnish["test"]
assert resp.surrogate_control.public
assert resp.surrogate_control.max_age == varnish


@pytest.mark.parametrize("environ", [
Expand Down
7 changes: 6 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ def test_index(monkeypatch):
)
request = pretend.stub()

response = pretend.stub(status_code=200, headers={})
response = pretend.stub(
status_code=200,
headers={},
cache_control=pretend.stub(),
surrogate_control=pretend.stub(),
)
render_response = pretend.call_recorder(lambda *a, **kw: response)

monkeypatch.setattr(views, "render_response", render_response)
Expand Down
2 changes: 1 addition & 1 deletion warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from warehouse.utils import cache, fastly, redirect, render_response


@cache("user_profile")
@cache(browser=1, varnish=120)
@fastly("user-profile", "user-profile~{username}")
def user_profile(app, request, username):
user = app.db.accounts.get_user(username)
Expand Down
4 changes: 0 additions & 4 deletions warehouse/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ site:
database:
migrations: "warehouse:migrations"

cache:
browser: false
varnish: false

paths:
documentation: data/packagedocs

Expand Down
6 changes: 3 additions & 3 deletions warehouse/legacy/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def daytime(app, request):
return Response(response, mimetype="text/plain")


@cache("legacy_json")
@cache(browser=1, varnish=120)
@fastly("legacy-json", "legacy-json~{project_name!n}")
def project_json(app, request, project_name):
# fail early if callback is invalid
Expand Down Expand Up @@ -90,7 +90,7 @@ def project_json(app, request, project_name):
return response


@cache("legacy_rss")
@cache(browser=1, varnish=120)
@fastly("legacy_rss")
def rss(app, request):
"""Dump the last N days' updates as an RSS feed.
Expand All @@ -113,7 +113,7 @@ def rss(app, request):
return response


@cache("legacy_rss")
@cache(browser=1, varnish=120)
@fastly("legacy_rss")
def packages_rss(app, request):
"""Dump the last N days' new projects as an RSS feed.
Expand Down
6 changes: 3 additions & 3 deletions warehouse/legacy/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)


@cache("simple")
@cache(browser=1, varnish=120)
@fastly("simple-index")
def index(app, request):
projects = app.db.packaging.all_projects()
Expand All @@ -43,7 +43,7 @@ def index(app, request):
return resp


@cache("simple")
@cache(browser=1, varnish=120)
@fastly("simple", "simple~{project_name!n}")
def project(app, request, project_name):
# Get the real project name for this project
Expand Down Expand Up @@ -108,7 +108,7 @@ def project(app, request, project_name):
return resp


@cache("packages")
@cache(browser=1, varnish=120)
def package(app, request, path):
# Get our filename and filepath from the request path
filename = os.path.basename(path)
Expand Down
2 changes: 1 addition & 1 deletion warehouse/packaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from warehouse.utils import cache, fastly, redirect, render_response


@cache("project_detail")
@cache(browser=1, varnish=120)
@fastly("project-detail", "project-detail~{project_name!n}")
def project_detail(app, request, project_name, version=None):
# Get the real project name for this project
Expand Down
13 changes: 5 additions & 8 deletions warehouse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,22 @@ def render_response(app, request, template, **variables):
return Response(template.render(**context), mimetype="text/html")


def cache(key):
def cache(browser=None, varnish=None):
def deco(fn):
@functools.wraps(fn)
def wrapper(app, request, *args, **kwargs):
resp = fn(app, request, *args, **kwargs)

if 200 <= resp.status_code < 400:
# Add in our standard Cache-Control headers
if (app.config.cache.browser
and app.config.cache.browser.get(key) is not None):
if browser is not None:
resp.cache_control.public = True
resp.cache_control.max_age = app.config.cache.browser[key]
resp.cache_control.max_age = browser

# Add in additional headers if we're using varnish
if (app.config.cache.varnish
and app.config.cache.varnish.get(key) is not None):
if varnish is not None:
resp.surrogate_control.public = True
resp.surrogate_control.max_age = \
app.config.cache.varnish[key]
resp.surrogate_control.max_age = varnish

return resp
return wrapper
Expand Down
2 changes: 1 addition & 1 deletion warehouse/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from warehouse.utils import cache, fastly, render_response


@cache("index")
@cache(browser=1, varnish=120)
@fastly("index")
def index(app, request):
return render_response(
Expand Down