Module: Cacher

Extended by:
Cacher
Included in:
Cacher, Base
Defined in:
lib/cacher.rb,
lib/cacher/version.rb

Defined Under Namespace

Classes: Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cacher.rb', line 47

def cache
  return @cache if instance_variable_defined? :@cache

  Cacher.cache ||= if defined?(::Rails) and ::Rails.respond_to? :cache
    Rails.cache
  elsif defined?(::RAILS_CACHE)
    RAILS_CACHE
  else
    raise ArgumentError.new <<-msg.strip
      please define a cache backend for Cacher.
    msg
  end

  @cache = Cacher.cache
end

#enabled=(value) ⇒ Object (writeonly)

Sets the attribute enabled

Parameters:

  • value

    the value to set the attribute enabled to.



84
85
86
# File 'lib/cacher.rb', line 84

def enabled=(value)
  @enabled = value
end

#marshal=(value) ⇒ Object (writeonly)

Sets the attribute marshal

Parameters:

  • value

    the value to set the attribute marshal to.



78
79
80
# File 'lib/cacher.rb', line 78

def marshal=(value)
  @marshal = value
end

#max_key_sizeObject



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

def max_key_size
  @max_key_size ||= Cacher.max_key_size
end

#namespaceObject



64
65
66
67
# File 'lib/cacher.rb', line 64

def namespace
  return @namespace if instance_variable_defined? :@namespace
  @namespace = Cacher.namespace
end

Class Method Details

.reset!Object

sets “factory defaults”



15
16
17
18
19
20
21
22
23
24
# File 'lib/cacher.rb', line 15

def self.reset!
  self.cache = nil
  self.namespace = false
  self.max_key_size = 250
  # default to false because Rails.cache handles marshalling by default
  self.marshal = false
  disable!

  self
end

.versionObject



2
3
4
# File 'lib/cacher/version.rb', line 2

def self.version
  '0.1.0'
end

Instance Method Details

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Cacher)

    the object that the method was called on



26
27
28
29
# File 'lib/cacher.rb', line 26

def configure
  yield self
  self
end

#disable!Object



89
90
91
# File 'lib/cacher.rb', line 89

def disable!
  @enabled = false
end

#enable!Object



85
86
87
# File 'lib/cacher.rb', line 85

def enable!
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/cacher.rb', line 93

def enabled?
  return @enabled if instance_variable_defined? :@enabled
  @enabled = Cacher.enabled?
end

#get(key, options = {}, &blk) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cacher.rb', line 110

def get(key, options={}, &blk)
  return set(key, options, &blk) if options.delete(:break)

  cached = cache_get(key)

  if cached.nil?
    return blk && set(key, options, &blk)
  end

  unmarshal_value(cached)
end

#initialize(opts = {}, &blk) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cacher.rb', line 31

def initialize(opts={}, &blk)
  opts.each do |k, v|
    send(:"#{k}=", v)
  end

  blk && configure(&blk)
end

#key?(key) ⇒ Boolean

core api ####

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/cacher.rb', line 104

def key?(key)
  return false unless enabled?

  !!cache_get(key)
end

#marshal?Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/cacher.rb', line 79

def marshal?
  return @marshal if instance_variable_defined? :@marshal
  @marshal = Cacher.marshal?
end

#namespaced?Boolean

Returns:

  • (Boolean)


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

def namespaced?
  !!namespace
end

#set(key, options = {}, &blk) ⇒ Object



122
123
124
125
126
# File 'lib/cacher.rb', line 122

def set(key, options={}, &blk)
  val = do_block(&blk)
  cache_set(key, marshal_value(val), options)
  val
end