Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
douban-api-proxy
Commits
6b1f3aa5
Commit
6b1f3aa5
authored
Aug 19, 2017
by
OzzieIsaacs
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'adv/cache-buster'
parents
bd02c921
a27848b3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
cache_buster.py
cps/cache_buster.py
+57
-0
web.py
cps/web.py
+2
-0
No files found.
cps/cache_buster.py
0 → 100644
View file @
6b1f3aa5
# Inspired by https://github.com/ChrisTM/Flask-CacheBust
# Uses query strings so CSS font files are found without having to resort to absolute URLs
import
hashlib
import
os
def
init_cache_busting
(
app
):
"""
Configure `app` to so that `url_for` adds a unique query string to URLs generated
for the `'static'` endpoint.
This allows setting long cache expiration values on static resources
because whenever the resource changes, so does its URL.
"""
static_folder
=
app
.
static_folder
# the rooted path to the static file folder
hash_table
=
{}
# map of file hashes
app
.
logger
.
debug
(
'Computing cache-busting values...'
)
# compute file hashes
for
dirpath
,
dirnames
,
filenames
in
os
.
walk
(
static_folder
):
for
filename
in
filenames
:
# compute version component
rooted_filename
=
os
.
path
.
join
(
dirpath
,
filename
)
with
open
(
rooted_filename
,
'r'
)
as
f
:
version
=
hashlib
.
md5
(
f
.
read
())
.
hexdigest
()[:
7
]
# save version to tables
file_path
=
rooted_filename
.
replace
(
static_folder
+
"/"
,
""
)
hash_table
[
file_path
]
=
version
app
.
logger
.
debug
(
'Finished computing cache-busting values'
)
def
bust_filename
(
filename
):
return
hash_table
.
get
(
filename
,
""
)
def
unbust_filename
(
filename
):
return
filename
.
split
(
"?"
,
1
)[
0
]
@
app
.
url_defaults
def
reverse_to_cache_busted_url
(
endpoint
,
values
):
"""
Make `url_for` produce busted filenames when using the 'static' endpoint.
"""
if
endpoint
==
'static'
:
values
[
"q"
]
=
bust_filename
(
values
[
'filename'
])
def
debusting_static_view
(
filename
):
"""
Serve a request for a static file having a busted name.
"""
return
original_static_view
(
filename
=
unbust_filename
(
filename
))
# Replace the default static file view with our debusting view.
original_static_view
=
app
.
view_functions
[
'static'
]
app
.
view_functions
[
'static'
]
=
debusting_static_view
cps/web.py
View file @
6b1f3aa5
...
...
@@ -21,6 +21,7 @@ from flask import (Flask, render_template, request, Response, redirect,
url_for
,
send_from_directory
,
make_response
,
g
,
flash
,
abort
,
Markup
,
stream_with_context
)
from
flask
import
__version__
as
flaskVersion
import
cache_buster
import
ub
from
ub
import
config
import
helper
...
...
@@ -200,6 +201,7 @@ mimetypes.add_type('image/vnd.djvu', '.djvu')
app
=
(
Flask
(
__name__
))
app
.
wsgi_app
=
ReverseProxied
(
app
.
wsgi_app
)
cache_buster
.
init_cache_busting
(
app
)
gevent_server
=
None
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment