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

Class Method Summary collapse

Class Attribute Details

.poolsObject

Returns the value of attribute pools.



19
20
21
# File 'lib/norton.rb', line 19

def pools
  @pools
end

.redisObject

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

Examples:

Norton.mget([a_user, another_user], [:followers_count, :profile_updated_at])

Parameters:

  • names (Array)

    需要检索的字段

Returns:

  • (Array)

    一组对象



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.

Examples:

{
  "default" => { "url" => "redis://localhost:6379/0", "pool" => 4, "timeout" => 2 },
  "norton2" => { "url" => "redis://localhost:6379/3", "pool" => 4, "timeout" => 2 }
}

Parameters:

  • options={}
    Hash
    Redis connection configuration

    url - Redis connection url pool - Connection pool size timeout - Connection pool timeout

Returns:

  • (Void)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/norton.rb', line 42

def setup(options = {})
  self.pools = {}
  options.deep_symbolize_keys!

  options.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