Class: Ramaze::Cache

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/ramaze/cache.rb

Overview

This is the wrapper of all caches, providing mechanism for switching caching from one adapter to another.

Constant Summary collapse

CACHES =

Holds all the instances of Cache that are being added.

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = Global.cache) ⇒ Cache

Initializes the cache, defined by Global.cache



59
60
61
# File 'lib/ramaze/cache.rb', line 59

def initialize(cache = Global.cache)
  @cache = cache.new
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



19
20
21
# File 'lib/ramaze/cache.rb', line 19

def cache
  @cache
end

Class Method Details

.add(*keys) ⇒ Object

This will define a method to access a new cache directly over singleton-methods on Cache.


The @cache_name is internally used for caches which do not save different caches in different namespaces, for example memcached. +++



39
40
41
42
43
44
45
# File 'lib/ramaze/cache.rb', line 39

def add *keys
  keys.each{|key|
    klass = Global.cache_alternative.fetch(key, MemoryCache)
    add_on(key, klass)
  }
  Log.dev("Added caches for: #{keys.join(', ')}")
end

.add_on(key, cache_class) ⇒ Object

Define a new cache available by Cache::key



48
49
50
51
52
53
54
# File 'lib/ramaze/cache.rb', line 48

def add_on(key, cache_class)
  CACHES[key] = new(cache_class)
  CACHES[key].instance_variable_set("@cache_name", key)
  self.class.class_eval do
    define_method(key){ CACHES[key] }
  end
end

.startup(options) ⇒ Object

Initializes the Cache for the general caches Ramaze uses. Cache#startup is called by Ramaze#startup, when initializing the Ramaze.trait(:essentials).



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

def startup(options)
  Cache.add :compiled, :actions, :patterns,
            :resolved, :shield, :action_methods
end

Instance Method Details

#[](key) ⇒ Object

Gets the value for the given key, or nil if not found.



64
65
66
# File 'lib/ramaze/cache.rb', line 64

def [](key)
  fetch(key)
end

#[]=(key, value) ⇒ Object

Sets key to value with an infinite time to live.



69
70
71
# File 'lib/ramaze/cache.rb', line 69

def []=(key, value)
  store(key, value)
end

#clearObject

Empties this cache.



74
75
76
# File 'lib/ramaze/cache.rb', line 74

def clear
  @cache.clear
end

#delete(*args) ⇒ Object

Deletes each passed key from this cache.



79
80
81
82
83
# File 'lib/ramaze/cache.rb', line 79

def delete(*args)
  args.each do |arg|
    @cache.delete("#{@cache_name}:#{arg}")
  end
end

#fetch(key, default = nil) ⇒ Object Also known as: get

Gets the value of the given key, or default if not found.



86
87
88
89
90
91
# File 'lib/ramaze/cache.rb', line 86

def fetch(key, default = nil)
  return default unless entry = @cache["#{@cache_name}:#{key}"]
  return entry[:value] if entry[:expires].nil? || entry[:expires] > Time.now
  @cache.delete("#{@cache_name}:#{key}")
  default
end

#store(key, value, opts = {}) ⇒ Object Also known as: set

Sets key to value. Supports the following options:

[+:ttl+] time to live in seconds


97
98
99
100
101
102
103
104
# File 'lib/ramaze/cache.rb', line 97

def store(key, value, opts = {})
  opts = {:ttl => opts} if opts.is_a?(Integer)
  @cache["#{@cache_name}:#{key}"] = {
    :expires => opts[:ttl] ? Time.now + opts[:ttl].to_i : nil,
    :value   => value
  }
  value
end

#values_at(*keys) ⇒ Object

Answers with value for each key.



109
110
111
112
113
# File 'lib/ramaze/cache.rb', line 109

def values_at(*keys)
  values = []
  keys.each {|key| values << fetch(key) }
  values
end