Class: ActiveSupport::Cache::CouchbaseStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::CouchbaseStore
- Defined in:
- lib/active_support/cache/couchbase_store.rb
Overview
This class implements Cache interface for Rails. To use it just put following line in your config/application.rb file:
config.cache_store = :couchbase_store
You can also pass additional connection options there
= {
:bucket => 'protected',
:username => 'protected',
:password => 'secret',
:expires_in => 30.seconds
}
config.cache_store = :couchbase_store,
Instance Method Summary (collapse)
-
- (Fixnum) decrement(name, amount = 1, options = nil)
Decrement an integer value in the cache.
-
- (true, false) delete(name, options = nil)
Deletes an entry in the cache.
-
- (Object) delete_entry(key, options)
protected
Delete an entry from the cache.
-
- (true, false) exists?(name, options = nil)
(also: #exist?)
Return true if the cache contains an entry for the given key.
-
- (Object) fetch(name, options = nil)
Fetches data from the cache, using the given key.
-
- (Fixnum) increment(name, amount = 1, options = nil)
Increment an integer value in the cache.
-
- (CouchbaseStore) initialize(*args)
constructor
Creates a new CouchbaseStore object, with the given options.
-
- (Object) read(name, options = nil)
Fetches data from the cache, using the given key.
-
- (Object) read_entry(key, options)
protected
Read an entry from the cache.
-
- (Hash) read_multi(*names)
Read multiple values at once from the cache.
-
- (Hash) stats(arg = nil)
Get the statistics from the memcached servers.
-
- (Fixnum, false) write(name, value, options = nil)
Writes the value to the cache, with the key.
-
- (Object) write_entry(key, value, options)
protected
Write an entry to the cache.
Constructor Details
- (CouchbaseStore) initialize(*args)
Creates a new CouchbaseStore object, with the given options. For more info see ActiveSupport::Cache::CouchbaseStore.{Couchbase{Couchbase::Bucket{Couchbase::Bucket#initialize}
ActiveSupport::Cache::CouchbaseStore.new(:bucket => "cache")
If no options are specified, then CouchbaseStore will connect to localhost port 8091 (default Couchbase Server port) and will use bucket named "default" which is always open for unauthorized access (if exists).
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_support/cache/couchbase_store.rb', line 49 def initialize(*args) args = [*(args.flatten)] = args. || {} @raise_errors = ![:quiet] = !.delete(:raise_errors) [:default_ttl] ||= .delete(:expires_in) [:default_format] ||= :marshal [:key_prefix] ||= .delete(:namespace) args.push() @data = ::Couchbase::Bucket.new(*args) end |
Instance Method Details
- (Fixnum) decrement(name, amount = 1, options = nil)
Decrement an integer value in the cache.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/active_support/cache/couchbase_store.rb', line 267 def decrement(name, amount = 1, = nil) ||= {} name = name if ttl = .delete(:expires_in) [:ttl] ||= ttl end [:create] = true instrument(:decrement, name, ) do |payload| payload[:amount] = amount if payload @data.decr(name, amount, ) end rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors false end |
- (true, false) delete(name, options = nil)
Deletes an entry in the cache.
212 213 214 215 216 217 218 219 |
# File 'lib/active_support/cache/couchbase_store.rb', line 212 def delete(name, = nil) ||= {} name = name instrument(:delete, name) do delete_entry(name, ) end end |
- (Object) delete_entry(key, options) (protected)
Delete an entry from the cache.
323 324 325 326 327 328 329 |
# File 'lib/active_support/cache/couchbase_store.rb', line 323 def delete_entry(key, ) # :nodoc: @data.delete(key, ) rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors false end |
- (true, false) exists?(name, options = nil) Also known as: exist?
Return true if the cache contains an entry for the given key.
197 198 199 200 201 202 203 204 |
# File 'lib/active_support/cache/couchbase_store.rb', line 197 def exists?(name, = nil) ||= {} name = name instrument(:exists?, name) do !read_entry(name, ).nil? end end |
- (Object) fetch(name, options = nil)
Fetches data from the cache, using the given key.
If there is data in the cache with the given key, then that data is returned. If there is no such data in the cache (a cache miss), then nil will be returned. However, if a block has been passed, that block will be run in the event of a cache miss. The return value of the block will be written to the cache under the given cache key, and that return value will be returned.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/active_support/cache/couchbase_store.rb', line 83 def fetch(name, = nil) ||= {} name = (name) if block_given? unless [:force] entry = instrument(:read, name, ) do |payload| payload[:super_operation] = :fetch if payload read_entry(name, ) end end if !entry.nil? instrument(:fetch_hit, name, ) { |payload| } entry else result = instrument(:generate, name, ) do |payload| yield end write(name, result, ) result end else read(name, ) end end |
- (Fixnum) increment(name, amount = 1, options = nil)
Increment an integer value in the cache.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/active_support/cache/couchbase_store.rb', line 235 def increment(name, amount = 1, = nil) ||= {} name = name if ttl = .delete(:expires_in) [:ttl] ||= ttl end [:create] = true instrument(:increment, name, ) do |payload| payload[:amount] = amount if payload @data.incr(name, amount, ) end rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors false end |
- (Object) read(name, options = nil)
Fetches data from the cache, using the given key.
If there is data in the cache with the given key, then that data is returned. Otherwise, nil is returned.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/active_support/cache/couchbase_store.rb', line 153 def read(name, = nil) ||= {} name = name if .delete(:raw) [:format] = :plain end instrument(:read, name, ) do |payload| entry = read_entry(name, ) payload[:hit] = !!entry if payload entry end end |
- (Object) read_entry(key, options) (protected)
Read an entry from the cache.
297 298 299 300 301 302 303 |
# File 'lib/active_support/cache/couchbase_store.rb', line 297 def read_entry(key, ) # :nodoc: @data.get(key, ) rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors nil end |
- (Hash) read_multi(*names)
Read multiple values at once from the cache.
Options can be passed in the last argument.
Returns a hash mapping the names provided to the values found.
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_support/cache/couchbase_store.rb', line 176 def read_multi(*names) = names. names = names.flatten.map{|name| (name)} [:assemble_hash] = true if .delete(:raw) [:format] = :plain end instrument(:read_multi, names, ) do @data.get(names, ) end rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors false end |
- (Hash) stats(arg = nil)
Get the statistics from the memcached servers.
290 291 292 |
# File 'lib/active_support/cache/couchbase_store.rb', line 290 def stats(arg = nil) @data.stats(arg) end |
- (Fixnum, false) write(name, value, options = nil)
Writes the value to the cache, with the key
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/active_support/cache/couchbase_store.rb', line 123 def write(name, value, = nil) ||= {} name = name if .delete(:raw) [:format] = :plain value = value.to_s value.force_encoding(Encoding::BINARY) if defined?(Encoding) end instrument(:write, name, ) do |payload| write_entry(name, value, ) end end |
- (Object) write_entry(key, value, options) (protected)
Write an entry to the cache.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/active_support/cache/couchbase_store.rb', line 306 def write_entry(key, value, ) # :nodoc: method = if [:unless_exists] || [:unless_exist] :add else :set end if ttl = .delete(:expires_in) [:ttl] ||= ttl end @data.send(method, key, value, ) rescue Couchbase::Error::Base => e logger.error("#{e.class}: #{e.message}") if logger raise if @raise_errors false end |