Class: Juno::Adapters::MemcachedNative

Inherits:
Base
  • Object
show all
Defined in:
lib/juno/adapters/memcached_native.rb

Overview

Memcached backend (using gem memcached)

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #close, #fetch

Constructor Details

#initialize(options = {}) ⇒ MemcachedNative

Constructor

Options:

  • :server - Memcached server (default localhost:11211)

  • :namespace - Key namespace

  • Other options passed to Memcached#new

Parameters:

  • options (Hash) (defaults to: {})


16
17
18
19
20
21
# File 'lib/juno/adapters/memcached_native.rb', line 16

def initialize(options = {})
  server = options.delete(:server) || 'localhost:11211'
  options.merge!(:prefix_key => options.delete(:namespace)) if options[:namespace]
  @default_ttl = options[:default_ttl] || 604800
  @cache = ::Memcached.new(server, options)
end

Instance Method Details

#clear(options = {}) ⇒ Object



53
54
55
56
# File 'lib/juno/adapters/memcached_native.rb', line 53

def clear(options = {})
  @cache.flush
  self
end

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



40
41
42
43
44
45
# File 'lib/juno/adapters/memcached_native.rb', line 40

def delete(key, options = {})
  value = @cache.get(key, false)
  @cache.delete(key)
  value
rescue ::Memcached::NotFound
end

#key?(key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# File 'lib/juno/adapters/memcached_native.rb', line 23

def key?(key, options = {})
  @cache.get(key, false)
  true
rescue ::Memcached::NotFound
  false
end

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



30
31
32
33
34
35
36
37
38
# File 'lib/juno/adapters/memcached_native.rb', line 30

def load(key, options = {})
  value = @cache.get(key, false)
  if value && options.include?(:expires)
    store(key, value, options)
  else
    value
  end
rescue ::Memcached::NotFound
end

#store(key, value, options = {}) ⇒ Object



47
48
49
50
51
# File 'lib/juno/adapters/memcached_native.rb', line 47

def store(key, value, options = {})
  # TTL must be Fixnum
  @cache.set(key, value, options[:expires] || @default_ttl, false)
  value
end