Class: ActiveSupport::Cache::CouchbaseStore

Inherits:
Store
  • Object
show all
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

cache_options = {
  :bucket => 'protected',
  :username => 'protected',
  :password => 'secret',
  :expires_in => 30.seconds
}
config.cache_store = :couchbase_store, cache_options

Defined Under Namespace

Modules: Threadsafe

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CouchbaseStore

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).


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_support/cache/couchbase_store.rb', line 51

def initialize(*args)
  args = [*(args.flatten)]
  options = args.extract_options! || {}
  @raise_errors = !options[:quiet] = !options.delete(:raise_errors)
  options[:default_ttl] ||= options.delete(:expires_in)
  options[:default_format] ||= :marshal
  options[:key_prefix] ||= options.delete(:namespace)
  @key_prefix = options[:key_prefix]
  options[:connection_pool] ||= options.delete(:connection_pool)
  args.push(options)

  if options[:connection_pool]
    if RUBY_VERSION.to_f < 1.9
      warn "connection_pool gem doesn't support ruby < 1.9"
    else
      @data = ::Couchbase::ConnectionPool.new(options[:connection_pool], *args)
    end
  end
  unless @data
    @data = ::Couchbase::Bucket.new(*args)
    @data.extend(Threadsafe)
  end
end

Instance Method Details

#decrement(name, amount = 1, options = nil) ⇒ Fixnum

Decrement an integer value in the cache.

Parameters:

  • name (String)

    name for the key

  • amount (Fixnum) (defaults to: 1)

    (1) the delta value

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :expires_in (Fixnum)

    the expiration time on the cache in seconds. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

  • :initial (Fixnum)

    this option allows to initialize the value if the key is missing in the cache

Returns:

  • (Fixnum)

    new value

Since:

  • 1.2.0.dp5


282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/active_support/cache/couchbase_store.rb', line 282

def decrement(name, amount = 1, options = nil)
  options ||= {}
  name = expanded_key name

  if ttl = options.delete(:expires_in)
    options[:ttl] ||= ttl
  end
  options[:create] = true
  instrument(:decrement, name, options) do |payload|
    payload[:amount] = amount if payload
    @data.decr(name, amount, options)
  end
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  false
end

#delete(name, options = nil) ⇒ true, false

Deletes an entry in the cache.

Returns:

  • (true, false)

    true if an entry is deleted

Since:

  • 1.2.0.dp5


227
228
229
230
231
232
233
234
# File 'lib/active_support/cache/couchbase_store.rb', line 227

def delete(name, options = nil)
  options ||= {}
  name = expanded_key name

  instrument(:delete, name) do
    delete_entry(name, options)
  end
end

#delete_entry(key, options) ⇒ Object (protected)

Delete an entry from the cache.


338
339
340
341
342
343
344
# File 'lib/active_support/cache/couchbase_store.rb', line 338

def delete_entry(key, options) # :nodoc:
  @data.delete(key, options)
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  false
end

#exists?(name, options = nil) ⇒ true, false Also known as: exist?

Return true if the cache contains an entry for the given key.

Returns:

  • (true, false)

Since:

  • 1.2.0.dp5


212
213
214
215
216
217
218
219
# File 'lib/active_support/cache/couchbase_store.rb', line 212

def exists?(name, options = nil)
  options ||= {}
  name = expanded_key name

  instrument(:exists?, name) do
    !read_entry(name, options).nil?
  end
end

#fetch(name, options = nil) ⇒ Object

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.

Parameters:

  • name (String)

    name for the key

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :force (true, false)

    if this option is true it will force cache miss.

  • :expires_in (Fixnum)

    the expiration time on the cache in seconds. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

  • :unless_exists (true, false)

    if this option is true it will write value only if the key doesn't exist in the database (it accepts :unless_exist too).

Returns:

  • (Object)

Since:

  • 1.2.0.dp5


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_support/cache/couchbase_store.rb', line 98

def fetch(name, options = nil)
  options ||= {}
  name = expanded_key(name)

  if block_given?
    unless options[:force]
      entry = instrument(:read, name, options) do |payload|
        payload[:super_operation] = :fetch if payload
        read_entry(name, options)
      end
    end

    if !entry.nil?
      instrument(:fetch_hit, name, options) { |payload| }
      entry
    else
      result = instrument(:generate, name, options) do |payload|
        yield
      end
      write(name, result, options)
      result
    end
  else
    read(name, options)
  end
