Module: Redlics

Extended by:
Redlics
Included in:
Redlics
Defined in:
lib/redlics.rb,
lib/redlics/key.rb,
lib/redlics/query.rb,
lib/redlics/config.rb,
lib/redlics/counter.rb,
lib/redlics/tracker.rb,
lib/redlics/version.rb,
lib/redlics/exception.rb,
lib/redlics/operators.rb,
lib/redlics/connection.rb,
lib/redlics/time_frame.rb,
lib/redlics/granularity.rb,
lib/redlics/query/operation.rb

Overview

Redlics version.

Defined Under Namespace

Modules: Connection, Counter, Exception, Granularity, Key, Operators, Tracker Classes: Config, Query, TimeFrame

Constant Summary collapse

LUA_CACHE =

Redlics constants.

Hash.new { |h, k| h[k] = Hash.new }
LUA_SCRIPT =
File.expand_path('../lua/script.lua', __FILE__).freeze
CONTEXTS =
{ counter: { short: :c, long: :counter }, tracker: { short: :t, long: :tracker }, operation: { short: :o, long: :operation } }.freeze
VERSION =
'0.1.9'

Instance Method Summary collapse

Instance Method Details

#configOpenStruct

Get or initialize Redlics config.

Returns:

  • (OpenStruct)

    Redlics configuration


78
79
80
# File 'lib/redlics.rb', line 78

def config
  @config ||= Redlics::Config.new
end

#configure {|config| ... } ⇒ OpenStruct

Set configuration of Redlics in a block.

Yields:

Returns:

  • (OpenStruct)

    Redlics configuration


85
86
87
# File 'lib/redlics.rb', line 85

def configure
  yield config if block_given?
end

#redisObject

Get or initialize the Redis connection.

Returns:

  • (Object)

    redis connection

Raises:

  • (ArgumentError)

32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redlics.rb', line 32

def redis
  raise ArgumentError, 'requires a block' unless block_given?
  redis_pool.with do |conn|
    retryable = true
    begin
      yield conn
    rescue Redis::BaseError => e
      raise e unless config.silent
    rescue Redis::CommandError => ex
      (conn.disconnect!; retryable = false; retry) if retryable && ex.message =~ /READONLY/
      raise unless config.silent
    end
  end
end

#script(file, *args) ⇒ String

Load Lua script file and arguments in Redis.

Parameters:

  • file (String)

    absolute path to the Lua script file

  • *args (Array)

    list of arguments for Redis evalsha

Returns:

  • (String)

    Lua script result


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/redlics.rb', line 53

def script(file, *args)
  begin
    cache = LUA_CACHE[redis { |r| r.client.options[:url] }]
    if cache.key?(file)
      sha = cache[file]
    else
      src = File.read(file)
      sha = redis { |r| r.redis.script(:load, src) }
      cache[file] = sha
    end
    redis { |r| r.evalsha(sha, *args) }
  rescue RuntimeError
    case $!.message
    when Exception::ErrorPatterns::NOSCRIPT
      LUA_CACHE[redis { |r| r.client.options[:url] }].clear
      retry
    else
      raise $! unless config.silent
    end
  end
end