Class: Cachetastic::Adapters::Mongoid

Inherits:
Base
  • Object
show all
Defined in:
lib/cachetastic/adapters/mongoid.rb

Defined Under Namespace

Classes: Store

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#debug?, #marshal, #unmarshal, #valid?

Constructor Details

#initialize(klass) ⇒ Mongoid

Returns a new instance of Mongoid.


24
25
26
27
# File 'lib/cachetastic/adapters/mongoid.rb', line 24

def initialize(klass)
  self.klass = klass
  super
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass


22
23
24
# File 'lib/cachetastic/adapters/mongoid.rb', line 22

def klass
  @klass
end

Instance Method Details

#delete(key) ⇒ Object

set


46
47
48
49
50
51
52
# File 'lib/cachetastic/adapters/mongoid.rb', line 46

def delete(key) # :nodoc:
  obj = connection.where(key: key).first
  if obj
    obj.destroy
  end
  return nil
end

#expire_allObject

delete


54
55
56
57
# File 'lib/cachetastic/adapters/mongoid.rb', line 54

def expire_all # :nodoc:
  connection.destroy_all
  return nil
end

#get(key) ⇒ Object

:nodoc:


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cachetastic/adapters/mongoid.rb', line 29

def get(key) # :nodoc:
  obj = connection.where(key: key).first
  if obj
    if obj.expiry_time > Time.now
      return obj.value
    else
      obj.destroy
    end
  end
  return nil
end

#set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) ⇒ Object

get


41
42
43
44
# File 'lib/cachetastic/adapters/mongoid.rb', line 41

def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) # :nodoc:
  Cachetastic::Adapters::Mongoid::Store.create!(cache_class: self.klass.name, key: key, value: value, expiry_time: expiry_time.from_now)
  return value
end

#transform_key(key) ⇒ Object

expire_all


59
60
61
# File 'lib/cachetastic/adapters/mongoid.rb', line 59

def transform_key(key) # :nodoc:
  key.to_s.hexdigest
end