Class: Merb::Cache::MemcachedStore

Inherits:
AbstractStore show all
Defined in:
merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb

Overview

Memcached store uses one or several Memcached servers for caching. It's flexible and can be used for fragment caching, action caching, page caching or object caching.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from AbstractStore

#delete_all!, #write_all

Constructor Details

- (MemcachedStore) initialize(config = {})

Returns a new instance of MemcachedStore

Parameters:

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

    Configuration options

Options Hash (config):

  • :namespace (String)
  • :servers (Array<String>) — default: ["127.0.0.1:11211"]

    Memcache servers.



15
16
17
18
19
20
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 15

def initialize(config = {})
  @namespace = config[:namespace]
  @servers = config[:servers] || ["127.0.0.1:11211"]

  connect(config)
end

Instance Attribute Details

- (Object) memcached

Returns the value of attribute memcached



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def memcached
  @memcached
end

- (Object) namespace

Returns the value of attribute namespace



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def namespace
  @namespace
end

- (Object) servers

Returns the value of attribute servers



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def servers
  @servers
end

Instance Method Details

- (Object) clone



85
86
87
88
89
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 85

def clone
  twin = super
  twin.memcached = @memcached.clone
  twin
end

- (Object) connect(config = {})

Establishes connection to Memcached.

Parameters:

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

    Configuration options passed to memcached. Only the options mentioned below are passed through.

Options Hash (config):

  • :buffer_requests (Boolean) — default: false

    Use bufferring

  • :no_block (Boolean) — default: false

    Use non-blocking async I/O.

  • :support_cas (Boolean) — default: false

    Support CAS.

See Also:



103
104
105
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 103

def connect(config = {})
  @memcached = ::Memcached.new(@servers, config.only(:buffer_requests, :no_block, :support_cas).merge(:namespace => @namespace))
end

- (Object) delete(key, parameters = {})

Deletes entry from cached by key.



72
73
74
75
76
77
78
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 72

def delete(key, parameters = {})
  begin
    @memcached.delete(normalize(key, parameters))
  rescue Memcached::NotFound
    nil
  end
end

- (Object) delete_all

Flushes the cache.



81
82
83
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 81

def delete_all
  @memcached.flush
end

- (Boolean) exists?(key, parameters = {})

returns true/false/nil based on if data identified by the key & parameters is persisted in the store.

With Memcached 1.2 protocol the only way to find if key exists in the cache is to read it. It is very fast and shouldn't be a concern.

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 61

def exists?(key, parameters = {})
  begin
    @memcached.get(normalize(key, parameters)) && true
  rescue  Memcached::Stored
    true
  rescue Memcached::NotFound
    nil
  end
end

- (Object) expire_time(conditions = {})

Returns expiration timestamp if :expire_in key is given.



114
115
116
117
118
119
120
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 114

def expire_time(conditions = {})
  if t = conditions[:expire_in]
    Time.now + t
  else
    0
  end
end

- (Object) fetch(key, parameters = {}, conditions = {}, &blk)

Fetches cached data by key if it exists. If it does not, uses passed block to get new cached value and writes it using given key.



52
53
54
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 52

def fetch(key, parameters = {}, conditions = {}, &blk)
  read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value)
end

- (Object) normalize(key, parameters = {})

Returns cache key calculated from base key and SHA2 hex from parameters.



109
110
111
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 109

def normalize(key, parameters = {})
  parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}"
end

- (Object) read(key, parameters = {})

Reads key from the cache.



29
30
31
32
33
34
35
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 29

def read(key, parameters = {})
  begin
    @memcached.get(normalize(key, parameters))
  rescue Memcached::NotFound, Memcached::Stored
    nil
  end
end

- (Boolean) writable?(key, parameters = {}, conditions = {})

Memcached store consideres all keys and parameters writable.

Returns:

  • (Boolean)


24
25
26
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 24

def writable?(key, parameters = {}, conditions = {})
  true
end

- (Object) write(key, data = nil, parameters = {}, conditions = {})

Writes data to the cache using key, parameters and conditions.



38
39
40
41
42
43
44
45
46
47
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 38

def write(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    begin
      @memcached.set(normalize(key, parameters), data, expire_time(conditions))
      true
    rescue
      nil
    end
  end
end