Permalink
Browse files

Adding ttl option to Stasis::Server.push

  • Loading branch information...
1 parent a60567c commit 9243826acedd9177f844fd895b83d8f3fd0da46a @winton committed Sep 9, 2011
Showing with 54 additions and 9 deletions.
  1. +3 −0 README.md
  2. +26 −9 lib/stasis/server.rb
  3. +25 −0 spec/stasis/server_spec.rb
View
@@ -282,6 +282,9 @@ Push to the server (in Ruby):
# Return rendered templates (false by default)
:return => false,
+ # Cache ttl for returned templates (nil by default)
+ :ttl => nil,
+
# Block until templates generate (false by default)
:wait => false,
View
@@ -19,22 +19,39 @@ def initialize(root, options={})
request = redis.lpop('stasis:requests')
if request
+ files = {}
request = Yajl::Parser.parse(request)
- params = request['paths'] + [
- {
- :collect => request['return'],
- :write => request['write']
- }
- ]
- files = stasis.render(*params)
+ paths = request['paths'].reject do |path|
+ files[path] = redis.get("stasis:caches:#{root}:#{path}")
+ end
+
+ if paths.empty? && !request['paths'].empty?
+ new_files = {}
+ else
+ params = request['paths'] + [
+ {
+ :collect => request['return'],
+ :write => request['write']
+ }
+ ]
+ new_files = stasis.render(*params)
+ end
+
+ if request['ttl']
+ new_files.each do |path, body|
+ key = "stasis:caches:#{root}:#{path}"
+ redis.set(key, body)
+ redis.expire(key, request['ttl'])
+ end
+ end
- if request['return'] && request['paths'] && !request['paths'].empty?
+ if request['return']
request['wait'] = true
end
if request['wait']
- response = files
+ response = files.merge(new_files)
redis.publish(self.class.response_key(request['id']), Yajl::Encoder.encode(response))
end
end
View
@@ -26,4 +26,29 @@
new_time_from_file.should == new_time
new_time_from_file.should_not == time
end
+
+ it "should expire after ttl" do
+ time = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :ttl => 1,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time2 = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time.should == time2
+ sleep 2
+ time3 = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time2.should_not == time3
+ end
end

0 comments on commit 9243826

Please sign in to comment.