Class: Cash::Mock

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

Defined Under Namespace

Classes: CacheEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMock

Returns a new instance of Mock.



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

def initialize
  @logging = false
end

Instance Attribute Details

#loggingObject

Returns the value of attribute logging.



54
55
56
# File 'lib/cash/mock.rb', line 54

def logging
  @logging
end

#serversObject

Returns the value of attribute servers.



3
4
5
# File 'lib/cash/mock.rb', line 3

def servers
  @servers
end

Instance Method Details

#add(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/cash/mock.rb', line 110

def add(key, value, ttl = CacheEntry.default_ttl, raw = false)
  if self.has_unexpired_key?(key)
    "NOT_STORED\r\n"
  else
    set(key, value, ttl, raw)
    "STORED\r\n"
  end
end

#append(key, value) ⇒ Object



119
120
121
# File 'lib/cash/mock.rb', line 119

def append(key, value)
  set(key, get(key, true).to_s + value.to_s, nil, true)
end

#decr(key, amount = 1) ⇒ Object



103
104
105
106
107
108
# File 'lib/cash/mock.rb', line 103

def decr(key, amount = 1)
  if self.has_unexpired_key?(key)
    self[key].decrement(amount)
    self[key].to_i
  end
end

#delete(key, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/cash/mock.rb', line 86

def delete(key, options = {})
  log "< delete #{key}"
  if self.has_unexpired_key?(key)
    log "> DELETED"
    super(key)
  else
    log "> NOT FOUND"
  end
end

#flush_allObject



127
128
129
130
# File 'lib/cash/mock.rb', line 127

def flush_all
  log('< flush_all')
  clear
end

#get(key, raw = false) ⇒ Object



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

def get(key, raw = false)
  log "< get #{key}"
  unless self.has_unexpired_key?(key)
    log('> END')
    return nil
  end
  
  log("> sending key #{key}")
  log('> END')
  if raw
    self[key].value
  else
    self[key].unmarshal
  end
end

#get_multi(keys) ⇒ Object



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

def get_multi(keys)
  slice(*keys).collect { |k,v| [k, v.unmarshal] }.to_hash_without_nils
end

#has_unexpired_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/cash/mock.rb', line 140

def has_unexpired_key?(key)
  self.has_key?(key) && !self[key].expired?
end

#incr(key, amount = 1) ⇒ Object



96
97
98
99
100
101
# File 'lib/cash/mock.rb', line 96

def incr(key, amount = 1)
  if self.has_unexpired_key?(key)
    self[key].increment(amount)
    self[key].to_i
  end
end

#log(message) ⇒ Object



144
145
146
147
# File 'lib/cash/mock.rb', line 144

def log(message)
  return unless logging
  logger.debug(message)
end

#loggerObject



149
150
151
# File 'lib/cash/mock.rb', line 149

def logger
  @logger ||= ActiveSupport::BufferedLogger.new(Rails.root.join('log/cash_mock.log'))
end

#namespaceObject



123
124
125
# File 'lib/cash/mock.rb', line 123

def namespace
  nil
end

#reset_runtimeObject



136
137
138
# File 'lib/cash/mock.rb', line 136

def reset_runtime
  [0, Hash.new(0)]
end

#set(key, value, ttl = CacheEntry.default_ttl, raw = false) ⇒ Object



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

def set(key, value, ttl = CacheEntry.default_ttl, raw = false)
  log "< set #{key} #{ttl}"
  self[key] = CacheEntry.new(value, raw, ttl)
  log('> STORED')
end

#statsObject



132
133
134
# File 'lib/cash/mock.rb', line 132

def stats
  {}
end