Class: Remix::Stash

Inherits:
Object show all
Defined in:
lib/remix/stash.rb

Defined Under Namespace

Modules: Extension, Protocol Classes: Cluster, ClusterError, ProtocolError

Constant Summary collapse

@@instances =
{}
@@clusters =
{:default => Cluster.new(%w[localhost:11211])}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Stash

Returns a new instance of Stash.



32
33
34
35
36
37
38
39
40
41
# File 'lib/remix/stash.rb', line 32

def initialize(name)
  @name = name
  @scope = nil
  if name == :root
    @local = @opts = {:coherency => :action, :ttl => 0, :cluster => :default}
  else
    @local = {}
    @opts = stash.default.dup
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/remix/stash.rb', line 8

def name
  @name
end

Class Method Details

.cluster(name) ⇒ Object



13
14
15
# File 'lib/remix/stash.rb', line 13

def self.cluster(name)
  @@clusters[name]
end

.cycle_actionObject



17
18
19
20
# File 'lib/remix/stash.rb', line 17

def self.cycle_action
  @@instances.each {|name, stash|
    stash.cycle if stash.default[:coherency] == :action}
end

.define_cluster(clusters) ⇒ Object



22
23
24
25
26
# File 'lib/remix/stash.rb', line 22

def self.define_cluster(clusters)
  clusters.each do |k,v|
    @@clusters[k] = Cluster === v ? v : Cluster.new(v)
  end
end

.new(name) ⇒ Object



28
29
30
# File 'lib/remix/stash.rb', line 28

def self.new(name)
  @@instances[name] ||= super
end

Instance Method Details

#add(*keys) ⇒ Object



43
44
45
46
47
48
# File 'lib/remix/stash.rb', line 43

def add(*keys)
  opts = default_opts(keys)
  value = keys.pop
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.add(io, key, value, opts[:ttl])}
end

#clear(*keys) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/remix/stash.rb', line 50

def clear(*keys)
  opts = default_opts(keys)
  if keys.empty?
    if @name == :root
      cluster(opts).each {|io| Protocol.flush(io)}
    else
      vk = vector_key
      cluster(opts).select(vk) {|io|
        unless Protocol.incr(io, vk, 1)
          Protocol.add(io, vk, '0')
          Protocol.incr(io, vk, 1)
        end
      }
    end
    cycle
  else
    # remove a specific key
    key = canonical_key(keys, opts)
    cluster(opts).select(key) {|io| Protocol.delete(io, key)}
  end
end

#clear_scopeObject



72
73
74
# File 'lib/remix/stash.rb', line 72

def clear_scope
  @scope = nil
end

#cycleObject



76
77
78
# File 'lib/remix/stash.rb', line 76

def cycle
  @vector = nil
end

#decr(*keys) ⇒ Object



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

def decr(*keys)
  opts = default_opts(keys)
  step = keys.pop
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.decr(io, key, step)}
end

#default(opts = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/remix/stash.rb', line 87

def default(opts = nil)
  if opts
    if opts.has_key? :coherency
      [:dynamic, :action, :transaction].include?(opts[:coherency]) or raise ArgumentError,
        "Invalid coherency setting used (#{opts[:coherency].inspect})"
    end
    @local.merge!(opts)
    @opts.merge!(opts)
    if @name == :root
      @@instances.each do |name, stash|
        stash.update_options unless name == :root
      end
    end
  end
  @opts
end

#delete(*keys) ⇒ Object



104
105
106
107
108
# File 'lib/remix/stash.rb', line 104

def delete(*keys)
  opts = default_opts(keys)
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.delete(io, key)}
end

#eval(*keys) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/remix/stash.rb', line 110

def eval(*keys)
  opts = default_opts(keys)
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io|
    value = Protocol.get(io, key)
    if value
      Marshal.load(value)
    else
      value = yield(*keys)
      Protocol.set(io, key, dump_value(value), opts[:ttl])
      value
    end
  }
end

#gate(*keys) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/remix/stash.rb', line 125

def gate(*keys)
  opts = default_opts(keys)
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io|
    if Protocol.get(io, key)
      yield(*keys)
      true
    else
      false
    end
  }
end

#get(*keys) ⇒ Object Also known as: []



138
139
140
141
142
# File 'lib/remix/stash.rb', line 138

def get(*keys)
  opts = default_opts(keys)
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| load_value(Protocol.get(io, key))}
end

#incr(*keys) ⇒ Object



145
146
147
148
149
150
# File 'lib/remix/stash.rb', line 145

def incr(*keys)
  opts = default_opts(keys)
  step = keys.pop
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.incr(io, key, step)}
end

#read(*keys) ⇒ Object



152
153
154
155
156
# File 'lib/remix/stash.rb', line 152

def read(*keys)
  opts = default_opts(keys)
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.get(io, key)}
end

#releaseObject



158
159
160
# File 'lib/remix/stash.rb', line 158

def release
  @@instances.delete(@name)
end

#scope(&b) ⇒ Object



162
163
164
165
# File 'lib/remix/stash.rb', line 162

def scope(&b)
  @scope = b
  self
end

#set(*keys) ⇒ Object Also known as: []=



167
168
169
170
171
172
# File 'lib/remix/stash.rb', line 167

def set(*keys)
  opts = default_opts(keys)
  value = keys.pop
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.set(io, key, dump_value(value), opts[:ttl])}
end

#transactionObject



175
176
177
178
179
# File 'lib/remix/stash.rb', line 175

def transaction
  yield self
ensure
  cycle
end

#write(*keys) ⇒ Object



181
182
183
184
185
186
# File 'lib/remix/stash.rb', line 181

def write(*keys)
  opts = default_opts(keys)
  value = keys.pop
  key = canonical_key(keys, opts)
  cluster(opts).select(key) {|io| Protocol.set(io, key, value, opts[:ttl])}
end