end

#increment(name, amount = 1, options = nil) ⇒ Fixnum

Increment an integer value in the cache.

Parameters:

  • name (String)

    name for the key

  • amount (Fixnum) (defaults to: 1)

    (1) the delta value

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :expires_in (Fixnum)

    the expiration time on the cache in seconds. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

  • :initial (Fixnum) — default: 1

    this option allows to initialize the value if the key is missing in the cache

Returns:

  • (Fixnum)

    new value

Since:

  • 1.2.0.dp5


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/active_support/cache/couchbase_store.rb', line 250

def increment(name, amount = 1, options = nil)
  options ||= {}
  name = expanded_key name

  if ttl = options.delete(:expires_in)
    options[:ttl] ||= ttl
  end
  options[:create] = true
  instrument(:increment, name, options) do |payload|
    payload[:amount] = amount if payload
    @data.incr(name, amount, options)
  end
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  false
end

#read(name, options = nil) ⇒ Object

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.

Parameters:

  • name (String)

    name for the key

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :expires_in (Fixnum)

    the expiration time on the cache in seconds. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

  • :raw (true, false)

    do not marshal the value if this option is true

Returns:

  • (Object)

Since:

  • 1.2.0.dp5


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/active_support/cache/couchbase_store.rb', line 168

def read(name, options = nil)
  options ||= {}
  name = expanded_key name
  if options.delete(:raw)
    options[:format] = :plain
  end

  instrument(:read, name, options) do |payload|
    entry = read_entry(name, options)
    payload[:hit] = !!entry if payload
    entry
  end
end

#read_entry(key, options) ⇒ Object (protected)

Read an entry from the cache.


312
313
314
315
316
317
318
# File 'lib/active_support/cache/couchbase_store.rb', line 312

def read_entry(key, options) # :nodoc:
  @data.get(key, options)
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  nil
end

#read_multi(*names) ⇒ Hash

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.

Returns:

  • (Hash)

    key-value pairs

Since:

  • 1.2.0.dp5


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/active_support/cache/couchbase_store.rb', line 191

def read_multi(*names)
  options = names.extract_options!
  names = names.flatten.map{|name| expanded_key(name)}
  options[:assemble_hash] = true
  if options.delete(:raw)
    options[:format] = :plain
  end
  instrument(:read_multi, names, options) do
    @data.get(names, options)
  end
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  false
end

#stats(*arg) ⇒ Hash

Get the statistics from the memcached servers.

Returns:

  • (Hash)

Since:

  • 1.2.0.dp5


305
306
307
# File 'lib/active_support/cache/couchbase_store.rb', line 305

def stats(*arg)
  @data.stats(*arg)
end

#write(name, value, options = nil) ⇒ Fixnum, false

Writes the value to the cache, with the key

Parameters:

  • name (String)

    name for the key

  • value (Object)

    value of the key

  • options (Hash) (defaults to: nil)

Options Hash (options):

  • :expires_in (Fixnum)

    the expiration time on the cache in seconds. Values larger than 30*24*60*60 seconds (30 days) are interpreted as absolute times (from the epoch).

Returns:

  • (Fixnum, false)

    false in case of failure and CAS value otherwise (it could be used as true value)

Since:

  • 1.2.0.dp5


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/active_support/cache/couchbase_store.rb', line 138

def write(name, value, options = nil)
  options ||= {}
  name = expanded_key name
  if options.delete(:raw)
    options[:format] = :plain
    value = value.to_s
    value.force_encoding(Encoding::BINARY) if defined?(Encoding)
  end

  instrument(:write, name, options) do |payload|
    write_entry(name, value, options)
  end
end

#write_entry(key, value, options) ⇒ Object (protected)

Write an entry to the cache.


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/active_support/cache/couchbase_store.rb', line 321

def write_entry(key, value, options) # :nodoc:
  method = if options[:unless_exists] || options[:unless_exist]
             :add
           else
             :set
           end
  if ttl = options.delete(:expires_in)
    options[:ttl] ||= ttl
  end
  @data.send(method, key, value, options)
rescue ::Couchbase::Error::Base => e
  logger.error("#{e.class}: #{e.message}") if logger
  raise if @raise_errors
  false
end