Skip to content

Commit

Permalink
Remove use of Tilt::Cache (#1922)
Browse files Browse the repository at this point in the history
Tilt::Cache is deprecated and will be removed. Copy the implementation
currently used as Sinatra::TemplateCache, so Sinatra is not affected by
the deprecation and removal of Tilt::Cache.

Fixes #1884
  • Loading branch information
jeremyevans authored May 15, 2023
1 parent 6045b01 commit 8e80d67
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
31 changes: 30 additions & 1 deletion lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,35 @@ def compile_block_template(template, options, &body)
end
end

# Extremely simple template cache implementation.
# * Not thread-safe.
# * Size is unbounded.
# * Keys are not copied defensively, and should not be modified after
# being passed to #fetch. More specifically, the values returned by
# key#hash and key#eql? should not change.
#
# Implementation copied from Tilt::Cache.
class TemplateCache
def initialize
@cache = {}
end

# Caches a value for key, or returns the previously cached value.
# If a value has been previously cached for key then it is
# returned. Otherwise, block is yielded to and its return value
# which may be nil, is cached under key and returned.
def fetch(*key)
@cache.fetch(key) do
@cache[key] = yield
end
end

# Clears the cache.
def clear
@cache = {}
end
end

# Base class for all Sinatra applications and middleware.
class Base
include Rack::Utils
Expand All @@ -928,7 +957,7 @@ class Base
def initialize(app = nil, **_kwargs)
super()
@app = app
@template_cache = Tilt::Cache.new
@template_cache = TemplateCache.new
@pinned_response = nil # whether a before! filter pinned the content-type
yield self if block_given?
end
Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/lib/sinatra/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def settings
end

def template_cache
super.fetch(:nested, @namespace) { Tilt::Cache.new }
super.fetch(:nested, @namespace) { TemplateCache.new }
end

def redirect_to(uri, *args)
Expand Down
2 changes: 1 addition & 1 deletion test/sinatra_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class SinatraTest < Minitest::Test
end

it "responds to #template_cache" do
assert_kind_of Tilt::Cache, Sinatra::Base.new!.template_cache
assert_kind_of Sinatra::TemplateCache, Sinatra::Base.new!.template_cache
end
end

0 comments on commit 8e80d67

Please sign in to comment.