Module: Norton
- Defined in:
- lib/norton.rb,
lib/norton/helper.rb,
lib/norton/counter.rb,
lib/norton/version.rb,
lib/norton/hash_map.rb,
lib/norton/timestamp.rb,
lib/norton/objects/hash.rb
Defined Under Namespace
Modules: Counter, HashMap, Helper, Objects, Timestamp Classes: InvalidType, NilObjectId
Constant Summary collapse
- SUPPORTED_TYPES =
%i(counter timestamp hash_map)
- VERSION =
"0.1.3"
Class Attribute Summary collapse
-
.pools ⇒ Object
Returns the value of attribute pools.
-
.redis ⇒ Object
Returns the value of attribute redis.
Class Method Summary collapse
-
.mget(objects, fields) ⇒ Array
批量获取多个对象的多个 Norton 字段, 仅仅支持 counter / timestamp.
-
.setup(options = {}) ⇒ Void
Setup your redis connection.
Class Attribute Details
.pools ⇒ Object
Returns the value of attribute pools.
19 20 21 |
# File 'lib/norton.rb', line 19 def pools @pools end |
.redis ⇒ Object
Returns the value of attribute redis.
20 21 22 |
# File 'lib/norton.rb', line 20 def redis @redis end |
Class Method Details
.mget(objects, fields) ⇒ Array
批量获取多个对象的多个 Norton 字段, 仅仅支持 counter / timestamp
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/norton.rb', line 66 def mget(objects, fields) pools_with_name = fields.each_with_object({}) do |name, hash| pool = objects[0].class.norton_value_redis_pool(name) hash[pool] ||= [] hash[pool] << name end pools_with_name.each do |pool, names| keys = objects.flat_map do |object| names.map { |name| object.norton_value_key(name) } end nested_values = pool.with do |conn| conn.mget(keys) end.each_slice(names.size) objects.zip(nested_values).each do |object, values| object.send(:assign_values, names.zip(values).to_h) end end objects end |
.setup(options = {}) ⇒ Void
Setup your redis connection.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/norton.rb', line 42 def setup( = {}) self.pools = {} .deep_symbolize_keys! .each do |name, conn_params| pool_size = (conn_params.delete(:pool) || 1).to_i timeout = (conn_params.delete(:timeout) || 2).to_i Norton.pools[name] = ConnectionPool.new(size: pool_size, timeout: timeout) do Redis.new(conn_params) end end Norton.redis = Norton.pools[:default] end |