Class: Memcache::LocalServer

Inherits:
Base show all
Defined in:
lib/memcache/local_server.rb

Instance Attribute Summary

Attributes inherited from Base

#prefix

Instance Method Summary collapse

Methods inherited from Base

#add, #append, #cas, #clear, #decr, #gets, #incr, #prepend, #replace

Constructor Details

#initializeLocalServer

Returns a new instance of LocalServer.



3
4
5
6
# File 'lib/memcache/local_server.rb', line 3

def initialize
  @data = {}
  @expiry = {}
end

Instance Method Details

#delete(key) ⇒ Object



57
58
59
# File 'lib/memcache/local_server.rb', line 57

def delete(key)
  @data.delete(cache_key(key)) && true
end

#flush_all(delay = 0) ⇒ Object



19
20
21
22
23
# File 'lib/memcache/local_server.rb', line 19

def flush_all(delay = 0)
  raise 'flush_all not supported with delay' if delay != 0
  @data.clear
  @expiry.clear
end

#get(keys, cas = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/memcache/local_server.rb', line 25

def get(keys, cas = false)
  if keys.kind_of?(Array)
    hash = {}
    keys.each do |key|
      key = key.to_s
      val = get(key)
      hash[key] = val if val
    end
    hash
  else
    key = cache_key(keys)
    if @expiry[key] and Time.now > @expiry[key]
      @data[key]   = nil
      @expiry[key] = nil
    end
    @data[key]
  end
end

#nameObject



8
9
10
# File 'lib/memcache/local_server.rb', line 8

def name
  "local:#{hash}"
end

#set(key, value, expiry = 0, flags = 0) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/memcache/local_server.rb', line 44

def set(key, value, expiry = 0, flags = 0)
  key = cache_key(key)
  @data[key] = value.to_s
  expiry = Time.at(expiry) if expiry > 60*60*24*30
  if expiry.kind_of?(Time)
    @expiry[key] = expiry
  else
    expiry = expiry.to_i
    @expiry[key] = expiry == 0 ? nil : Time.now + expiry
  end
  value
end

#statsObject



12
13
14
15
16
17
# File 'lib/memcache/local_server.rb', line 12

def stats
  { # curr_items may include items that have expired.
    'curr_items'   => @data.size,
    'expiry_count' => @expiry.size,
  }
end