Module: Redlics::Key
Overview
Key namespace
Instance Method Summary collapse
-
#bucketize?(context, options = {}) ⇒ Boolean
Check if Redlics can bucketize.
-
#decode(string) ⇒ Integer
Decode a number with a mapping table.
-
#encode(number) ⇒ String
Encode a number with a mapping table.
-
#exists?(key) ⇒ Boolean
Check if a key exists in Redis.
-
#name(context, event, granularity, past, options = {}) ⇒ String, Array
Construct the key name with given parameters.
-
#timeframed(context, event, time_object, options = {}) ⇒ Array
Construct an array with all keys of a time frame in a given granularity.
-
#unique_namespace ⇒ String
Create a unique operation key in Redis.
-
#with_namespace(key) ⇒ String
Prepend namespace to a key.
Instance Method Details
#bucketize?(context, options = {}) ⇒ Boolean
Check if Redlics can bucketize.
102 103 104 |
# File 'lib/redlics/key.rb', line 102 def bucketize?(context, = {}) context[:long] == :counter && Redlics.config.bucket && ![:id].nil? end |
#decode(string) ⇒ Integer
Decode a number with a mapping table.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/redlics/key.rb', line 77 def decode(string) decoded = '' string = string.to_s token = 0 while token <= string.size - 1 number = decode_map[string[token]].to_s decoded += number.size == 1 ? "0#{number}" : number token += 1 end decoded.to_i end |
#encode(number) ⇒ String
Encode a number with a mapping table.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/redlics/key.rb', line 61 def encode(number) encoded = '' number = number.to_s number = (number.size % 2) != 0 ? "0#{number}" : number token = 0 while token <= number.size - 1 encoded += encode_map[number[token..token+1].to_i.to_s].to_s token += 2 end encoded end |
#exists?(key) ⇒ Boolean
Check if a key exists in Redis.
93 94 95 |
# File 'lib/redlics/key.rb', line 93 def exists?(key) Redlics.redis { |r| r.exists(key) } end |
#name(context, event, granularity, past, options = {}) ⇒ String, Array
Construct the key name with given parameters.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/redlics/key.rb', line 17 def name(context, event, granularity, past, = {}) past ||= Time.now event ||= 'nil' granularity = Granularity.validate(context, granularity).first event = encode_event(event) if Redlics.config.encode[:events] key = "#{context[:short]}#{Redlics.config.separator}#{event}#{Redlics.config.separator}#{time_format(granularity, past)}" key = with_namespace(key) if [:namespaced] return bucketize(key, [:id]) if bucketize?(context, ) return unbucketize(key, [:id]) if context[:long] == :counter && ![:id].nil? key end |
#timeframed(context, event, time_object, options = {}) ⇒ Array
Construct an array with all keys of a time frame in a given granularity.
39 40 41 42 43 44 45 |
# File 'lib/redlics/key.rb', line 39 def timeframed(context, event, time_object, = {}) = { namespaced: true }.merge() timeframe = TimeFrame.new(context, time_object, ) timeframe.splat do |time| name(context, event, timeframe.granularity, time, ) end end |
#unique_namespace ⇒ String
Create a unique operation key in Redis.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/redlics/key.rb', line 108 def unique_namespace loop do ns = operation unless exists?(ns) Redlics.redis do |conn| conn.pipelined do |redis| redis.set(ns, 0) redis.expire(ns, Redlics.config.operation_expiration) end end break ns end end end |
#with_namespace(key) ⇒ String
Prepend namespace to a key.
51 52 53 54 55 |
# File 'lib/redlics/key.rb', line 51 def with_namespace(key) return key unless Redlics.config.namespace.length > 0 return key if key.split(Redlics.config.separator).first == Redlics.config.namespace.to_s "#{Redlics.config.namespace}#{Redlics.config.separator}#{key}" end |