Module: RedisModelExtension::ClassRedisKey
- Defined in:
- lib/redis-model-extension/redis_key.rb
Overview
Class Redis Key
generators of redis keys used for creating keys for search and save of models & aliases
Instance Method Summary collapse
-
#alias_exists?(alias_name, args = {}) ⇒ Boolean
Check if key by alias name and arguments exists in db.
-
#exists?(args = {}) ⇒ Boolean
Check if key by arguments exists in db.
-
#generate_alias_key(alias_name, args = {}) ⇒ Object
Generates redis key for storing indexes for dynamic aliases will produce something like: your_class:dynamic:name_of_your_dynami_alias:field_value2:field_value1…
-
#generate_key(args = {}, key = "key") ⇒ Object
Generates redis key for storing object * will produce something like: your_class:key:field_value1:field_value2…
Instance Method Details
#alias_exists?(alias_name, args = {}) ⇒ Boolean
Check if key by alias name and arguments exists in db
73 74 75 |
# File 'lib/redis-model-extension/redis_key.rb', line 73 def alias_exists? alias_name, args = {} RedisModelExtension::Database.redis.exists(self.name.constantize.generate_alias_key(alias_name, args)) end |
#exists?(args = {}) ⇒ Boolean
Check if key by arguments exists in db
68 69 70 |
# File 'lib/redis-model-extension/redis_key.rb', line 68 def exists? args = {} RedisModelExtension::Database.redis.exists(self.name.constantize.generate_key(args)) end |
#generate_alias_key(alias_name, args = {}) ⇒ Object
Generates redis key for storing indexes for dynamic aliases will produce something like: your_class:dynamic:name_of_your_dynami_alias:field_value2:field_value1… (field values are sorted by fields order)
-
dynamic_alias_name (Symbol) - name of your dynamic alias
-
args (Hash) - arguments of your model
-
field_order (Array of symbols) - order of fields (ex. [:field2, :field1])
-
field_args (Hash) - hash of values for aliasing (ex. => “field_value1”, :field2 => “field_value2”)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/redis-model-extension/redis_key.rb', line 32 def generate_alias_key alias_name, args = {} #check if asked dynamic alias exists raise ArgumentError, "Unknown dynamic alias: '#{alias_name}', use: #{redis_alias_config.keys.join(", ")} " unless redis_alias_config.has_key?(alias_name.to_sym) #normalize input hash of arguments args = HashWithIndifferentAccess.new(args) # prepare class name + dynamic + alias name out = "#{self.name.to_s.underscore.to_sym}:alias:#{alias_name}" #get config config = redis_alias_config[alias_name.to_sym] # use all specified keys config[:main_fields].each do |key| out += add_item_to_redis_key args, key end #is alias dynamic? if config[:order_field] && config[:args_field] #check if input arguments has order field if args.has_key?(config[:order_field]) && args[config[:order_field]] && args.has_key?(config[:args_field]) && args[config[:args_field]] #use filed order from defined field in args args[config[:order_field]].each do |key| out += add_item_to_redis_key args[config[:args_field]], key end else # use global search out += ":*" end end out end |
#generate_key(args = {}, key = "key") ⇒ Object
Generates redis key for storing object
-
will produce something like: your_class:key:field_value1:field_value2…
(depending on your redis_key setting)
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/redis-model-extension/redis_key.rb', line 13 def generate_key args = {}, key = "key" #normalize input hash of arguments args = HashWithIndifferentAccess.new(args) out = "#{self.name.to_s.underscore.to_sym}:#{key}" redis_key_config.each do |key| out += add_item_to_redis_key args, key end out end |