Module: RedisUtility
- Extended by:
- RedisUtility
- Included in:
- RedisUtility
- Defined in:
- lib/redis_utility.rb,
lib/redis_utility/version.rb
Overview
module RedisUtility namespace for redis methods
Constant Summary collapse
- KEY_BATCH_SIZE =
1000
- VERSION =
'1.0.0'
Instance Method Summary collapse
- #cache(key, params = {}) ⇒ Object
- #cache_string(key, params = {}) ⇒ Object
-
#export_data(key_patterns, filename) ⇒ Object
Export the key pattern.
- #export_string_data(key_patterns, filename) ⇒ Object
-
#import_data(file) ⇒ Object
Imports a line-by-line json string.
- #reconnect ⇒ Object
- #redis ⇒ Object
- #redis_config ⇒ Object
- #redis_config=(redis_conf) ⇒ Object
Instance Method Details
#cache(key, params = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/redis_utility.rb', line 112 def cache(key, params = {}) expire = params[:expire] recalculate = params[:recalculate] if recalculate || (value = redis.get(key)).nil? value = MultiJson.encode(yield(self)) redis.set(key, value) redis.expire(key, expire) if expire end MultiJson.decode(value) end |
#cache_string(key, params = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/redis_utility.rb', line 98 def cache_string(key, params = {}) expire = params[:expire] recalculate = params[:recalculate] if recalculate || (value = redis.get(key)).nil? value = yield(self).to_s redis.set(key, value) redis.expire(key, expire) if expire end value end |
#export_data(key_patterns, filename) ⇒ Object
Export the key pattern
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/redis_utility.rb', line 56 def export_data(key_patterns, filename) key_patterns = [key_patterns] if key_patterns.is_a? String File.open(filename, 'w+b') do |f| key_patterns.each do |kp| allkeys = kp.include?('*') ? redis.keys(kp).sort : [kp] print "Working on #{kp}: #{quantity_with_unit(allkeys.size, 'key')}\n" nstart = 0 while nstart < allkeys.size keys = allkeys[nstart...nstart + KEY_BATCH_SIZE] types = redis.pipelined { keys.each { |k| redis.type(k) } } # print "Got types\n" string_keys = [] pkeys = [] pvals = redis.pipelined do keys.each_with_index do |key, idx| case types[idx] when 'string' string_keys << key when 'hash' pkeys << key redis.hgetall(key) when 'list' pkeys << key redis.lrange(key, 0, -1) when 'zset' pkeys << key redis.zrange(key, 0, -1, with_scores: true) else print "RedisUtility: Can not deal with #{types[idx]} for key #{key}, skipped\n" end end end write_pipelined_results(pkeys, pvals, f) write_string_keys(string_keys, f) nstart += KEY_BATCH_SIZE print '.' if nstart < allkeys.size end end end end |
#export_string_data(key_patterns, filename) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/redis_utility.rb', line 126 def export_string_data(key_patterns, filename) key_patterns = [key_patterns] if key_patterns.is_a? String File.open(filename, 'w') do |f| key_patterns.each do |kp| keys = redis.keys(kp) write_string_keys(keys, f) end end end |
#import_data(file) ⇒ Object
Imports a line-by-line json string
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/redis_utility.rb', line 16 def import_data(file) bzip2_openfile(file, 'rb') do |f1| until f1.eof? keys = 0 redis.pipelined do # rubocop:disable Lint/AssignmentInCondition while curr_line = f1.gets # rubocop:enable Lint/AssignmentInCondition line = JSON.parse(curr_line) line.each do |key, val| keys += 1 # first delete the record from the server before adding new value case val when Hash redis.del key redis.mapped_hmset(key, val) when Array redis.del key if val[0].is_a?(Array) && val[0][1].is_a?(Float) # zset val = val.map { |v| [v[1], v[0]] } redis.zadd(key, val) else redis.rpush(key, val) end else redis.set(key, val) end end # Done with the line if keys > KEY_BATCH_SIZE print '.' break end end end end end end |
#reconnect ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/redis_utility.rb', line 155 def reconnect if @redis @redis._client.disconnect @redis = nil redis # This reconnects to redis with right configurations end nil end |
#redis ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/redis_utility.rb', line 145 def redis unless @redis # print "RedisUtility: Connecting\n" cfg = redis_config.dup cfg[:timeout] = 60 # Set longer timeout for efficient bulk loading/save @redis = Redis.new(cfg) end @redis end |
#redis_config ⇒ Object
137 138 139 |
# File 'lib/redis_utility.rb', line 137 def redis_config @redis_config end |
#redis_config=(redis_conf) ⇒ Object
141 142 143 |
# File 'lib/redis_utility.rb', line 141 def redis_config=(redis_conf) @redis_config = redis_conf end |