Module: Legion::Cache::Memcached

Extended by:
Memcached
Includes:
Pool
Included in:
Legion::Cache, Memcached
Defined in:
lib/legion/cache/memcached.rb

Instance Method Summary collapse

Methods included from Pool

#available, #close, #connected?, #pool_size, #restart, #size, #timeout

Instance Method Details

#client(servers: Legion::Settings[:cache][:servers], **opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/cache/memcached.rb', line 10

def client(servers: Legion::Settings[:cache][:servers], **opts)
  return @client unless @client.nil?

  @pool_size = opts.key?(:pool_size) ? opts[:pool_size] : Legion::Settings[:cache][:pool_size] || 10
  @timeout = opts.key?(:timeout) ? opts[:timeout] : Legion::Settings[:cache][:timeout] || 5

  Dalli.logger = Legion::Logging
  @client = ConnectionPool.new(size: pool_size, timeout: timeout) do
    Dalli::Client.new(servers, Legion::Settings[:cache].merge(opts))
  end

  @connected = true
  @client
end

#delete(key) ⇒ Object



37
38
39
# File 'lib/legion/cache/memcached.rb', line 37

def delete(key)
  client.with { |conn| conn.delete(key) == true }
end

#fetch(key, ttl = nil) ⇒ Object



29
30
31
# File 'lib/legion/cache/memcached.rb', line 29

def fetch(key, ttl = nil)
  client.with { |conn| conn.fetch(key, ttl) }
end

#flush(delay = 0) ⇒ Object



41
42
43
# File 'lib/legion/cache/memcached.rb', line 41

def flush(delay = 0)
  client.with { |conn| conn.flush(delay).first }
end

#get(key) ⇒ Object



25
26
27
# File 'lib/legion/cache/memcached.rb', line 25

def get(key)
  client.with { |conn| conn.get(key) }
end

#set(key, value, ttl = 180) ⇒ Object



33
34
35
# File 'lib/legion/cache/memcached.rb', line 33

def set(key, value, ttl = 180)
  client.with { |conn| conn.set(key, value, ttl).positive? }
end