Class: Memcache

Inherits:
Object
  • Object
show all
Defined in:
lib/Memcache.rb

Overview

this file is part of manqod manqod is distributed under the CDDL licence the author of manqod is Dobai-Pataky Balint([email protected])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers, options = {}) ⇒ Memcache

Returns a new instance of Memcache.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/Memcache.rb', line 6

def initialize(servers,options={})
  @prefix_key=options[:prefix_key]
  @memcache_lib=nil
  @memcache=nil
  @version=nil
  begin
    @version=Memcached::VERSION
    einfo("using memcached(#{version})")
    @memcache_lib=:memcached
  rescue =>err
    begin
      @version=Dalli::VERSION
      einfo("falling back memcache to dalli(#{version})")
      @memcache_lib=:dalli
    rescue =>err
      eerror("No memcache lib available, Follow install instructions on http://manqod.sourceforge.net")
    end
  end

  case memcache_lib
    when :memcached
      options[:default_ttl] = 0
      options[:timeout] = 30
      @memcache = Memcached.new(servers,options)
    when :dalli
      options.delete(:prefix_key)
      options[:expires_in]=0
      @memcache = Dalli::Client.new(servers,options)
    else
      eeror("No memcache lib available")
  end
end

Instance Attribute Details

#memcacheObject (readonly)

Returns the value of attribute memcache.



38
39
40
# File 'lib/Memcache.rb', line 38

def memcache
  @memcache
end

#memcache_libObject (readonly)

Returns the value of attribute memcache_lib.



38
39
40
# File 'lib/Memcache.rb', line 38

def memcache_lib
  @memcache_lib
end

#versionObject (readonly)

Returns the value of attribute version.



38
39
40
# File 'lib/Memcache.rb', line 38

def version
  @version
end

Instance Method Details

#get(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/Memcache.rb', line 40

def get(key)
  case memcache_lib
    when :memcached
      begin
        return memcache.get(key)
      rescue Memcached::NotFound
        ewarn("cannot load data for key:#{key.inspect}")
        return nil
      end
    when :dalli
      if key.class.name == "Array"
        values=Hash.new
        key.each{|k| values[k]=dalli_get_one(k)}
        return values
      else
        dalli_get_one(key)
      end
  end
end

#to_sObject



61
62
63
# File 'lib/Memcache.rb', line 61

def to_s
  "Memcache(#{memcache_lib}-#{version})[#{@prefix_key}]"
end