Class: Cache
- Inherits:
-
Object
- Object
- Cache
- Defined in:
- lib/cache.rb,
lib/cache/config.rb,
lib/cache/version.rb
Defined Under Namespace
Modules: ActiveSupportCacheDalliStore, ActiveSupportCacheFileStore, ActiveSupportCacheMemoryStore, ActiveSupportCacheNullStore, ActiveSupportCacheStore, DalliClient, MemCache, Memcached, MemcachedRails, Nothing, Redis, RedisNamespace Classes: Config, DriverNotFound, Error
Constant Summary collapse
- VERSION =
"0.3.6"
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
For compatibility with Rails 2.x.
-
#metal ⇒ Object
readonly
Returns the value of attribute metal.
Instance Method Summary collapse
-
#_cas(k, ttl, &blk) ⇒ Object
Default implementation of #cas which is overridden in some drivers.
-
#_fetch(k, ttl, &blk) ⇒ Object
Default implementation of #fetch for drivers that do not define a method natively for it.
-
#cas(k, ttl = nil, &blk) ⇒ Object
(also: #compare_and_swap)
Get the current value (if any), pass it into a block, and set the result.
-
#decrement(k, amount = 1, ignored_options = nil) ⇒ Object
Decrement a value.
-
#delete(k, ignored_options = nil) ⇒ Object
Delete a value.
-
#exist?(k, ignored_options = nil) ⇒ Boolean
(also: #exists?)
Check if something exists.
-
#fetch(k, ttl = nil, &blk) ⇒ Object
Try to get a value and if it doesn’t exist, set it to the result of the block.
-
#flush ⇒ Object
(also: #clear)
Flush the cache.
-
#get(k, ignored_options = nil) ⇒ Object
(also: #read)
Get a value.
-
#get_multi(*ks) ⇒ Object
Get multiple cache entries.
-
#increment(k, amount = 1, ignored_options = nil) ⇒ Object
Increment a value.
-
#initialize(metal) ⇒ Cache
constructor
Create a new Cache instance by wrapping a client of your choice.
-
#set(k, v, ttl = nil, ignored_options = nil) ⇒ Object
(also: #write)
Store a value.
-
#stats ⇒ Object
Get stats.
Constructor Details
#initialize(metal) ⇒ Cache
Create a new Cache instance by wrapping a client of your choice.
Supported memcached clients:
-
memcached (either a Memcached or a Memcached::Rails)
-
dalli (either a Dalli::Client or an ActiveSupport::Cache::DalliStore)
-
memcache-client (MemCache, the one commonly used by Rails)
Supported Redis clients:
-
redis (Redis)
-
redis-namespace (Redis::Namespace)
metal - Either an instance of one of the above classes, or :nothing if you
want to use the Cache API but not actually cache anything.
Examples:
# Use Memcached
raw_client = Memcached.new('127.0.0.1:11211')
cache = Cache.new(raw_client)
cache.get(...)
cache.set(...)
# Don't cache anything
cache = Cache.new(:nothing)
cache.get(...)
cache.set(...)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cache.rb', line 50 def initialize() @pid = ::Process.pid @config = Config.new if != :nothing @metal = Cache === ? . : end if @metal client_class = @metal.class.to_s driver_class = client_class.gsub('::', '') else client_class = 'Nothing' driver_class = 'Nothing' end filename = client_class. gsub(/([a-z])([A-Z]+)/) { [$1.downcase, $2.downcase].join('_') }. gsub('::', '_').downcase begin require "cache/#{filename}" extend Cache.const_get(driver_class) rescue LoadError, NameError => e puts "#{e.class}: #{e.}" puts e.backtrace[0..5].join("\n") raise DriverNotFound, client_class end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/cache.rb', line 15 def config @config end |
#logger ⇒ Object
For compatibility with Rails 2.x
19 20 21 |
# File 'lib/cache.rb', line 19 def logger @logger end |
#metal ⇒ Object (readonly)
Returns the value of attribute metal.
16 17 18 |
# File 'lib/cache.rb', line 16 def @metal end |
Instance Method Details
#_cas(k, ttl, &blk) ⇒ Object
Default implementation of #cas which is overridden in some drivers
192 193 194 195 196 197 198 199 |
# File 'lib/cache.rb', line 192 def _cas(k, ttl, &blk) if blk and _exist?(k) old_v = _get k new_v = blk.call old_v _set k, new_v, _get_ttl(ttl) new_v end end |
#_fetch(k, ttl, &blk) ⇒ Object
Default implementation of #fetch for drivers that do not define a method natively for it
170 171 172 173 174 175 176 177 178 |
# File 'lib/cache.rb', line 170 def _fetch(k, ttl, &blk) if _exist? k _get k elsif blk v = blk.call _set k, v, ttl v end end |
#cas(k, ttl = nil, &blk) ⇒ Object Also known as: compare_and_swap
Get the current value (if any), pass it into a block, and set the result.
Example:
cache.cas 'hello' { |current| 'world' }
184 185 186 187 |
# File 'lib/cache.rb', line 184 def cas(k, ttl = nil, &blk) handle_fork _cas(k, ttl, &blk) end |
#decrement(k, amount = 1, ignored_options = nil) ⇒ Object
Decrement a value.
Example:
cache.decrement 'high-fives'
153 154 155 |
# File 'lib/cache.rb', line 153 def decrement(k, amount = 1, = nil) increment k, -amount end |
#delete(k, ignored_options = nil) ⇒ Object
Delete a value.
Example:
cache.delete 'hello'
112 113 114 115 |
# File 'lib/cache.rb', line 112 def delete(k, = nil) handle_fork _delete k end |
#exist?(k, ignored_options = nil) ⇒ Boolean Also known as: exists?
Check if something exists.
Example:
cache.exist? 'hello'
132 133 134 135 |
# File 'lib/cache.rb', line 132 def exist?(k, = nil) handle_fork _exist? k end |
#fetch(k, ttl = nil, &blk) ⇒ Object
Try to get a value and if it doesn’t exist, set it to the result of the block.
Accepts :expires_in for compatibility with Rails.
Example:
cache.fetch 'hello' { 'world' }
163 164 165 166 |
# File 'lib/cache.rb', line 163 def fetch(k, ttl = nil, &blk) handle_fork _fetch(k, _get_ttl(ttl), &blk) end |
#flush ⇒ Object Also known as: clear
Flush the cache.
Example:
cache.flush
121 122 123 124 |
# File 'lib/cache.rb', line 121 def flush handle_fork _flush end |
#get(k, ignored_options = nil) ⇒ Object Also known as: read
Get a value.
Example:
cache.get 'hello'
80 81 82 83 |
# File 'lib/cache.rb', line 80 def get(k, = nil) handle_fork _get k end |
#get_multi(*ks) ⇒ Object
Get multiple cache entries.
Example:
cache.get_multi 'hello', 'privyet'
91 92 93 94 |
# File 'lib/cache.rb', line 91 def get_multi(*ks) handle_fork _get_multi ks end |
#increment(k, amount = 1, ignored_options = nil) ⇒ Object
Increment a value.
Example:
cache.increment 'high-fives'
142 143 144 145 146 147 |
# File 'lib/cache.rb', line 142 def increment(k, amount = 1, = nil) handle_fork new_v = _get(k).to_i + amount _set k, new_v, 0 new_v end |
#set(k, v, ttl = nil, ignored_options = nil) ⇒ Object Also known as: write
Store a value. Note that this will Marshal it.
Example:
cache.set 'hello', 'world'
cache.set 'hello', 'world', 80 # seconds til it expires
101 102 103 104 |
# File 'lib/cache.rb', line 101 def set(k, v, ttl = nil, = nil) handle_fork _set k, v, _get_ttl(ttl) end |
#stats ⇒ Object
Get stats.
Example:
cache.stats
205 206 207 208 |
# File 'lib/cache.rb', line 205 def stats handle_fork _stats end |