Module: Cacher
Defined Under Namespace
Classes: Base
Instance Attribute Summary collapse
- #cache ⇒ Object
-
#enabled ⇒ Object
writeonly
Sets the attribute enabled.
-
#marshal ⇒ Object
writeonly
Sets the attribute marshal.
- #max_key_size ⇒ Object
- #namespace ⇒ Object
Class Method Summary collapse
-
.reset! ⇒ Object
sets “factory defaults”.
- .version ⇒ Object
Instance Method Summary collapse
- #configure {|_self| ... } ⇒ Object
- #disable! ⇒ Object
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
- #get(key, options = {}, &blk) ⇒ Object
- #initialize(opts = {}, &blk) ⇒ Object
-
#key?(key) ⇒ Boolean
core api ####.
- #marshal? ⇒ Boolean
- #namespaced? ⇒ Boolean
- #set(key, options = {}, &blk) ⇒ Object
Instance Attribute Details
#cache ⇒ Object
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
84 85 86 |
# File 'lib/cacher.rb', line 84 def enabled=(value) @enabled = value end |
#marshal=(value) ⇒ Object (writeonly)
Sets the attribute marshal
78 79 80 |
# File 'lib/cacher.rb', line 78 def marshal=(value) @marshal = value end |
#max_key_size ⇒ Object
74 75 76 |
# File 'lib/cacher.rb', line 74 def max_key_size @max_key_size ||= Cacher.max_key_size end |
#namespace ⇒ Object
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 |
.version ⇒ Object
2 3 4 |
# File 'lib/cacher/version.rb', line 2 def self.version '0.1.0' end |
Instance Method Details
#configure {|_self| ... } ⇒ Object
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
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, ={}, &blk) return set(key, , &blk) if .delete(:break) cached = cache_get(key) if cached.nil? return blk && set(key, , &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 ####
104 105 106 107 108 |
# File 'lib/cacher.rb', line 104 def key?(key) return false unless enabled? !!cache_get(key) end |
#marshal? ⇒ 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
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, ={}, &blk) val = do_block(&blk) cache_set(key, marshal_value(val), ) val end |