How may we implement caching for a database Driven Map-Tile Server?
The ideas i have are:
1) cache responses (bytes) for unique z-x-y requests in memory (this will not scale well, if we have a large map area)
2a) render requested tiles to file system as /site/tiles/z/x/y.png
and server from there subsequently,
2b) apply suitable caching headers
to ensure HTTP 304
for re-requests
I had implemented this basic map-tile server with Leaflet.js and a simple .aspx page - see here.
Each request for a map-tile hits the db, pulls out a binary-tile-image and returns a .png
file like so:
MapTest.html
<div id='map'></div>
<script>
var map = L.map('map').setView([8.2, 6.95], 7);
L.tileLayer('**http://localhost/tileserver/tile.aspx?z={z}&x={x}&y={y}**', {
minZoom: 7, maxZoom: 16,
attribution: 'My Tile Server'
}).addTo(map);
</script>
Tiles.aspx
Option Strict On
Partial Class tile
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim z, x, y As Integer
z = CInt(Request.QueryString("z"))
x = CInt(Request.QueryString("x"))
y = CInt(Request.QueryString("y"))
Dim b() As Byte = DB.GetTile(z, x, y)
Response.Buffer = True
Response.Charset = ""
'Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "image/png"
Response.AddHeader("content-disposition", "attachment;filename=" & y & ".png")
Response.BinaryWrite(b)
Response.Flush()
Response.End()
End Sub
Please how can we improve this with an effective caching?