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.

Examples:

Add `Moneta::Cache` to proxy stack

Moneta.build do
  use(:Cache) do
   backend { 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

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

  • :backend (Moneta store)

    Moneta store used as backend

Yield Parameters:

  • Builder

    block


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

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

Instance Attribute Details

#backendObject


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

def backend
  @backend
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: {})

83
84
85
86
87
# File 'lib/moneta/cache.rb', line 83

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

#closeObject

Explicitly close the store

Returns:

  • nil


90
91
92
93
# File 'lib/moneta/cache.rb', line 90

def close
  @cache.close
  @backend.close
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 `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::Transformer`)

  • Other (Object)

    options as defined by the adapters or middleware

Returns:

  • (Object)

    current value


77
78
79
80
# File 'lib/moneta/cache.rb', line 77

def delete(key, options = {})
  @cache.delete(key, options)
  @backend.delete(key, options)
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: {})

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)
  @backend.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 `Moneta::Expires`)

  • :prefix (String)

    Prefix key (See `Moneta::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) || @backend.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 `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::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 = @backend.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 `Moneta::Expires`)

  • :raw (Boolean)

    Raw access without value transformation (See `Moneta::Transformer`)

  • :prefix (String)

    Prefix key (See `Moneta::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)
  @backend.store(key, value, options)
end