Class: Lux::Cache

Inherits:
Object show all
Defined in:
lib/lux/cache/cache.rb

Defined Under Namespace

Classes: MemoryCache, NullCache

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



5
6
7
# File 'lib/lux/cache/cache.rb', line 5

def initialize
  @server = Lux::Cache::MemoryCache.new
end

Instance Method Details

#[](key) ⇒ Object



107
108
109
# File 'lib/lux/cache/cache.rb', line 107

def [] key
  @server.get key.to_s
end

#[]=(key, value) ⇒ Object



102
103
104
105
# File 'lib/lux/cache/cache.rb', line 102

def []= key, value
  @server.set key.to_s, value
  value
end

#delete(key, data = nil) ⇒ Object



49
50
51
# File 'lib/lux/cache/cache.rb', line 49

def delete key, data=nil
  @server.delete(key)
end

#fetch(key, opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lux/cache/cache.rb', line 53

def fetch key, opts={}
  opts = { ttl: opts } unless opts.is_a?(Hash)
  opts = opts.to_opts(:ttl, :force, :log, :if)

  return yield if opts.if.is_a?(FalseClass)

  opts.ttl     = opts.ttl.to_i if opts.ttl
  opts.log   ||= Lux.config(:log_to_stdout)  unless opts.log.class   == FalseClass
  opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass

  @server.delete key if opts.force

  Lux.log " Cache.fetch.get #{key} (ttl: #{opts.ttl.or(:nil)})".green if opts.log

  data = @server.fetch key, opts.ttl do
    speed = Lux.speed { data = yield }

    Lux.log " Cache.fetch.SET #{key} len:#{data.to_s.length} (#{speed})".red if opts.log

    data
  end

  data
end

#generate_key(*data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lux/cache/cache.rb', line 83

def generate_key *data
  keys = []

  for el in [data].flatten
    keys.push el.id if el.respond_to?(:id)

    if el.respond_to?(:updated_at)
      keys.push el.updated_at
    elsif el.respond_to?(:created_at)
      keys.push el.created_at
    else
      keys.push el.to_s
    end
  end

  key = keys.join('-')
  Crypt.sha1(key)
end

#is_available?Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/lux/cache/cache.rb', line 78

def is_available?
  set('lux-test', 9)
  get('lux-test') == 9
end

#read(key) ⇒ Object Also known as: get



32
33
34
35
# File 'lib/lux/cache/cache.rb', line 32

def read key
  return nil if (Lux.current.no_cache? rescue false)
  @server.get(key)
end

#read_multi(*args) ⇒ Object Also known as: get_multi



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

def read_multi *args
  @server.get_multi(*args)
end

#serverObject



28
29
30
# File 'lib/lux/cache/cache.rb', line 28

def server
  @server
end

#server=(name) ⇒ Object

sert cache server Lux.cache.server = :memory Lux.cache.server = :memcached Lux.cache.server = Dalli::Client.new(‘localhost:11211’, { :namespace=>Digest::MD5.hexdigest(__FILE__), :compress => true, :expires_in => 1.hour })



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lux/cache/cache.rb', line 13

def server= name
  @server = if name.is_a?(Symbol)
    if name == :memcached
      require 'dalli'
      Dalli::Client.new('127.0.0.1:11211', { :namespace=>Digest::MD5.hexdigest(__FILE__)[0,4], :compress => true,  :expires_in => 1.hour })
    else
      "Lux::Cache::#{name.to_s.classify}Cache".constantize.new
    end
  else
    name
  end

  fetch('cache-test') { true }
end

#write(key, data, ttl = nil) ⇒ Object Also known as: set



43
44
45
46
# File 'lib/lux/cache/cache.rb', line 43

def write key, data, ttl=nil
  ttl = ttl.to_i if ttl
  @server.set(key, data, ttl)
end