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, ActiveSupportCacheStore, DalliClient, MemCache, Memcached, MemcachedRails, Redis, RedisNamespace Classes: Config
Constant Summary collapse
- VERSION =
"0.3.3"
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.
Class Method Summary collapse
-
.wrap(metal = nil) ⇒ Object
Create a new Cache instance by wrapping a client of your choice.
Instance Method Summary collapse
-
#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
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 = nil) ⇒ Cache
constructor
:nodoc:.
-
#set(k, v, ttl = nil, ignored_options = nil) ⇒ Object
(also: #write)
Store a value.
-
#stats ⇒ Object
Get stats.
Constructor Details
#initialize(metal = nil) ⇒ Cache
:nodoc:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cache.rb', line 30 def initialize( = nil) #:nodoc: @pid = ::Process.pid @config = Config.new @metal = if .is_a?(Cache) . elsif elsif defined?(::Rails) and ::Rails.respond_to?(:cache) and rails_cache = ::Rails.cache rails_cache else require 'active_support/cache' require 'active_support/cache/memory_store' ::ActiveSupport::Cache::MemoryStore.new end = @metal.class.name.delete('::') # Memcached::Rails -> 'MemcachedRails' require "cache/#{.underscore}" extend Cache.const_get() end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
24 25 26 |
# File 'lib/cache.rb', line 24 def config @config end |
#logger ⇒ Object
For compatibility with Rails 2.x
28 29 30 |
# File 'lib/cache.rb', line 28 def logger @logger end |
#metal ⇒ Object (readonly)
Returns the value of attribute metal.
25 26 27 |
# File 'lib/cache.rb', line 25 def @metal end |
Class Method Details
.wrap(metal = nil) ⇒ Object
Create a new Cache instance by wrapping a client of your choice.
Defaults to an in-process memory store.
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:
Example:
raw_client = Memcached.new('127.0.0.1:11211')
cache = Cache.wrap raw_client
20 21 22 |
# File 'lib/cache.rb', line 20 def self.wrap( = nil) new end |
Instance Method Details
#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' }
150 151 152 153 154 155 156 157 158 |
# File 'lib/cache.rb', line 150 def cas(k, ttl = nil, &blk) handle_fork if blk and _exist?(k) old_v = _get k new_v = blk.call old_v _set k, new_v, extract_ttl(ttl) new_v end end |
#decrement(k, amount = 1, ignored_options = nil) ⇒ Object
Decrement a value.
Example:
cache.decrement 'high-fives'
125 126 127 |
# File 'lib/cache.rb', line 125 def decrement(k, amount = 1, = nil) increment k, -amount end |
#delete(k, ignored_options = nil) ⇒ Object
Delete a value.
Example:
cache.delete 'hello'
85 86 87 88 |
# File 'lib/cache.rb', line 85 def delete(k, = nil) handle_fork _delete k end |
#exist?(k, ignored_options = nil) ⇒ Boolean
Check if something exists.
Example:
cache.exist? 'hello'
105 106 107 108 |
# File 'lib/cache.rb', line 105 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' }
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cache.rb', line 135 def fetch(k, ttl = nil, &blk) handle_fork if _exist? k _get k elsif blk v = blk.call _set k, v, extract_ttl(ttl) v end end |
#flush ⇒ Object Also known as: clear
Flush the cache.
Example:
cache.flush
94 95 96 97 |
# File 'lib/cache.rb', line 94 def flush handle_fork _flush end |
#get(k, ignored_options = nil) ⇒ Object Also known as: read
Get a value.
Example:
cache.get 'hello'
53 54 55 56 |
# File 'lib/cache.rb', line 53 def get(k, = nil) handle_fork _get k end |
#get_multi(*ks) ⇒ Object
Get multiple cache entries.
Example:
cache.get_multi 'hello', 'privyet'
64 65 66 67 |
# File 'lib/cache.rb', line 64 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'
114 115 116 117 118 119 |
# File 'lib/cache.rb', line 114 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
74 75 76 77 |
# File 'lib/cache.rb', line 74 def set(k, v, ttl = nil, = nil) handle_fork _set k, v, extract_ttl(ttl) end |
#stats ⇒ Object
Get stats.
Example:
cache.stats
166 167 168 169 |
# File 'lib/cache.rb', line 166 def stats handle_fork _stats end |