Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

resource changes now invalidate megatile cache

  • Loading branch information...
commit c382afd9222b4b5bec6af99296a328c86144d558 1 parent 3963140
@bennytheshap authored
View
5 Gemfile
@@ -39,7 +39,6 @@ group :development, :test do
gem 'rb-fsevent'
gem 'capybara'
- gem 'cucumber-rails'
gem 'database_cleaner'
gem 'fuubar'
gem 'fuubar-cucumber'
@@ -51,6 +50,10 @@ group :development, :test do
gem 'tattletail'
end
+group :test do
+ gem 'cucumber-rails'
+end
+
group :production do
gem 'exceptional'
end
View
36 app/controllers/megatiles_controller.rb
@@ -6,32 +6,22 @@ def index
authorize! :do_things, @world
- if params.has_key? :x_min
- # if @megatiles.count * @world.megatile_width > 1000
- x_min = params[:x_min].to_i
- x_max = params[:x_max].to_i
- y_min = params[:y_min].to_i
- y_max = params[:y_max].to_i
-
- if (x_max - x_min)*(y_max - y_min) > 2000
- render :status => :request_entity_too_large, :text => "Request too large"
- return
- end
-
- @megatiles = Megatile.where(:world_id => @world.id).where("x >= :x_min AND x<= :x_max AND y >= :y_min AND y <= :y_max",
- {:x_min => x_min, :x_max => x_max, :y_min => y_min, :y_max => y_max}).includes(:resource_tiles)
- else
- if @world.width * @world.height > 2000
- render :status => :request_entity_too_large, :text => "Request too large"
- return
- end
- @megatiles = @world.megatiles
+ # if @megatiles.count * @world.megatile_width > 1000
+ x_min = params[:x_min].to_i
+ x_max = params[:x_max].to_i
+ y_min = params[:y_min].to_i
+ y_max = params[:y_max].to_i
+
+ if (x_max - x_min)*(y_max - y_min) > 2000
+ render :status => :request_entity_too_large, :text => "Request too large"
+ return
end
+ data = MegatileRegionCache.MegatilesInRegion(@world.id, x_min, y_min, x_max, y_max)
+ ret = "{\"megatiles\": #{data}}"
+
respond_to do |format|
- format.xml { render_for_api :megatile_with_resources, :xml => @megatiles, :root => :megatiles }
- format.json { render_for_api :megatile_with_resources, :json => @megatiles, :root => :megatiles }
- format.mpac { render_for_api :megatile_with_resources, :mpac => @megatiles, :root => :megatiles }
+ format.json { render :text => ret, :content_type => 'application/json' }
end
end
View
2  app/models/megatile.rb
@@ -85,7 +85,7 @@ def json
def invalidate_cache
Rails.cache.delete cache_key
- self.megatile_region_cache.invalidate
+ self.megatile_region_cache.invalidate if self.megatile_region_cache
end
def cache_key
View
12 app/models/megatile_region_cache.rb
@@ -7,7 +7,7 @@ class MegatileRegionCache < ActiveRecord::Base
validates_uniqueness_of :x_min, :scope => [:world_id]
validates_uniqueness_of :x_max, :scope => [:world_id]
- def Combine_jsons(jsonlist)
+ def self.CombineJSONs(jsonlist)
ret = "["
list_length = jsonlist.count
list_length.times do |i|
@@ -17,10 +17,18 @@ def Combine_jsons(jsonlist)
ret << "]"
end
+ def self.MegatilesInRegion(world_id, x_min, y_min, x_max, y_max)
+ caches = MegatileRegionCache.where(:world_id => world_id).where("x_min >= :x_min AND x_max<= :x_max AND y_min >= :y_min AND y_max <= :y_max",
+ {:x_min => x_min, :x_max => x_max, :y_min => y_min, :y_max => y_max})
+ jsonlist = caches.map { |cache| cache.json.strip[1..-2] } #this should be one long list; we don't want the square brackets
+
+ CombineJSONs jsonlist
+ end
+
def json
Rails.cache.fetch(cache_key) do
jsonlist = self.megatiles.map { |mt| mt.json }
- Combine_jsons jsonlist
+ CombineJSONs jsonlist
end
end
View
6 app/models/resource_tile.rb
@@ -5,6 +5,8 @@ class ResourceTile < ActiveRecord::Base
belongs_to :world
has_many :agents
has_many :resources
+
+ after_save :invalidate_megatile_cache
def self.dist
@@dist ||= SimpleRandom.new
@@ -253,4 +255,8 @@ def non_owner_permitted_actions
def <=> other
self.location <=> other.location
end
+
+ def invalidate_megatile_cache
+ megatile.invalidate_cache
+ end
end
View
5 spec/models/megatile_region_cache_spec.rb
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe MegatileRegionCache do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
14 spec/models/megatile_spec.rb
@@ -46,6 +46,20 @@
megatile.reload.resource_tiles.count.should == 9
end
end
+
+
+ describe 'cache invalidation' do
+ let(:megatile) { create :megatile }
+ #let(:megatile_cache) { create :megatile_region_cache } #FIXME need to pass the megatile in here
+ before { megatile.spawn_resources }
+ it 'invalidates its cache when a contained resource is saved' do
+ megatile.json
+ Rails.cache.read(megatile.cache_key).should_not be_nil
+ megatile.resource_tiles.first.save!
+ Rails.cache.read(megatile.cache_key).should be_nil
+ end
+ end
+
context 'Listings' do
let!(:listing) { create :listing }
View
1  spec/models/resource_tile_spec.rb
@@ -100,4 +100,5 @@
resource_tile.location.should == [1, 3]
end
end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.