source: subversion/sites/tile.openstreetmap.org/cat_tile.rb-no-count @ 5396

Last change on this file since 5396 was 5386, checked in by jonb, 13 years ago

update tile cat_tile.rb script and provide some variants which are used if the machine is under heavy load (e.g. turning off instant rendering or access limit counting).

  • Property svn:executable set to *
File size: 1.7 KB
Line 
1require 'cgi'
2
3cgi = CGI.new
4
5module Foo
6  require 'mysql'
7  require 'date'
8  require 'time'
9  require 'singleton'
10
11  class Bar
12    def call_local_sql
13      dbh = nil
14      begin
15        dbh = Mysql.real_connect('localhost', 'tile', 'tile', 'tile')
16        sql = yield
17        res = dbh.query(sql)
18        return res #        if res.nil? then return true else return res end
19      rescue MysqlError =>ex
20        puts ex
21      ensure
22        dbh.close unless dbh.nil?
23      end
24      nil
25    end
26  end
27end
28
29#first check number of accesses
30
31fb = Foo::Bar.new
32
33#now send the tile
34x = cgi['x'].to_i
35y = cgi['y'].to_i
36z = cgi['z'].to_i
37
38if z and (z > 18 or z < 0)
39  exit
40end
41
42# valid x/y for tiles are 0 ... 2^zoom-1
43limit = (2 ** z) - 1
44
45if x and (x < 0 or x > limit)
46  print IO.read("/home/www/tile/images/blank-000000.png")
47  exit
48end
49
50if y and (y < 0 or y > limit)
51  print IO.read("/home/www/tile/images/blank-000000.png")
52  exit
53end
54
55res = fb.call_local_sql { "select data, dirty_t, created_at from tiles where x = #{x} and y=#{y} and z=#{z} limit 1" }
56if res.nil?
57  exit
58end
59if res.num_rows == 0
60  fb.call_local_sql { "insert into tiles (x,y,z,dirty_t, created_at) values (#{x},#{y},#{z},'true',NOW())" }
61  exit
62else
63  res.each_hash do |row|
64    created_at = Time.parse(row['created_at'])
65    expires_at = created_at + (60*60*24*3)
66    if row['dirty_t'] == 'true' or expires_at < Time.now
67      cgi.header("expires" => Time.now + (60*30))
68    else
69      cgi.header("expires" => expires_at)
70    end
71    cgi.header("image/png")
72    print row['data']
73    if row['dirty_t'] == 'false' and expires_at < Time.now
74      fb.call_local_sql { "update tiles set dirty_t = 'true' where x = #{x} and y=#{y} and z=#{z}" }
75    end
76  end
77end
78
Note: See TracBrowser for help on using the repository browser.