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



114
115
116
117
118
119
120
121
# File 'lib/cash/mock.rb', line 114

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



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

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

#decr(key, amount = 1) ⇒ Object



107
108
109
110
111
112
# File 'lib/cash/mock.rb', line 107

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



90
91
92
93
94
95
96
97
98
# File 'lib/cash/mock.rb', line 90

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



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

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
85
86
87
88
# File 'lib/cash/mock.rb', line 70

def get(key, raw = false)
  if key.is_a?(Array)
    get_multi(*key)
  else
    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
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)


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

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

#incr(key, amount = 1) ⇒ Object



100
101
102
103
104
105
# File 'lib/cash/mock.rb', line 100

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

#log(message) ⇒ Object



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

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

#loggerObject



153
154
155
# File 'lib/cash/mock.rb', line 153

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

#namespaceObject



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

def namespace
  nil
end

#reset_runtimeObject



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

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



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

def stats
  {}
end