Files

Padrino::Cache::Store::Mongo

MongoDB Cache Store

Public Class Methods

new(client, options={}) click to toggle source

Initialize Mongo store with client connection and optional username and password.

@param client

Instance of Mongo connection

@param [Hash] opts

optiosn to pass into Mongo connection

@example

Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
# or from your app
set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
# you can provide a marshal parser (to store ruby objects)
set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :parser => :marshal)

@api public

# File lib/padrino-cache/store/mongo.rb, line 24
def initialize(client, options={})
  @client = client
  @options = {
    :capped => true,
    :collection => 'cache',
    :size => 64,
    :max => 100
  }.merge(options)

  if @options[:username] && @options[:password]
    @client.authenticate(@options[:username], @options[:password], true)
  end
  @backend = get_collection
  super(options)
end

Public Instance Methods

delete(key) click to toggle source

Delete the value for a given key

@param [String] key

cache key

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.delete('records')

@api public

# File lib/padrino-cache/store/mongo.rb, line 97
def delete(key)
  if not @options[:capped]
    @backend.remove({:_id => key})
  else
    # Mongo will overwrite it with a simple object {_id: new ObjectId()}
    @backend.update({:_id => key},{},{:upsert => true})
  end
end
flush() click to toggle source

Reinitialize your cache

@example

# with: MyApp.cache.set('records', records)
MyApp.cache.flush
MyApp.cache.get('records') # => nil

@api public

# File lib/padrino-cache/store/mongo.rb, line 115
def flush
  @backend.drop
  @backend = get_collection
end
get(key) click to toggle source

Return the a value for the given key

@param [String] key

cache key

@example

# with MyApp.cache.set('records', records)
MyApp.cache.get('records')
# File lib/padrino-cache/store/mongo.rb, line 50
def get(key)
  doc = @backend.find_one( :_id => key, '$or' => [ { :expires_at => { '$gt' => Time.now.to_i } }, { :expires_at => -1 } ] )
  return nil if doc.nil?
  expiry = doc['expires_at']
  if now_before? expiry
    parser.decode(doc['value'].to_s)
  else
    delete(key)
    nil
  end
end
set(key, value, opts = nil) click to toggle source

Set or update the value for a given key and optionally with an expire time Default expiry is Time.now + 86400s.

@param [String] key

cache key

@param value

value of cache key

@example

MyApp.cache.set('records', records)
MyApp.cache.set('records', records, :expires_in => 30) # => 30 seconds

@api public

# File lib/padrino-cache/store/mongo.rb, line 76
def set(key, value, opts = nil)
  key = key.to_s
  value = BSON::Binary.new(parser.encode(value)) if value
  @backend.update(
    {:_id => key},
    {:_id => key, :value => value, :expires_at => get_expiry(opts) },
    {:upsert => true}
  )
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.