本文整理汇总了Python中warehouse.cache.origin.origin_cache函数的典型用法代码示例。如果您正苦于以下问题:Python origin_cache函数的具体用法?Python origin_cache怎么用?Python origin_cache使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了origin_cache函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_response_hook
def test_response_hook(self, seconds, keys):
class Fake:
pass
class Cache:
@staticmethod
@pretend.call_recorder
def cache(keys, request, response, seconds, stale_while_revalidate,
stale_if_error):
pass
response = pretend.stub()
deco = origin.origin_cache(seconds, keys=keys)
@deco
def view(context, request):
return response
key_maker = pretend.call_recorder(
lambda obj: origin.CacheKeys(cache=["one", "two"], purge=[])
)
cacher = Cache()
context = Fake()
callbacks = []
request = pretend.stub(
registry={"cache_keys": {Fake: key_maker}},
find_service=lambda iface: cacher,
add_response_callback=callbacks.append,
)
assert view(context, request) is response
assert key_maker.calls == [pretend.call(context)]
assert len(callbacks) == 1
callbacks[0](request, response)
assert cacher.cache.calls == [
pretend.call(
sorted(["one", "two"] + ([] if keys is None else keys)),
request,
response,
seconds=seconds,
stale_while_revalidate=None,
stale_if_error=None,
),
]
示例2: test_response_hook
def test_response_hook(self, seconds):
class Fake:
pass
class Cache:
@staticmethod
@pretend.call_recorder
def cache(keys, request, response, seconds):
pass
response = pretend.stub()
if seconds is None:
deco = origin.origin_cache
else:
deco = origin.origin_cache(seconds)
@deco
def view(context, request):
return response
key_maker = pretend.call_recorder(lambda obj: ["one", "two"])
cacher = Cache()
context = Fake()
callbacks = []
request = pretend.stub(
registry={"cache_keys": {Fake: key_maker}},
find_service=lambda iface: cacher,
add_response_callback=callbacks.append,
)
assert view(context, request) is response
assert key_maker.calls == [pretend.call(context)]
assert len(callbacks) == 1
callbacks[0](request, response)
assert cacher.cache.calls == [
pretend.call(["one", "two"], request, response, seconds=seconds),
]
示例3: cache_control
from sqlalchemy.orm.exc import NoResultFound
from warehouse.cache.http import cache_control
from warehouse.cache.origin import origin_cache
from warehouse.packaging.interfaces import IDownloadStatService
from warehouse.packaging.models import File, Release
@view_config(
route_name="legacy.api.json.project",
renderer="json",
decorator=[
cache_control(15 * 60), # 15 minutes
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=5 * 60, # 5 minutes
stale_if_error=1 * 24 * 60 * 60, # 1 day
),
],
)
def json_project(project, request):
if project.name != request.matchdict.get("name", project.name):
return HTTPMovedPermanently(
request.current_route_path(name=project.name),
)
try:
release = (
request.db.query(Release)
.filter(Release.project == project)
.order_by(Release._pypi_ordering.desc())
示例4: cache_control
from warehouse.cache.http import cache_control
from warehouse.cache.origin import origin_cache
from warehouse.packaging.interfaces import IDownloadStatService
from warehouse.packaging.models import File, Release, JournalEntry
@view_config(
route_name="legacy.api.json.project",
renderer="json",
decorator=[
cache_control(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=1 * 24 * 60 * 60, # 1 day
),
origin_cache(7 * 24 * 60 * 60), # 7 days
],
)
def json_project(project, request):
if project.name != request.matchdict.get("name", project.name):
return HTTPMovedPermanently(
request.current_route_path(name=project.name),
)
try:
release = project.releases.order_by(
Release._pypi_ordering.desc()
).limit(1).one()
except NoResultFound:
return HTTPNotFound()
示例5: Response
return Response(status=403)
@view_config(context=DatabaseNotAvailable)
def service_unavailable(exc, request):
return httpexception_view(HTTPServiceUnavailable(), request)
@view_config(
route_name="robots.txt",
renderer="robots.txt",
decorator=[
cache_control(1 * 24 * 60 * 60), # 1 day
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=6 * 60 * 60, # 6 hours
stale_if_error=1 * 24 * 60 * 60, # 1 day
),
],
)
def robotstxt(request):
request.response.content_type = "text/plain"
return {}
@view_config(
route_name="opensearch.xml",
renderer="opensearch.xml",
decorator=[
cache_control(1 * 24 * 60 * 60), # 1 day
origin_cache(
示例6: origin_cache
from pyramid.view import view_config
from sqlalchemy.orm import joinedload
from warehouse.cache.origin import origin_cache
from warehouse.packaging.models import Project, Release
from warehouse.xml import XML_CSP
@view_config(
route_name="rss.updates",
renderer="rss/updates.xml",
decorator=[
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=5 * 24 * 60 * 60, # 5 days
keys=["all-projects"],
)
],
)
def rss_updates(request):
request.response.content_type = "text/xml"
request.find_service(name="csp").merge(XML_CSP)
latest_releases = (
request.db.query(Release)
.options(joinedload(Release.project))
.order_by(Release.created.desc())
.limit(40)
.all()
示例7: origin_cache
from pyramid.httpexceptions import HTTPMovedPermanently, HTTPNotFound
from pyramid.view import view_config
from sqlalchemy.orm.exc import NoResultFound
from warehouse.accounts.models import User
from warehouse.cache.origin import origin_cache
from warehouse.packaging.models import Release, Role
@view_config(
route_name="packaging.project",
renderer="packaging/detail.html",
decorator=[
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=5 * 24 * 60 * 60, # 5 days
),
],
)
def project_detail(project, request):
if project.name != request.matchdict.get("name", project.name):
return HTTPMovedPermanently(
request.current_route_path(name=project.name),
)
try:
release = (
request.db.query(Release)
.filter(Release.project == project)
.order_by(
示例8: origin_cache
from sqlalchemy.orm.exc import NoResultFound
from warehouse.accounts.models import User
from warehouse.cache.http import cache_control
from warehouse.cache.origin import origin_cache
from warehouse.packaging.interfaces import IDownloadStatService, IFileStorage
from warehouse.packaging.models import Release, File, Role
@view_config(
route_name="packaging.project",
renderer="packaging/detail.html",
decorator=[
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=5 * 24 * 60 * 60, # 5 days
),
],
)
def project_detail(project, request):
if project.name != request.matchdict.get("name", project.name):
return HTTPMovedPermanently(
request.current_route_path(name=project.name),
)
try:
release = (
request.db.query(Release)
.options(joinedload(Release.uploader))
.filter(Release.project == project)
示例9: cache_control
from warehouse.cache.http import cache_control
from warehouse.csrf import csrf_protect
from warehouse.sessions import uses_session
from warehouse.utils.http import is_safe_url
@view_config(
route_name="accounts.profile",
renderer="accounts/profile.html",
decorator=[
cache_control(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=1 * 24 * 60 * 60, # 1 day
),
origin_cache(30 * 24 * 60 * 60), # 30 days
],
)
def profile(user, request):
if user.username != request.matchdict.get("username", user.username):
return HTTPMovedPermanently(
request.current_route_path(username=user.username),
)
return {"user": user}
@forbidden_view_config()
@view_config(
route_name="accounts.login",
renderer="accounts/login.html",
示例10: cache_control
from warehouse.cache.http import cache_control
from warehouse.cache.origin import origin_cache
from warehouse.packaging.models import JournalEntry, File, Project, Release
@view_config(
route_name="legacy.api.simple.index",
renderer="legacy/api/simple/index.html",
decorator=[
cache_control(
10 * 60, # 10 minutes
stale_while_revalidate=1 * 24 * 60 * 60, # 1 day
stale_if_error=1 * 24 * 60 * 60, # 1 day
),
origin_cache(7 * 24 * 60 * 60), # 7 days
],
)
def simple_index(request):
# Get the latest serial number
serial = request.db.query(func.max(JournalEntry.id)).scalar() or 0
request.response.headers["X-PyPI-Last-Serial"] = serial
# Fetch the name and normalized name for all of our projects
projects = (
request.db.query(Project.name, Project.normalized_name)
.order_by(Project.normalized_name)
.all()
)
return {"projects": projects}
示例11: origin_cache
from pyramid.view import view_config
from sqlalchemy.orm.exc import NoResultFound
from warehouse.utils import readme
from warehouse.accounts.models import User
from warehouse.cache.origin import origin_cache
from warehouse.packaging.models import Project, Release, Role
@view_config(
route_name="packaging.project",
context=Project,
renderer="packaging/detail.html",
decorator=[
origin_cache(
1 * 24 * 60 * 60, stale_if_error=5 * 24 * 60 * 60 # 1 day, 5 days stale
)
],
)
def project_detail(project, request):
if project.name != request.matchdict.get("name", project.name):
return HTTPMovedPermanently(request.current_route_path(name=project.name))
try:
release = (
request.db.query(Release)
.filter(Release.project == project)
.order_by(Release.is_prerelease.nullslast(), Release._pypi_ordering.desc())
.limit(1)
.one()
)
示例12: cache_control
SITEMAP_MAXSIZE = 50000
Bucket = collections.namedtuple("Bucket", ["name", "modified"])
@view_config(
route_name="index.sitemap.xml",
renderer="sitemap/index.xml",
decorator=[
cache_control(1 * 60 * 60), # 1 hour
origin_cache(
1 * 24 * 60 * 60, # 1 day
stale_while_revalidate=6 * 60 * 60, # 6 hours
stale_if_error=1 * 24 * 60 * 60, # 1 day
keys=["all-projects"],
),
],
)
def sitemap_index(request):
request.response.content_type = "text/xml"
request.find_service(name="csp").merge(XML_CSP)
# We have > 50,000 URLs on PyPI and a single sitemap file can only support
# a maximum of 50,000 URLs. We need to split our URLs up into multiple
# files so we need to stick all of our URLs into buckets, in addition we
# want our buckets to remain stable so that an URL won't change what bucket
# it is in just because another URL is added or removed. Finally we also
# want to minimize the number of buckets we have to reduce the number of