Class: Moneta::Cache

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/cache.rb

Overview

Combines two stores. One is used as cache, the other as backend adapter.

Examples:

Add ‘Moneta::Cache` to proxy stack

Moneta.build do
  use(:Cache) do
   adapter { adapter :File, :dir => 'data' }
   cache { adapter :Memory }
  end
end

Defined Under Namespace

Classes: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Defaults

#[], #[]=, #decrement, #fetch, included, #supports?

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(options = {}) {|Builder| ... } ⇒ Cache

Returns a new instance of Cache.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :cache (Moneta store)

    Moneta store used as cache

  • :adapter (Moneta store)

    Moneta store used as adapter

Yield Parameters:

  • Builder

    block



44
45
46
47
# File 'lib/moneta/cache.rb', line 44

def initialize(options = {}, &block)
  @cache, @adapter = options[:cache], options[:adapter]
  DSL.new(self, &block) if block_given?
end

Instance Attribute Details

#adapterObject



38
39
40
# File 'lib/moneta/cache.rb', line 38

def adapter
  @adapter
end

#cacheObject



38
39
40
# File 'lib/moneta/cache.rb', line 38

def cache
  @cache
end

Instance Method Details

#clear(options = {}) ⇒ void

This method returns an undefined value.

Clear all keys in this store

Parameters:

  • options (Hash) (defaults to: {})


93
94
95
96
97
# File 'lib/moneta/cache.rb', line 93

def clear(options = {})
  @cache.clear(options)
  @adapter.clear(options)
  self
end

#closeObject

Explicitly close the store

Returns:

  • nil



100
101
102
103
# File 'lib/moneta/cache.rb', line 100

def close
  @cache.close
  @adapter.close
end

#create(key, value, options = {}) ⇒ Boolean

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically sets a key to value if it’s not set.

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

Returns:

  • (Boolean)

    key was set



77
78
79
80
81
82
83
84
# File 'lib/moneta/cache.rb', line 77

def create(key, value, options = {})
  if @adapter.create(key, value, options)
    @cache.store(key, value, options)
    true
  else
    false
  end
end

#delete(key, options = {}) ⇒ Object

Delete the key from the store and return the current value

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value



87
88
89
90
# File 'lib/moneta/cache.rb', line 87

def delete(key, options = {})
  @cache.delete(key, options)
  @adapter.delete(key, options)
end

#featuresObject



106
107
108
# File 'lib/moneta/cache.rb', line 106

def features
  @features ||= ((@cache.features + [:create, :increment]) & @adapter.features).freeze
end

#increment(key, amount = 1, options = {}) ⇒ Object

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Atomically increment integer value with key

This method also accepts negative amounts.

Parameters:

  • key (Object)
  • amount (Integer) (defaults to: 1)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value from store



71
72
73
74
# File 'lib/moneta/cache.rb', line 71

def increment(key, amount = 1, options = {})
  @cache.delete(key, options)
  @adapter.increment(key, amount, options)
end

#key?(key, options = {}) ⇒ Boolean

Exists the value with key

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Boolean)


50
51
52
# File 'lib/moneta/cache.rb', line 50

def key?(key, options = {})
  @cache.key?(key, options) || @adapter.key?(key, options)
end

#load(key, options = {}) ⇒ Object

Fetch value with key. Return nil if the key doesn’t exist

Parameters:

  • key (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Update expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    value



55
56
57
58
59
60
61
62
# File 'lib/moneta/cache.rb', line 55

def load(key, options = {})
  value = @cache.load(key, options)
  if value == nil
    value = @adapter.load(key, options)
    @cache.store(key, value, options) if value != nil
  end
  value
end

#store(key, value, options = {}) ⇒ Object

Store value with key

Parameters:

  • key (Object)
  • value (Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :expires (Integer)

    Set expiration time (See Expires)

  • :raw (Boolean)

    Raw access without value transformation (See Transformer)

  • :prefix (String)

    Prefix key (See Transformer)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • value



65
66
67
68
# File 'lib/moneta/cache.rb', line 65

def store(key, value, options = {})
  @cache.store(key, value, options)
  @adapter.store(key, value, options)
end