Class: Bearcat::RateLimiting::RedisScript
- Inherits:
- 
      Object
      
        - Object
- Bearcat::RateLimiting::RedisScript
 
- Defined in:
- lib/bearcat/rate_limiting/redis_script.rb
Overview
RedisScript represents a lua script in the filesystem. It loads the script from disk and handles talking to redis to execute it. Error handling is handled by LuaError.
Defined Under Namespace
Classes: LuaError, TemplateContext
Instance Method Summary collapse
- 
  
    
      #call(redis, *args)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Passes the script and supplied arguments to redis for evaulation. 
- #content ⇒ Object
- #digest ⇒ Object
- 
  
    
      #initialize(file)  ⇒ RedisScript 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Loads the script file from disk and calculates its SHA1sum.
Constructor Details
#initialize(file) ⇒ RedisScript
Loads the script file from disk and calculates its SHA1 sum.
| 17 18 19 | # File 'lib/bearcat/rate_limiting/redis_script.rb', line 17 def initialize(file) @file = Pathname.new(file) end | 
Instance Method Details
#call(redis, *args) ⇒ Object
Passes the script and supplied arguments to redis for evaulation. It first attempts to use a script redis has already cached by using the EVALSHA command, but falls back to providing the full script text via EVAL if redis has not seen this script before. Future invocations will then use EVALSHA without erroring.
| 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # File 'lib/bearcat/rate_limiting/redis_script.rb', line 32 def call(redis, *args) t = Time.now begin redis.evalsha(digest, *args) rescue => e e. =~ /NOSCRIPT/ ? redis.eval(content, *args) : raise end rescue => e if LuaError.intercepts?(e) raise LuaError.new(e, @file, content) else raise end end | 
#content ⇒ Object
| 47 48 49 | # File 'lib/bearcat/rate_limiting/redis_script.rb', line 47 def content @content ||= load_lua(@file) end | 
#digest ⇒ Object
| 51 52 53 | # File 'lib/bearcat/rate_limiting/redis_script.rb', line 51 def digest @digest ||= Digest::SHA1.hexdigest content end |