Class: Cash::Buffered

Inherits:
Object
  • Object
show all
Defined in:
lib/cash/buffered.rb

Direct Known Subclasses

NestedBuffered

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 (private)



101
102
103
# File 'lib/cash/buffered.rb', line 101

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



55
56
57
58
# File 'lib/cash/buffered.rb', line 55

def add(key, value, *options)
  @buffer[key] = value
  buffer_command Command.new(:add, key, value, *options)
end

#decr(key, amount = 1) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cash/buffered.rb', line 47

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



60
61
62
63
# File 'lib/cash/buffered.rb', line 60

def delete(key, *options)
  @buffer[key] = nil
  buffer_command Command.new(:delete, key, *options)
end

#flushObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cash/buffered.rb', line 70

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
# File 'lib/cash/buffered.rb', line 26

def get(key, *options)
  if @buffer.has_key?(key)
    @buffer[key]
  else
    @buffer[key] = @cache.get(key, *options)
  end
end

#get_multi(*keys) ⇒ Object



65
66
67
68
# File 'lib/cash/buffered.rb', line 65

def get_multi(*keys)
  values = keys.collect { |key| get(key) }
  keys.zip(values).to_hash_without_nils
end

#incr(key, amount = 1) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/cash/buffered.rb', line 39

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

#popObject



18
19
20
# File 'lib/cash/buffered.rb', line 18

def pop
  @cache
end

#pushObject



22
23
24
# File 'lib/cash/buffered.rb', line 22

def push
  NestedBuffered.new(self, @lock)
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/cash/buffered.rb', line 83

def respond_to?(method)
  @cache.respond_to?(method)
end

#set(key, value, *options) ⇒ Object



34
35
36
37
# File 'lib/cash/buffered.rb', line 34

def set(key, value, *options)
  @buffer[key] = value
  buffer_command Command.new(:set, key, value, *options)
end