Module: MeRedis::ClassMethods
- Defined in:
- lib/me_redis.rb
Instance Method Summary collapse
- #configure(config = nil) {|me_config| ... } ⇒ Object
- #get_compressor_for_key(key) ⇒ Object
- #get_compressor_namespace_from_key(key) ⇒ Object
- #key_zip_regxp ⇒ Object
- #me_config ⇒ Object
- #zip?(key) ⇒ Boolean
- #zip_crumbs ⇒ Object
- #zip_ns_finder ⇒ Object
Instance Method Details
#configure(config = nil) {|me_config| ... } ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/me_redis.rb', line 20 def configure( config = nil ) # at start they are nils, but at subsequent calls they may not be nils me_config.key_zip_regxp = nil me_config.compress_ns_regexp = nil @zip_ns_finder = nil config.each{ |key,value| me_config.send( "#{key}=", value ) } if config yield( me_config ) if block_given? prepare_zip_crumbs prepare_compressors # useful for chaining with dynamic class creations self end |
#get_compressor_for_key(key) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/me_redis.rb', line 106 def get_compressor_for_key( key ) if me_config.compress_ns_regexp me_config.default_compressor else me_config.compress_namespaces[get_compressor_namespace_from_key( key )] end end |
#get_compressor_namespace_from_key(key) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/me_redis.rb', line 79 def get_compressor_namespace_from_key( key ) ns_matched = zip_ns_finder[:rgxps_ns] && key.match(zip_ns_finder[:rgxps_ns]) if ns_matched&.captures zip_ns_finder[:rgxps_arr][ns_matched.captures.each_with_index.find{|el,i| el}[1]] else zip_ns_finder[:string_ns] && key.match(zip_ns_finder[:string_ns])&.send(:[], 0) end end |
#key_zip_regxp ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/me_redis.rb', line 68 def key_zip_regxp return me_config.key_zip_regxp if me_config.key_zip_regxp regexp_parts = [] #reverse order just to be sure we replaced longer strings before shorter # also we need to sort by length, not just sort, because we must try to replace 'z_key_a' first, # and only after that we can replace 'key' regexp_parts << "(#{zip_crumbs.keys.sort_by(&:length).reverse.join('|')})" if zip_crumbs regexp_parts << '(\d+)' if me_config.integers_to_base62 me_config.key_zip_regxp ||= /#{regexp_parts.join('|')}/ end |
#me_config ⇒ Object
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 |
# File 'lib/me_redis.rb', line 38 def me_config @me_config ||= Struct.new( # if set - configures Redis hash_max_ziplist_entries value, # otherwise it will be filled from Redis hash-max-ziplist-value :hash_max_ziplist_entries, # same as above only for value, only resets it globally if present :hash_max_ziplist_value, # array or hash or string/sym of key crumbs to zip, if a hash given it used as is, # otherwise meredis tries to construct hash by using first char from each key + integer in base62 form for # subsequent appearence of a crumb starting with same char :zip_crumbs, # zip integers in keys to base62 form :integers_to_base62, # regex composed from zip_crumbs keys and integer regexp if integers_to_base62 is set :key_zip_regxp, # prefixes/namespaces for keys need zipping, # acceptable formats: # 1. single string/sym will map it to defauilt compressor # 2. array of string/syms will map it to defauilt compressor # 3. hash maps different kinds of 1 and 2 to custom compressors :compress_namespaces, # if configured than default_compressor used for compression of all keys matched and compress_namespaces is ignored :compress_ns_regexp, :default_compressor ).new(512) end |
#zip?(key) ⇒ Boolean
88 89 90 91 92 |
# File 'lib/me_redis.rb', line 88 def zip?(key) me_config.compress_ns_regexp&.match?(key) || zip_ns_finder[:string_ns]&.match?(key) || zip_ns_finder[:rgxps_ns]&.match?(key) end |
#zip_crumbs ⇒ Object
66 |
# File 'lib/me_redis.rb', line 66 def zip_crumbs; me_config.zip_crumbs end |
#zip_ns_finder ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/me_redis.rb', line 94 def zip_ns_finder return @zip_ns_finder if @zip_ns_finder regexps_compress_ns = me_config.compress_namespaces.keys.select{|key| key.is_a?(Regexp) } strs_compress_ns = me_config.compress_namespaces.keys.select{|key| !key.is_a?(Regexp) } @zip_ns_finder = { string_ns: strs_compress_ns.length == 0 ? nil : /\A(#{strs_compress_ns.sort_by(&:length).reverse.join('|')})/, rgxps_ns: regexps_compress_ns.length == 0 ? nil : /\A#{regexps_compress_ns.map{|rgxp| "(#{rgxp})" }.join('|')}/, rgxps_arr: regexps_compress_ns } end |