Module: Familia

Includes:
Gibbler::Complex
Included in:
Bone, Customer, Session
Defined in:
lib/familia.rb,
lib/familia.rb,
lib/familia.rb,
lib/familia/tools.rb,
lib/familia/object.rb,
lib/familia/helpers.rb,
lib/familia/redisobject.rb

Defined Under Namespace

Modules: ClassMethods, Collector, InstanceMethods, Stamps, Status, Tools, VERSION Classes: HashKey, List, NoIndex, NonUniqueKey, NotConnected, Problem, RedisObject, Set, SortedSet, String

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.apiversion(r = nil, &blk) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/familia.rb', line 101

def apiversion(r=nil, &blk)  
  if blk.nil?
    @apiversion = r if r; 
  else
    tmp = @apiversion
    @apiversion = r
    blk.call
    @apiversion = tmp
  end
  @apiversion 
end

.classesObject (readonly)

Returns the value of attribute classes.



35
36
37
# File 'lib/familia.rb', line 35

def classes
  @classes
end

.clientsObject (readonly)

Returns the value of attribute clients.



35
36
37
# File 'lib/familia.rb', line 35

def clients
  @clients
end

.debugObject

Returns the value of attribute debug.



36
37
38
# File 'lib/familia.rb', line 36

def debug
  @debug
end

.delimObject

Returns the value of attribute delim.



36
37
38
# File 'lib/familia.rb', line 36

def delim
  @delim
end

.dump_methodObject

Returns the value of attribute dump_method.



36
37
38
# File 'lib/familia.rb', line 36

def dump_method
  @dump_method
end

.load_methodObject

Returns the value of attribute load_method.



36
37
38
# File 'lib/familia.rb', line 36

def load_method
  @load_method
end

.secretObject

Returns the value of attribute secret.



36
37
38
# File 'lib/familia.rb', line 36

def secret
  @secret
end

.uriObject

Returns the value of attribute uri.



35
36
37
# File 'lib/familia.rb', line 35

def uri
  @uri
end

Class Method Details

.connect(uri = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/familia.rb', line 71

def connect(uri=nil)
  uri &&= URI.parse uri if String === uri
  uri ||= Familia.uri
  conf = uri.conf
  conf[:thread_safe] = true
  client = Redis.new conf
  Familia.trace :CONNECT, client, conf.inspect, caller[0..3] if Familia.debug
  @clients[uri.serverid] = client
end

.connected?(uri = nil) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/familia.rb', line 86

def connected?(uri=nil)
  uri &&= URI.parse uri if String === uri
  @clients.has_key?(uri.serverid)
end

.debug?Boolean

Returns:

  • (Boolean)


38
# File 'lib/familia.rb', line 38

def debug?() @debug == true end

.default_suffix(a = nil) ⇒ Object



90
# File 'lib/familia.rb', line 90

def default_suffix(a=nil) @suffix = a if a; @suffix end

.default_suffix=(a) ⇒ Object



91
# File 'lib/familia.rb', line 91

def default_suffix=(a) @suffix = a end

.included(obj) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/familia.rb', line 127

def self.included(obj)
  obj.send :include, Familia::InstanceMethods
  obj.send :include, Gibbler::Complex
  obj.extend Familia::ClassMethods
  obj.class_zset :instances, :class => obj, :reference => true
  # :object is a special redis object because its reserved
  # for storing the marshaled instance data (e.g. to_json).
  # When it isn't defined explicitly we define it here b/c
  # it's assumed to exist in other places (see #save).
  obj.string :object, :class => obj unless obj.redis_object? :object
  Familia.classes << obj
end

.index(r = nil) ⇒ Object



92
# File 'lib/familia.rb', line 92

def index(r=nil)  @index = r if r; @index end

.index=(r) ⇒ Object



93
# File 'lib/familia.rb', line 93

def index=(r) @index = r; r end

.info(*msg) ⇒ Object



39
40
41
# File 'lib/familia.rb', line 39

def info *msg
  STDERR.puts *msg
end

.join(*r) ⇒ Object



94
# File 'lib/familia.rb', line 94

def join(*r) r.join(Familia.delim) end

.ld(*msg) ⇒ Object



42
43
44
# File 'lib/familia.rb', line 42

def ld *msg
  info *msg if debug?
end

.reconnect_all!Object



80
81
82
83
84
85
# File 'lib/familia.rb', line 80

def reconnect_all!
  Familia.classes.each do |klass|
    klass.redis.client.reconnect
    Familia.info "#{klass} ping: #{klass.redis.ping}" if debug?
  end
end

.redis(uri = nil) ⇒ Object

A convenience method for returning the appropriate Redis connection. If uri is an Integer, we’ll treat it as a database number. If it’s a String, we’ll treat it as a full URI (e.g. redis://1.2.3.4/15). Otherwise we’ll return the default connection.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/familia.rb', line 59

def redis(uri=nil)
  if Integer === uri
    tmp = Familia.uri
    tmp.db = uri
    uri = tmp
  elsif String === uri
    uri &&= URI.parse uri
  end
  uri ||= Familia.uri
  connect(uri) unless @clients[uri.serverid] 
  @clients[uri.serverid]
end

.rediskey(*args) ⇒ Object



96
97
98
99
100
# File 'lib/familia.rb', line 96

def rediskey *args
  el = args.flatten.compact
  el.unshift @apiversion unless @apiversion.nil?
  el.join(Familia.delim)
end

.split(r) ⇒ Object



95
# File 'lib/familia.rb', line 95

def split(r) r.split(Familia.delim) end

.trace(label, redis_client, ident, context = nil) ⇒ Object



45
46
47
48
49
# File 'lib/familia.rb', line 45

def trace label, redis_client, ident, context=nil
  return unless Familia.debug?
  info "%s (%d:%s): %s" % [label, Thread.current.object_id, redis_client.object_id, ident] 
  info "  +-> %s" % [context].flatten[0..3].join("\n      ") if context
end