Class: Cash::Buffered
- Inherits:
-
Object
show all
- Defined in:
- lib/cash/buffered.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(memcache, lock) ⇒ Buffered
Returns a new instance of Buffered.
11
12
13
14
15
16
|
# File 'lib/cash/buffered.rb', line 11
def initialize(memcache, lock)
@buffer = {}
@commands = []
@cache = memcache
@lock = lock
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
109
110
111
|
# File 'lib/cash/buffered.rb', line 109
def method_missing(method, *args, &block)
@cache.send(method, *args, &block)
end
|
Class Method Details
.push(cache, lock) ⇒ Object
3
4
5
6
7
8
9
|
# File 'lib/cash/buffered.rb', line 3
def self.push(cache, lock)
if cache.is_a?(Buffered)
cache.push
else
Buffered.new(cache, lock)
end
end
|
Instance Method Details
#add(key, value, *options) ⇒ Object
63
64
65
66
|
# File 'lib/cash/buffered.rb', line 63
def add(key, value, *options)
@buffer[key] = value
buffer_command Command.new(:add, key, value, *options)
end
|
#decr(key, amount = 1) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/cash/buffered.rb', line 55
def decr(key, amount = 1)
return unless value = get(key, true)
@buffer[key] = [value.to_i - amount, 0].max
buffer_command Command.new(:decr, key, amount)
@buffer[key]
end
|
#delete(key, *options) ⇒ Object
68
69
70
71
|
# File 'lib/cash/buffered.rb', line 68
def delete(key, *options)
@buffer[key] = nil
buffer_command Command.new(:delete, key, *options)
end
|
#flush ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/cash/buffered.rb', line 78
def flush
sorted_keys = @commands.select(&:requires_lock?).collect(&:key).uniq.sort
sorted_keys.each do |key|
@lock.acquire_lock(key)
end
perform_commands
ensure
@buffer = {}
sorted_keys.each do |key|
@lock.release_lock(key)
end
end
|
#get(key, *options) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/cash/buffered.rb', line 26
def get(key, *options)
if @buffer.has_key?(key)
if @buffer[key].is_a?(Array) && @buffer[key][0].is_a?(ActiveRecord::Base)
@buffer[key].map(&:shallow_clone)
elsif @buffer[key].is_a?(ActiveRecord::Base)
@buffer[key].shallow_clone
else
@buffer[key]
end
else
@buffer[key] = @cache.get(key, *options)
end
end
|
#get_multi(*keys) ⇒ Object
73
74
75
76
|
# File 'lib/cash/buffered.rb', line 73
def get_multi(*keys)
values = keys.collect { |key| get(key) }
keys.zip(values).to_hash_without_nils
end
|
#incr(key, amount = 1) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/cash/buffered.rb', line 47
def incr(key, amount = 1)
return unless value = get(key, true)
@buffer[key] = value.to_i + amount
buffer_command Command.new(:incr, key, amount)
@buffer[key]
end
|
#pop ⇒ Object
18
19
20
|
# File 'lib/cash/buffered.rb', line 18
def pop
@cache
end
|
#push ⇒ Object
22
23
24
|
# File 'lib/cash/buffered.rb', line 22
def push
NestedBuffered.new(self, @lock)
end
|
#respond_to?(method) ⇒ Boolean
91
92
93
|
# File 'lib/cash/buffered.rb', line 91
def respond_to?(method)
@cache.respond_to?(method)
end
|
#set(key, value, *options) ⇒ Object
42
43
44
45
|
# File 'lib/cash/buffered.rb', line 42
def set(key, value, *options)
@buffer[key] = value
buffer_command Command.new(:set, key, value, *options)
end
|