Skip to content

Commit bfb043c

Browse files
committed
add simple development tile cache:
- change scene url to http://localhost:8000/vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt - run 'python proxy.py' - delete .tiles to clear the cache
1 parent aa21687 commit bfb043c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Icon
1212
# Thumbnails
1313
._*
1414

15+
# proxy.py cache
16+
.tiles
17+
1518
# Files that might appear on external disk
1619
.Spotlight-V100
1720
.Trashes

Diff for: proxy.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Originally from http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/
2+
#
3+
# Usage:
4+
# A call to http://localhost:8000/example.com/foo.html will cache the file
5+
# at http://example.com/foo.html on disc and not redownload it again.
6+
# To clear the cache simply do a `rm *.cached`. To stop the server simply
7+
# send SIGINT (Ctrl-C). It does not handle any headers or post data.
8+
#
9+
# see also: https://pymotw.com/2/BaseHTTPServer/
10+
11+
import BaseHTTPServer
12+
import hashlib
13+
import os
14+
import urllib2
15+
16+
from BaseHTTPServer import HTTPServer
17+
from SocketServer import ThreadingMixIn
18+
19+
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
20+
pass
21+
22+
class CacheHandler(BaseHTTPServer.BaseHTTPRequestHandler):
23+
def do_GET(self):
24+
m = hashlib.md5()
25+
m.update(self.path)
26+
cache_filename = ".tiles/" + m.hexdigest()
27+
if os.path.exists(cache_filename):
28+
print "Cache hit"
29+
data = open(cache_filename).readlines()
30+
else:
31+
print "Cache miss"
32+
data = urllib2.urlopen("http:/" + self.path, timeout=10).readlines()
33+
open(cache_filename, 'wb').writelines(data)
34+
35+
self.send_response(200)
36+
self.send_header("Content-Encoding", "gzip")
37+
self.end_headers()
38+
self.wfile.writelines(data)
39+
40+
def run():
41+
if not os.path.exists(".tiles"):
42+
os.makedirs(".tiles")
43+
44+
server_address = ('', 8000)
45+
httpd = ThreadedHTTPServer(server_address, CacheHandler)
46+
httpd.serve_forever()
47+
48+
if __name__ == '__main__':
49+
run()

0 commit comments

Comments
 (0)